Path: redb-route/redb.Route.IbmMq/Fluent/IbmMqDsl.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/Fluent/IbmMqDsl.cs
----
using System.Text;
using redb.Route.Abstractions;
namespace redb.Route.IbmMq;
///
/// Fluent API for IBM MQ endpoints.
///
/// .From(Wmq.Queue("DEV.QUEUE.1").Host("mq-host").QueueManager("QM1").Channel("DEV.APP.SVRCONN"))
/// .To(Wmq.Queue("ORDERS.OUT").Host("mq-host").Persistent())
/// .From(Wmq.Topic("EVENTS/ORDER").Host("mq-host").QueueManager("QM1"))
///
///
public static class Wmq
{
/// Creates an IBM MQ queue endpoint.
public static IbmMqBuilder Queue(string name) => new(name, IbmMqDestinationType.Queue);
/// Creates an IBM MQ topic endpoint.
public static IbmMqBuilder Topic(string name) => new(name, IbmMqDestinationType.Topic);
}
///
/// Fluent builder for IBM MQ endpoint URIs.
///
public sealed class IbmMqBuilder
{
private readonly string _destination;
private readonly IbmMqDestinationType _destType;
// Connection
private string? _connectionFactory;
private string? _host;
private int? _port;
private string? _channel;
private string? _queueManager;
private string? _user;
private string? _password;
private string? _clientId;
// Consumer
private IbmMqReceiveMode? _receiveMode;
private string? _concurrentConsumers;
private int? _waitInterval;
private bool _batching;
private int? _maxBatchMessages;
private int? _pollTimeoutMs;
private int? _batchingIntervalMs;
private bool _disableReplyTo;
private int? _backoutThreshold;
private string? _backoutQueue;
private int? _backoutDelay;
private string? _selector;
private bool? _convert;
// Producer
private IbmMqPersistence? _persistence;
private string? _persistenceExpr;
private string? _priority;
private string? _expiry;
private IbmMqTargetClient? _targetClient;
private IbmMqMessageType? _messageType;
private string? _messageTypeExpr;
// Transactions
private bool? _transacted;
// Dead Letter
// RPC
private bool _replyTo;
private bool _preserveMessageQos;
private string? _replyToQueue;
private string? _replyToQueueManager;
private string? _temporaryModel;
private int? _timeout;
private IbmMqCorrelationPattern? _correlationPattern;
// SSL
private string? _sslCipherSpec;
private string? _sslCertLabel;
private string? _sslPeerName;
private string? _sslKeyRepository;
private int? _sslKeyResetCount;
// Advanced
private int? _ccsid;
private bool? _mqmdWriteEnabled;
private bool? _mqmdReadEnabled;
internal IbmMqBuilder(string destination, IbmMqDestinationType destType)
{
ArgumentException.ThrowIfNullOrWhiteSpace(destination);
_destination = destination;
_destType = destType;
}
// ── Connection ────────────────────────────────────────────────────
/// Use a named connection factory registered in DI.
public IbmMqBuilder ConnectionFactory(string name) { _connectionFactory = name; return this; }
/// Queue manager hostname. Default "localhost".
public IbmMqBuilder Host(string host) { _host = host; return this; }
/// MQ listener port. Default 1414.
public IbmMqBuilder Port(int port) { _port = port; return this; }
/// Server-connection channel. Default "DEV.APP.SVRCONN".
public IbmMqBuilder Channel(string channel) { _channel = channel; return this; }
/// Queue manager name. Default "QM1".
public IbmMqBuilder QueueManager(string qm) { _queueManager = qm; return this; }
/// Authentication user.
public IbmMqBuilder User(string user) { _user = user; return this; }
/// Authentication password.
public IbmMqBuilder Password(string password) { _password = password; return this; }
/// Client identifier for connection tracking.
public IbmMqBuilder ClientId(string id) { _clientId = id; return this; }
// ── Consumer ──────────────────────────────────────────────────────
///
/// Receive strategy: (synchronous MQGET-WAIT, default) or
/// (event-driven XMS push, <50 ms latency).
///
public IbmMqBuilder ReceiveMode(IbmMqReceiveMode mode) { _receiveMode = mode; return this; }
/// Shortcut for ReceiveMode(IbmMqReceiveMode.Listener) — event-driven XMS receive.
public IbmMqBuilder Listener() { _receiveMode = IbmMqReceiveMode.Listener; return this; }
/// Number of concurrent consumers. Default 1.
public IbmMqBuilder ConcurrentConsumers(int count) { _concurrentConsumers = count.ToString(); return this; }
/// Consumer parallelism as a string: a number or "auto" (= max(CPU, 2)).
public IbmMqBuilder ConcurrentConsumers(string count) { _concurrentConsumers = count; return this; }
/// MQGET wait interval in milliseconds. Default 5000.
public IbmMqBuilder WaitInterval(int ms) { _waitInterval = ms; return this; }
/// Batch mode (batching=true): the route gets the messages as a List<IMessage>, read under syncpoint.
public IbmMqBuilder Batching() { _batching = true; return this; }
/// Largest number of messages in a batch (maxBatchMessages, default 100).
public IbmMqBuilder MaxBatchMessages(int max) { _maxBatchMessages = max; return this; }
/// Idle timeout of a batch in milliseconds (pollTimeoutMs, default 1000).
public IbmMqBuilder PollTimeoutMs(int ms) { _pollTimeoutMs = ms; return this; }
/// Hard limit of a batch in milliseconds from its first message (batchingIntervalMs).
public IbmMqBuilder BatchingIntervalMs(int ms) { _batchingIntervalMs = ms; return this; }
/// The consumer ignores a request's reply-to (Apache Camel disableReplyTo): no reply, and a batch takes it.
public IbmMqBuilder DisableReplyTo() { _disableReplyTo = true; return this; }
/// Backout threshold before routing to backout queue.
public IbmMqBuilder BackoutThreshold(int threshold) { _backoutThreshold = threshold; return this; }
/// Backout (poison message) queue name.
public IbmMqBuilder BackoutQueue(string queue) { _backoutQueue = queue; return this; }
///
/// Pause after a backout in milliseconds (default 1000, 0 = none): no worker of the consumer reads again until it
/// has passed. Poll and batch modes. redb's addition, not in Apache Camel.
///
public IbmMqBuilder BackoutDelay(int ms) { _backoutDelay = ms; return this; }
/// Message selector expression.
public IbmMqBuilder Selector(string selector) { _selector = selector; return this; }
/// Apply MQGMO_CONVERT on receive. Default true.
public IbmMqBuilder Convert(bool convert = true) { _convert = convert; return this; }
// ── Producer ──────────────────────────────────────────────────────
/// Set messages as persistent.
public IbmMqBuilder Persistent() { _persistence = IbmMqPersistence.Persistent; return this; }
/// Set messages as non-persistent.
public IbmMqBuilder NonPersistent() { _persistence = IbmMqPersistence.NonPersistent; return this; }
/// Message persistence mode.
public IbmMqBuilder Persistence(IbmMqPersistence mode) { _persistence = mode; return this; }
/// Message persistence from an expression (resolves to enum name or numeric value).
public IbmMqBuilder Persistence(IExpression expr) { _persistenceExpr = expr.ToTemplateString(); return this; }
/// Message priority (0–9). -1 = queue default.
public IbmMqBuilder Priority(int priority) { _priority = priority.ToString(); return this; }
/// Message priority from an expression.
public IbmMqBuilder Priority(IExpression expr) { _priority = expr.ToTemplateString(); return this; }
/// Message expiry in tenths of a second. -1 = unlimited.
public IbmMqBuilder Expiry(int tenthsOfSecond) { _expiry = tenthsOfSecond.ToString(); return this; }
/// Message expiry from an expression.
public IbmMqBuilder Expiry(IExpression expr) { _expiry = expr.ToTemplateString(); return this; }
/// Target client mode — Jms (with RFH2) or Mq (raw MQMD).
public IbmMqBuilder TargetClient(IbmMqTargetClient client) { _targetClient = client; return this; }
/// Message type for outgoing messages.
public IbmMqBuilder MessageType(IbmMqMessageType type) { _messageType = type; return this; }
/// Message type from an expression (resolves to enum name or numeric value).
public IbmMqBuilder MessageType(IExpression expr) { _messageTypeExpr = expr.ToTemplateString(); return this; }
// ── Transactions ──────────────────────────────────────────────────
/// Enable local MQ transactions.
public IbmMqBuilder Transacted() { _transacted = true; return this; }
///
/// Sets transacted explicitly. false sends at once even inside a .Transacted() block, outside its
/// transaction; left unset, a producer follows the block.
///
public IbmMqBuilder Transacted(bool value) { _transacted = value; return this; }
// DeadLetterQueue/MaxRedeliveries verbs removed (the options sweep): a second,
// dead vocabulary for poison handling - the implemented one is IBM MQ's native
// BackoutThreshold/BackoutQueue pair (BOTHRESH/BOQNAME), see .BackoutThreshold().
// ── RPC ───────────────────────────────────────────────────────────
/// Enable request/reply pattern.
public IbmMqBuilder ReplyTo() { _replyTo = true; return this; }
///
/// Keep the reply-to headers as the reply-to of an ordinary send (Apache Camel preserveMessageQos); without it a
/// forwarded sender's reply-to is not carried on.
///
public IbmMqBuilder PreserveMessageQos() { _preserveMessageQos = true; return this; }
/// Reply-to queue name (if not using dynamic temporary queue).
public IbmMqBuilder ReplyToQueue(string queue) { _replyToQueue = queue; _replyTo = true; return this; }
/// Reply-to queue manager.
public IbmMqBuilder ReplyToQueueManager(string qm) { _replyToQueueManager = qm; return this; }
///
/// Model queue of the dynamic reply queue (IBM MQ TEMPMODEL). Default SYSTEM.DEFAULT.MODEL.QUEUE; the
/// developer image grants its app user DEV.APP.MODEL.QUEUE.
///
public IbmMqBuilder TemporaryModel(string modelQueue) { _temporaryModel = modelQueue; return this; }
/// RPC timeout in seconds. Default 30.
public IbmMqBuilder Timeout(int seconds) { _timeout = seconds; return this; }
/// RPC correlation pattern — MsgId or CorrelId.
public IbmMqBuilder CorrelationPattern(IbmMqCorrelationPattern pattern) { _correlationPattern = pattern; return this; }
// ── SSL ───────────────────────────────────────────────────────────
/// SSL CipherSpec name.
public IbmMqBuilder SslCipherSpec(string cipher) { _sslCipherSpec = cipher; return this; }
/// SSL certificate label.
public IbmMqBuilder SslCertLabel(string label) { _sslCertLabel = label; return this; }
/// SSL peer name (DN pattern).
public IbmMqBuilder SslPeerName(string peerName) { _sslPeerName = peerName; return this; }
/// SSL key repository path (without .kdb extension).
public IbmMqBuilder SslKeyRepository(string path) { _sslKeyRepository = path; return this; }
/// SSL key reset count in bytes.
public IbmMqBuilder SslKeyResetCount(int bytes) { _sslKeyResetCount = bytes; return this; }
// ── Advanced ──────────────────────────────────────────────────────
/// Coded Character Set ID. Default 1208 (UTF-8).
public IbmMqBuilder CCSID(int ccsid) { _ccsid = ccsid; return this; }
/// Allow writing MQMD fields from exchange headers.
public IbmMqBuilder MqmdWriteEnabled(bool enabled = true) { _mqmdWriteEnabled = enabled; return this; }
/// Read all MQMD fields into exchange headers. Default true.
public IbmMqBuilder MqmdReadEnabled(bool enabled = true) { _mqmdReadEnabled = enabled; return this; }
// ── Build ─────────────────────────────────────────────────────────
/// Builds the IBM MQ endpoint URI string.
public string Build()
{
var sb = new StringBuilder();
sb.Append("wmq:");
sb.Append(_destination);
var sep = '?';
void Append(string key, string value)
{
sb.Append(sep); sb.Append(key); sb.Append('=');
sb.Append(Uri.EscapeDataString(value)); sep = '&';
}
void AppendIf(string key, string? value) { if (value != null) Append(key, value); }
void AppendBool(string key, bool value) { if (value) Append(key, "true"); }
void AppendBoolExplicit(string key, bool? value)
{
if (value.HasValue) Append(key, value.Value.ToString().ToLowerInvariant());
}
void AppendInt(string key, int? value) { if (value.HasValue) Append(key, value.Value.ToString()); }
void AppendIntOrExpression(string key, string? value)
{
if (value is null) return;
if (value.Contains("${"))
Append(key + "Expression", value);
else
Append(key, value);
}
// Destination type (only if topic — queue is default)
if (_destType == IbmMqDestinationType.Topic)
Append("destinationType", "Topic");
// Connection
AppendIf("connectionFactory", _connectionFactory);
AppendIf("host", _host);
AppendInt("port", _port);
AppendIf("channel", _channel);
AppendIf("queueManager", _queueManager);
AppendIf("user", _user);
AppendIf("password", _password);
AppendIf("clientId", _clientId);
// Consumer
if (_receiveMode.HasValue)
Append("receiveMode", _receiveMode.Value.ToString());
AppendIf("concurrentConsumers", _concurrentConsumers);
AppendInt("waitInterval", _waitInterval);
AppendBool("batching", _batching);
AppendInt("maxBatchMessages", _maxBatchMessages);
AppendInt("pollTimeoutMs", _pollTimeoutMs);
AppendInt("batchingIntervalMs", _batchingIntervalMs);
AppendBool("disableReplyTo", _disableReplyTo);
AppendInt("backoutThreshold", _backoutThreshold);
AppendInt("backoutDelay", _backoutDelay);
AppendIf("backoutQueue", _backoutQueue);
AppendIf("selector", _selector);
AppendBoolExplicit("convert", _convert);
// Producer
if (_persistence.HasValue)
Append("persistence", _persistence.Value.ToString());
if (_persistenceExpr is not null)
Append("persistenceExpression", _persistenceExpr);
AppendIntOrExpression("priority", _priority);
AppendIntOrExpression("expiry", _expiry);
if (_targetClient.HasValue)
Append("targetClient", _targetClient.Value.ToString());
if (_messageType.HasValue)
Append("messageType", _messageType.Value.ToString());
if (_messageTypeExpr is not null)
Append("messageTypeExpression", _messageTypeExpr);
// Transactions
if (_transacted is { } transacted) Append("transacted", transacted ? "true" : "false");
// Dead Letter
// RPC
AppendBool("replyTo", _replyTo);
AppendBool("preserveMessageQos", _preserveMessageQos);
AppendIf("replyToQueue", _replyToQueue);
AppendIf("replyToQueueManager", _replyToQueueManager);
AppendIf("temporaryModel", _temporaryModel);
AppendInt("timeout", _timeout);
if (_correlationPattern.HasValue)
Append("correlationPattern", _correlationPattern.Value.ToString());
// SSL
AppendIf("sslCipherSpec", _sslCipherSpec);
AppendIf("sslCertLabel", _sslCertLabel);
AppendIf("sslPeerName", _sslPeerName);
AppendIf("sslKeyRepository", _sslKeyRepository);
AppendInt("sslKeyResetCount", _sslKeyResetCount);
// Advanced
AppendInt("cCSID", _ccsid);
AppendBoolExplicit("mqmdWriteEnabled", _mqmdWriteEnabled);
AppendBoolExplicit("mqmdReadEnabled", _mqmdReadEnabled);
return sb.ToString();
}
/// Implicit conversion to URI string.
public static implicit operator string(IbmMqBuilder b) => b.Build();
///
public override string ToString() => Build();
}