/// <summary> /// Internal constructor. /// </summary> /// <param name="hiveBus">The <see cref="HiveBus"/>.</param> /// <param name="name">The channel name (maximum of 250 characters).</param> /// <param name="durable"> /// Optionally specifies that the channel should survive message cluster restarts. /// This defaults to <c>false</c>. /// </param> /// <param name="exclusive"> /// Optionally specifies that this channel instance will exclusively receive /// messages from the queue. This defaults to <c>false</c>. /// </param> /// <param name="autoDelete"> /// Optionally specifies that the channel should be deleted once all consumers have /// disconnected. This defaults to <c>false</c>. /// </param> /// <param name="messageTTL"> /// <para> /// Optionally specifies the maximum time a message can remain in the channel before /// being deleted. This defaults to <c>null</c> which disables this feature. /// </para> /// <note> /// The maximum possible TTL is about <b>24.855 days</b>. /// </note> /// </param> /// <param name="maxLength"> /// Optionally specifies the maximum number of messages that can be waiting in the channel /// before messages at the front of the channel will be deleted. This defaults /// to unconstrained. /// </param> /// <param name="maxLengthBytes"> /// Optionally specifies the maximum total bytes of messages that can be waiting in /// the channel before messages at the front of the channel will be deleted. This /// defaults to unconstrained. /// </param> internal BasicChannel( HiveBus hiveBus, string name, bool durable = false, bool exclusive = false, bool autoDelete = false, TimeSpan?messageTTL = null, int?maxLength = null, int?maxLengthBytes = null) : base(hiveBus, name) { Covenant.Requires <ArgumentNullException>(hiveBus != null); Covenant.Requires <ArgumentException>(maxLength == null || maxLength.Value > 0); Covenant.Requires <ArgumentException>(maxLengthBytes == null || maxLengthBytes.Value > 0); queue = EasyBus.QueueDeclare( name: name, passive: false, durable: durable, exclusive: exclusive, autoDelete: autoDelete, perQueueMessageTtl: HiveBus.TTLToMilliseconds(messageTTL), maxLength: maxLength, maxLengthBytes: maxLengthBytes); // We're going to use the default exchange which automatically // routes messages to the queue by name. exchange = Exchange.GetDefault(); }
/// <summary> /// Begins consuming messages from a queue. /// </summary> /// <param name="queue">The source queue.</param> protected void StartListening(IQueue queue) { Covenant.Requires <ArgumentNullException>(queue != null); subscription = EasyBus.Consume(queue, async(byte[] bytes, MessageProperties properties, MessageReceivedInfo info) => { await DispatchAsync(bytes, properties, info); }); }
/// <summary> /// Internal constructor. /// </summary> /// <param name="hiveBus">The <see cref="HiveBus"/>.</param> /// <param name="name">The channel name.</param> /// <param name="durable"> /// Optionally specifies that the channel should survive message cluster restarts. /// This defaults to <c>false</c>. /// </param> /// <param name="autoDelete"> /// Optionally specifies that channel should be automatically deleted when the /// last consumer is removed. /// </param> /// <param name="messageTTL"> /// <para> /// Optionally specifies the maximum time a message can remain in the channel before /// being deleted. This defaults to <c>null</c> which disables this feature. /// </para> /// <note> /// The maximum possible TTL is about <b>24.855 days</b>. /// </note> /// </param> /// <param name="maxLength"> /// Optionally specifies the maximum number of messages that can be waiting in the channel /// before messages at the front of the channel will be deleted. This defaults /// to unconstrained. /// </param> /// <param name="maxLengthBytes"> /// Optionally specifies the maximum total bytes of messages that can be waiting in /// the channel before messages at the front of the channel will be deleted. This /// defaults to unconstrained. /// </param> /// <param name="publishOnly"> /// Optionally specifies that the channel instance returned will only be able /// to publish messages and not consume them. Enabling this avoid the creation /// of a queue that will unnecessary for this situation. /// </param> internal BroadcastChannel( HiveBus hiveBus, string name, bool durable = false, bool autoDelete = false, TimeSpan?messageTTL = null, int?maxLength = null, int?maxLengthBytes = null, bool publishOnly = false) : base(hiveBus, name) { Covenant.Requires <ArgumentNullException>(hiveBus != null); Covenant.Requires <ArgumentException>(maxLength == null || maxLength.Value > 0); Covenant.Requires <ArgumentException>(maxLengthBytes == null || maxLengthBytes.Value > 0); this.sourceID = Guid.NewGuid().ToString("D").ToLowerInvariant(); this.publishOnly = publishOnly; exchange = EasyBus.ExchangeDeclare( name: name, type: EasyNetQ.Topology.ExchangeType.Fanout, passive: false, durable: durable, autoDelete: autoDelete, alternateExchange: null, delayed: false); queue = EasyBus.QueueDeclare( name: $"{name}-{sourceID}", passive: false, durable: durable, exclusive: true, autoDelete: true, perQueueMessageTtl: HiveBus.TTLToMilliseconds(messageTTL), maxLength: maxLength, maxLengthBytes: maxLengthBytes); EasyBus.Bind(exchange, queue, routingKey: "#"); }
/// <summary> /// Releases all associated resources. /// </summary> /// <param name="disposing">Pass <c>true</c> if we're disposing, <c>false</c> if we're finalizing.</param> protected virtual void Dispose(bool disposing) { lock (syncLock) { if (!isDisposed) { if (disposing) { try { disposingNow = true; foreach (var channel in channels) { try { channel.Dispose(); } catch { // Intentionally ignoring these. } } channels.Clear(); channels = null; EasyBus.Dispose(); EasyBus = null; } finally { disposingNow = false; } } isDisposed = true; } } }
/// <summary> /// Asynchronously publishes a message to an exchange. /// </summary> /// <typeparam name="TMessage">The message type.</typeparam> /// <param name="exchange">The target exchane.</param> /// <param name="message">The message.</param> /// <param name="routingKey">The routing key.</param> /// <param name="headers">Optional message headers.</param> protected async Task PublishAsync <TMessage>(IExchange exchange, TMessage message, string routingKey, params KeyValuePair <string, string>[] headers) where TMessage : class, new() { Covenant.Requires <ArgumentNullException>(exchange != null); Covenant.Requires <ArgumentNullException>(message != null); var body = Serialize(message, Encoding.UTF8); var properties = new MessageProperties() { Type = typeof(TMessage).FullName, ContentType = "application/json", ContentEncoding = "utf-8" }; if (headers != null && headers.Length > 0) { foreach (var header in headers) { properties.Headers.Add(header.Key, header.Value); } } await EasyBus.PublishAsync(exchange, routingKey, mandatory : false, properties, body); }