Path: redb-route/redb.Route.IbmMq/IbmMqEndpoint.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/IbmMqEndpoint.cs
----
using IBM.WMQ;
using Microsoft.Extensions.Logging;
using redb.Route.Abstractions;
using redb.Route.Core;
namespace redb.Route.IbmMq;
///
/// IBM MQ endpoint. Delegates connection ownership to via a
/// process-wide pool keyed by connection identity.
///
/// The endpoint owns only its consumer/producer handles (MQQueue / MQTopic). Pooled queue
/// managers survive Stop/Start cycles and are released only when the parent
/// is disposed.
///
///
public sealed class IbmMqEndpoint : EndpointBase
{
private readonly IbmMqComponent _component;
private bool _closed;
/// Logger from the parent component.
internal ILogger? Logger { get; }
/// Destination name (queue or topic) extracted from the URI path.
public string Destination { get; }
/// Creates an IBM MQ endpoint.
public IbmMqEndpoint(EndpointUri uri, IbmMqComponent component, IbmMqEndpointOptions options)
: base(uri, component, options)
{
_component = component;
Logger = component.Logger;
Destination = uri.Path;
_headerFilter = new EndpointHeaderFilter(this, options.HeaderFilterStrategy);
Connection = component.ResolveConnection(options);
XmsConnectionSource = Connection.CreateXmsConnection;
}
///
/// The queue manager this endpoint connects to: the factory connectionFactory=name names in the context
/// registry, or the inline options when there is none.
///
internal IbmMqConnectionFactory Connection { get; }
///
/// Opens the XMS connection of the event-driven consumer: of
/// . A test sets a fake here to drive a start that fails part-way without a broker.
///
internal Func XmsConnectionSource { get; set; }
private readonly EndpointHeaderFilter _headerFilter;
///
/// The header filter strategy of this endpoint: the one headerFilterStrategy=#name names, or
/// . A name the registry does not hold fails when the producer or
/// consumer is built.
///
internal IHeaderFilterStrategy HeaderFilterStrategy => _headerFilter.Or(IbmMqHeaderFilterStrategy.Default);
/// Typed access to options.
internal new IbmMqEndpointOptions Options => base.Options;
/// Typed options exposed for the component pool.
internal IbmMqEndpointOptions EndpointOptions => Options;
///
public override IProducer CreateProducer() => new IbmMqProducer(this, Options);
///
public override IConsumer CreateConsumer(IProcessor processor) => new IbmMqConsumer(this, processor, Options);
///
/// Returns the shared instance from the component pool,
/// connecting on first use.
///
internal Task GetQueueManagerAsync(CancellationToken ct = default)
=> _component.GetOrCreateQueueManagerAsync(this, ct);
///
/// Creates a fresh dedicated owned by the caller.
/// Producers and consumers use this instead of the shared pool to avoid MQI-call
/// serialisation deadlocks (MQGET WAIT on the shared connection would otherwise
/// block concurrent MQPUTs / MQOPENs).
///
internal Task CreateDedicatedQueueManagerAsync(string ownerTag, CancellationToken ct = default)
=> _component.CreateDedicatedQueueManagerAsync(this, ownerTag, ct);
///
/// Opens an MQ queue with the specified open options.
///
internal async Task OpenQueueAsync(int openOptions, CancellationToken ct = default)
{
var qm = await GetQueueManagerAsync(ct).ConfigureAwait(false);
return qm.AccessQueue(Destination, openOptions);
}
///
/// Opens an MQ topic for subscribing with the specified subscribe options.
///
internal async Task OpenTopicAsync(string topicString, int subscribeOptions, CancellationToken ct = default)
{
var qm = await GetQueueManagerAsync(ct).ConfigureAwait(false);
var subName = $"REDB.{Destination}.{Guid.NewGuid():N}";
return qm.AccessTopic(topicString, null, MQC.MQSO_CREATE | subscribeOptions, null, subName);
}
///
/// Opens an MQ topic for publishing with the specified open options.
///
internal async Task OpenTopicForPublishAsync(string topicString, int openOptions, CancellationToken ct = default)
{
var qm = await GetQueueManagerAsync(ct).ConfigureAwait(false);
return qm.AccessTopic(topicString, null, MQC.MQTOPIC_OPEN_AS_PUBLICATION, openOptions);
}
///
public override async Task Stop(CancellationToken ct = default)
{
if (_closed) return;
_closed = true;
// The MQQueueManager is owned by IbmMqComponent and is NOT disconnected here \u2014 it
// survives Stop/Start cycles and is released only on RouteContext.DisposeAsync
// (which calls Component.DisposeAsync).
Logger?.LogInformation("IBM MQ endpoint stopped: {Uri}", EndpointUri.ForLog(Uri.ToString(), Logger));
await base.Stop(ct).ConfigureAwait(false);
}
}