Exemple #1
0
 /// <summary>
 /// Adds exchange log information to the dictionary of values.
 /// </summary>
 /// <param name="log">Dictionary containing log values.</param>
 public void LogProperties(IDictionary <string, object> log)
 {
     log["Exchange"] = IsDefaultExchange ? "Default-Exchange" : GetLogDetails();
     if (QueueMeta != null)
     {
         log["Queue"] = QueueMeta.GetLogDetails();
     }
 }
        /// <summary>
        /// Creates a queue and its associated exchange on the message broker.  If the associated
        /// exchange is the RabbitMQ default exchange, only the queue is created.  For a non-default
        /// exchange, the queue is bound to the exchange.  If the metadata has route-keys specified,
        /// the queue is bound the to exchange for each specified key.
        /// </summary>
        /// <param name="bus">Reference to the advanced bus.</param>
        /// <param name="meta">The metadata describing the queue.</param>
        /// <returns>Reference to the created queue.</returns>
        public static async Task <IQueue> QueueDeclare(this IAdvancedBus bus, QueueMeta meta)
        {
            if (bus == null)
            {
                throw new ArgumentNullException(nameof(bus));
            }
            if (meta == null)
            {
                throw new ArgumentNullException(nameof(meta));
            }

            IExchange    exchange     = Exchange.GetDefault(); //  Assume default exchange.
            ExchangeMeta exchangeMeta = meta.Exchange;

            if (!meta.Exchange.IsDefaultExchange)
            {
                exchange = await bus.ExchangeDeclareAsync(exchangeMeta.ExchangeName, exchangeMeta.ExchangeType,
                                                          durable : exchangeMeta.IsDurable,
                                                          autoDelete : exchangeMeta.IsAutoDelete,
                                                          passive : exchangeMeta.IsPassive,
                                                          alternateExchange : exchangeMeta.AlternateExchangeName);
            }

            IQueue queue = await bus.QueueDeclareAsync(meta.ScopedQueueName,
                                                       durable : meta.IsDurable,
                                                       autoDelete : meta.IsAutoDelete,
                                                       exclusive : meta.IsExclusive,
                                                       passive : meta.IsPassive,
                                                       maxPriority : meta.MaxPriority,
                                                       deadLetterExchange : meta.DeadLetterExchange,
                                                       deadLetterRoutingKey : meta.DeadLetterRoutingKey,
                                                       perQueueMessageTtl : meta.PerQueueMessageTtl);


            // Queues defined on the default exchange so don't bind.
            if (exchangeMeta.IsDefaultExchange)
            {
                return(queue);
            }

            string[] routeKeys = meta.RouteKeys ?? new string[] { };
            if (routeKeys.Length > 0)
            {
                // If route-keys specified, bind the queue to the exchange
                // for each route-key.
                foreach (string routeKey in meta.RouteKeys ?? new string[] {})
                {
                    bus.Bind(exchange, queue, routeKey);
                }
            }
            else
            {
                await bus.BindAsync(exchange, queue, string.Empty);
            }

            return(queue);
        }
Exemple #3
0
        /// <summary>
        /// Defines a queue on an exchange.
        /// </summary>
        /// <param name="queueName">The name of the queue.</param>
        /// <param name="exchange">Reference to the exchange on which it should be created.</param>
        /// <param name="config">Delegate used to specify additional queue metadata.</param>
        /// <returns>The queue metadata.</returns>
        public static QueueMeta Define(string queueName, ExchangeMeta exchange, Action <QueueMeta> config = null)
        {
            if (string.IsNullOrWhiteSpace(queueName))
            {
                throw new ArgumentException("Queue name not specified.", nameof(queueName));
            }
            if (exchange == null)
            {
                throw new ArgumentNullException(nameof(exchange));
            }

            var queue = new QueueMeta
            {
                QueueName       = queueName,
                ScopedQueueName = queueName,
                Exchange        = exchange
            };

            config?.Invoke(queue);
            return(queue);
        }
Exemple #4
0
        /// <summary>
        /// Defines a queue on the default exchange.
        ///
        /// https://www.rabbitmq.com/tutorials/amqp-concepts.html#exchange-default
        /// </summary>
        /// <typeparam name="TMessage">Type of message associated with exchange.</typeparam>
        /// <param name="busName">The key specified within the application's settings
        /// specifying the broker connection.</param>
        /// <param name="queueName">The name of the queue to be created.</param>
        /// <param name="config">Delegate used to specify the queue metadata.</param>
        /// <returns>The exchange metadata.</returns>
        public static ExchangeMeta <TMessage> DefineDefault <TMessage>(string busName, string queueName,
                                                                       Action <QueueMeta> config = null)
            where TMessage : IMessage
        {
            if (string.IsNullOrWhiteSpace(busName))
            {
                throw new ArgumentException("Bus name not specified.", nameof(busName));
            }
            if (string.IsNullOrWhiteSpace(queueName))
            {
                throw new ArgumentException("Queue name not specified.", nameof(queueName));
            }

            var exchange = new ExchangeMeta <TMessage> {
                BusName = busName
            };

            exchange.QueueMeta = QueueMeta.Define(queueName, exchange);

            config?.Invoke(exchange.QueueMeta);
            return(exchange);
        }