Path: redb-route/redb.Route.MqttNet/MqttEndpointOptions.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.MqttNet/MqttEndpointOptions.cs
----
using redb.Route.Core;
namespace redb.Route.MqttNet;
///
/// Endpoint options for the MQTT component. Bound from URI parameters via reflection.
///
public sealed class MqttEndpointOptions : EndpointOptions
{
/// Subscribe or Publish mode.
public MqttMode Mode { get; set; } = MqttMode.Subscribe;
// ── Connection ────────────────────────────────────────────────────
/// Named broker reference (registered via AddBroker()).
public string? Broker { get; set; }
/// Inline broker URL (e.g. tcp://localhost:1883). Alternative to named Broker.
public string? Server { get; set; }
/// Server port. Defaults to 1883 (or 8883 for TLS).
public int Port { get; set; }
/// Username for broker authentication.
public string? Username { get; set; }
/// Password for broker authentication.
[Sensitive]
public string? Password { get; set; }
/// Client ID. Auto-generated if not set.
public string? ClientId { get; set; }
///
/// Named from the route registry. Lets broker credentials
/// live in the registry instead of the endpoint URI, so they never reach logs or dashboards.
///
[RegistryReference(typeof(MqttConnectionFactory))]
public string? ConnectionFactory { get; set; }
/// Enable TLS/SSL.
public bool UseTls { get; set; }
/// Keep-alive interval in seconds. Default 60.
public int KeepAlive { get; set; } = 60;
/// Clean session / clean start. Default true.
public bool CleanSession { get; set; } = true;
///
/// MQTT 5 Session Expiry Interval in seconds (Apache Camel paho-mqtt5 sessionExpiryInterval): how long the
/// broker keeps the session after the connection closes. Default 0: the session ends with the connection, even with
/// false (MQTT 5, 3.1.2.11.2). A kept session needs cleanSession=false and a value
/// above 0; only then does the broker send an unacknowledged message again after a reconnect.
///
public int SessionExpiryInterval { get; set; }
///
/// The longest pause, in milliseconds, before the consumer connects again after a failed message on a kept session
/// (Apache Camel paho-mqtt5 maxReconnectDelay). The pause starts at 1 second and doubles with each failed
/// message in a row, up to this value; a success resets it. Default 128000.
///
public int MaxReconnectDelay { get; set; } = 128_000;
// ── Subscribe (consumer) ──────────────────────────────────────────
/// Quality of Service level (0, 1, 2). Default 0.
public int Qos { get; set; }
/// QoS level as expression (e.g. ${header['qos']}). Resolved at runtime, overrides if set.
public string? QosExpression { get; set; }
/// MQTT 5.0 shared subscription group name. Enables load-balanced consuming.
public string? SharedSubscription { get; set; }
///
/// Number of concurrent processing workers for received messages (default 1 = serial).
/// When > 1, received messages are dispatched to a bounded pool of N workers, so up to N
/// messages are processed in parallel. Each message is acknowledged only AFTER its worker
/// finishes processing (manual ack), preserving at-least-once delivery for QoS 1/2 — unlike a
/// generic .Threads(N) in the route, which would acknowledge on hand-off (before processing).
/// Ordering is not preserved when > 1.
///
// A string so "auto" binds verbatim instead of silently degrading to the int default.
public string? ConcurrentConsumers { get; set; }
/// Resolved consumer parallelism: 1, N, or auto = max(CPU, 2) (see ).
public int ResolvedConcurrentConsumers => ConcurrencyOption.Resolve(ConcurrentConsumers, "concurrentConsumers");
// ── Publish (producer) ────────────────────────────────────────────
/// Retain message on broker. Default false.
public bool Retain { get; set; }
/// Message expiry interval in seconds (MQTT 5.0). 0 = no expiry.
public int MessageExpiryInterval { get; set; }
/// Message expiry as expression (e.g. ${body['ttl']}). Resolved at runtime, overrides if set.
public string? MessageExpiryIntervalExpression { get; set; }
/// Content type for published messages (MQTT 5.0).
public string? ContentType { get; set; }
/// Response topic for request/response pattern (MQTT 5.0).
public string? ResponseTopic { get; set; }
///
public override void Validate()
{
if (Broker is null && Server is null)
throw new ArgumentException("Either Broker or Server is required for MQTT endpoint.");
if (Qos < 0 || Qos > 2)
throw new ArgumentException($"QoS must be 0, 1, or 2. Got: {Qos}");
_ = ConcurrencyOption.Resolve(ConcurrentConsumers, "concurrentConsumers"); // loud on garbage
if (SessionExpiryInterval < 0)
throw new ArgumentException($"MQTT sessionExpiryInterval must be >= 0 seconds. Got: {SessionExpiryInterval}");
if (MaxReconnectDelay < 0)
throw new ArgumentException($"MQTT maxReconnectDelay must be >= 0 milliseconds. Got: {MaxReconnectDelay}");
}
}