Path: redb-route/redb.Route.IbmMq/IbmMqSendBatch.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.IbmMq/IbmMqSendBatch.cs
----
using System.Collections.Concurrent;
using IBM.WMQ;
using Microsoft.Extensions.Logging;
using redb.Route.Abstractions;
namespace redb.Route.IbmMq;
///
/// The deferred puts of one producer in one transacted block. On commit every message is put under syncpoint and the
/// unit of work is committed once (MQCMIT), so the block's puts arrive together or not at all; a failed put backs the
/// unit of work out (MQBACK). On rollback nothing has been put. A lost connection during the puts or the commit is
/// reported to the producer, so its next send reconnects; one during MQCMIT leaves the outcome in doubt and says so.
///
internal sealed class IbmMqSendBatch : ITransactedAction
{
private readonly ConcurrentQueue _messages = new();
private readonly MQQueue? _queue;
private readonly MQTopic? _topic;
private readonly IbmMqDestinationType _destinationType;
private readonly string _destination;
private readonly MQQueueManager _queueManager;
private readonly SemaphoreSlim _commitLock;
private readonly ILogger? _logger;
private readonly Action _recordBytesOut;
private readonly Action _onConnectionLost;
///
/// The producer's lock: its connection is shared by every exchange, and a unit of work belongs to the connection, so
/// one batch at a time puts and commits on it. Another batch's MQCMIT would otherwise commit half of this one.
///
/// The endpoint's BytesOut, given the batch's message bytes once MQCMIT succeeded.
/// Tells the producer its connection is lost, when a put or the commit says so.
public IbmMqSendBatch(
MQQueue? queue,
MQTopic? topic,
IbmMqDestinationType destinationType,
string destination,
MQQueueManager queueManager,
SemaphoreSlim commitLock,
ILogger? logger,
Action recordBytesOut,
Action onConnectionLost)
{
_queue = queue;
_topic = topic;
_destinationType = destinationType;
_destination = destination;
_queueManager = queueManager;
_commitLock = commitLock;
_logger = logger;
_recordBytesOut = recordBytesOut;
_onConnectionLost = onConnectionLost;
}
/// Adds a message to the batch, after the ones the block deferred before it.
public void Add(MQMessage message) => _messages.Enqueue(message);
///
public async Task Commit(CancellationToken ct = default)
{
await _commitLock.WaitAsync(ct).ConfigureAwait(false);
try
{
var pmo = new MQPutMessageOptions
{
Options = MQC.MQPMO_SYNCPOINT | MQC.MQPMO_FAIL_IF_QUIESCING
};
var committing = false;
try
{
foreach (var message in _messages)
{
if (_destinationType == IbmMqDestinationType.Topic)
_topic!.Put(message, pmo);
else
_queue!.Put(message, pmo);
}
committing = true;
_queueManager.Commit();
}
catch (MQException ex) when (IbmMqConnectionLoss.IsConnectionLost(ex.ReasonCode))
{
// The queue manager backs out an uncommitted unit of work when its connection ends, so there is nothing
// to back out here. A loss during MQCMIT itself leaves the outcome unknown: the commit may have been
// done before the connection broke.
if (committing)
_logger?.LogWarning(ex,
"IBM MQ connection lost during the commit of a transactional batch: destination={Destination}, " +
"messages={Count}. The outcome is in doubt: the messages may or may not have been committed.",
_destination, _messages.Count);
_onConnectionLost(ex);
throw;
}
catch (MQException)
{
// None of the batch may stand without the rest: back the unit of work out, then report the failure.
BackOut();
throw;
}
long sent = 0;
foreach (var message in _messages)
sent += message.MessageLength;
_recordBytesOut(sent);
_logger?.LogDebug("IBM MQ transactional batch committed: destination={Destination}, messages={Count}",
_destination, _messages.Count);
}
finally
{
_commitLock.Release();
}
}
///
public Task Rollback(CancellationToken ct = default)
{
_logger?.LogDebug("IBM MQ transactional batch rolled back: destination={Destination}, messages={Count}",
_destination, _messages.Count);
return Task.CompletedTask;
}
private void BackOut()
{
try
{
_queueManager.Backout();
}
catch (MQException backoutEx)
{
_logger?.LogWarning(backoutEx,
"IBM MQ backout after a failed batch put failed: destination={Destination}. The unit of work ends with the " +
"connection.", _destination);
}
}
}