private List <string> BindingRoutingKeys(RabbitCommonOptions extendedProperties)
        {
            /*
             * When the delimiter is null, we get a String[1] containing the original.
             */
            var delimeter = extendedProperties.BindingRoutingKeyDelimiter;

            if (delimeter == null)
            {
                if (extendedProperties.BindingRoutingKey == null)
                {
                    return(null);
                }

                return(new List <string>()
                {
                    extendedProperties.BindingRoutingKey.Trim()
                });
            }

            if (extendedProperties.BindingRoutingKey == null)
            {
                return(null);
            }

            var trimmed = extendedProperties.BindingRoutingKey.Split(delimeter, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim());

            return(new List <string>(trimmed));
        }
 private string GetDeadLetterExchangeName(RabbitCommonOptions properties)
 {
     if (properties.DeadLetterExchange == null)
     {
         return(properties.Prefix + RabbitCommonOptions.DEAD_LETTER_EXCHANGE);
     }
     else
     {
         return(properties.DeadLetterExchange);
     }
 }
        private RabbitConfig.IBinding NotPartitionedBinding(
            IExchange exchange,
            Queue queue,
            string rk,
            RabbitCommonOptions extendedProperties)
        {
            var routingKey = rk;

            if (routingKey == null)
            {
                routingKey = "#";
            }

            var arguments = new Dictionary <string, object>();

            foreach (var entry in extendedProperties.QueueBindingArguments)
            {
                arguments.Add(entry.Key, entry.Value);
            }

            switch (exchange)
            {
            case TopicExchange topic:
            {
                var binding = BindingBuilder.Bind(queue).To(topic).With(routingKey);
                DeclareBinding(queue.QueueName, binding);
                return(binding);
            }

            case DirectExchange direct:
            {
                var binding = BindingBuilder.Bind(queue).To(direct).With(routingKey);
                DeclareBinding(queue.QueueName, binding);
                return(binding);
            }

            case FanoutExchange fanout:
            {
                var binding = BindingBuilder.Bind(queue).To(fanout);
                DeclareBinding(queue.QueueName, binding);
                return(binding);
            }

            case HeadersExchange:
            {
                var binding = new RabbitConfig.Binding(queue.QueueName + "." + exchange.ExchangeName + ".binding", queue.QueueName, DestinationType.QUEUE, exchange.ExchangeName, string.Empty, arguments);
                DeclareBinding(queue.QueueName, binding);
                return(binding);
            }

            default:
                throw new ProvisioningException("Cannot bind to a " + exchange.Type + " exchange");
            }
        }
Пример #4
0
        private void DeadLetters(RabbitCommonOptions properties)
        {
            if (properties.DeadLetterExchange != null)
            {
                _exchanges.Add(properties.DeadLetterExchange);
            }

            if (properties.DeadLetterQueueName != null)
            {
                _queues.Add(properties.DeadLetterQueueName);
            }
        }
        private Dictionary <string, object> GetQueueArgs(string queueName, RabbitCommonOptions properties, bool isDlq)
        {
            var args = new Dictionary <string, object>();

            if (!isDlq)
            {
                if (properties.AutoBindDlq.Value)
                {
                    string dlx;
                    if (properties.DeadLetterExchange != null)
                    {
                        dlx = properties.DeadLetterExchange;
                    }
                    else
                    {
                        dlx = ApplyPrefix(properties.Prefix, "DLX");
                    }

                    args.Add("x-dead-letter-exchange", dlx);

                    string dlRk;
                    if (properties.DeadLetterRoutingKey != null)
                    {
                        dlRk = properties.DeadLetterRoutingKey;
                    }
                    else
                    {
                        dlRk = queueName;
                    }

                    args.Add("x-dead-letter-routing-key", dlRk);
                }
            }
            else
            {
                if (properties.DlqDeadLetterExchange != null)
                {
                    args.Add("x-dead-letter-exchange", properties.DlqDeadLetterExchange);
                }

                if (properties.DlqDeadLetterRoutingKey != null)
                {
                    args.Add("x-dead-letter-routing-key", properties.DlqDeadLetterRoutingKey);
                }
            }

            AddAdditionalArgs(args, properties, isDlq);
            return(args);
        }
        private void AutoBindDLQ(string baseQueueName, string routingKey, RabbitCommonOptions properties)
        {
            var autoBindDlq = properties.AutoBindDlq.Value;

            _logger.LogDebug("autoBindDLQ=" + autoBindDlq + " for: " + baseQueueName);
            if (autoBindDlq)
            {
                string dlqName;
                if (properties.DeadLetterQueueName == null)
                {
                    dlqName = ConstructDLQName(baseQueueName);
                }
                else
                {
                    dlqName = properties.DeadLetterQueueName;
                }

                var dlq = new Queue(dlqName, true, false, false, GetQueueArgs(dlqName, properties, true));
                DeclareQueue(dlqName, dlq);
                var dlxName = GetDeadLetterExchangeName(properties);
                if (properties.DeclareDlx.Value)
                {
                    DeclareExchange(dlxName, new ExchangeBuilder(dlxName, properties.DeadLetterExchangeType).Durable(true).Build());
                }

                var arguments = new Dictionary <string, object>();

                properties.DlqBindingArguments?.ToList().
                ForEach(entry => arguments.Add(entry.Key, entry.Value));

                var dlRoutingKey  = properties.DeadLetterRoutingKey ?? routingKey;
                var dlBindingName = dlq.QueueName + "." + dlxName + "." + dlRoutingKey + ".binding";
                var dlqBinding    = new RabbitConfig.Binding(dlBindingName, dlq.QueueName, DestinationType.QUEUE, dlxName, dlRoutingKey, arguments);
                DeclareBinding(dlqName, dlqBinding);
                if (properties is RabbitConsumerOptions options && options.RepublishToDlq.Value)
                {
                    /*
                     * Also bind with the base queue name when republishToDlq is used, which does not know about partitioning
                     */
                    var bindingName = dlq.QueueName + "." + dlxName + "." + baseQueueName + ".binding";
                    DeclareBinding(dlqName, new RabbitConfig.Binding(bindingName, dlq.QueueName, DestinationType.QUEUE, dlxName, baseQueueName, arguments));
                }
            }
        }
        private IExchange BuildExchange(RabbitCommonOptions properties, string exchangeName)
        {
            try
            {
                var builder = new ExchangeBuilder(exchangeName, properties.ExchangeType);
                builder.Durable(properties.ExchangeDurable.GetValueOrDefault());
                if (properties.ExchangeAutoDelete.GetValueOrDefault())
                {
                    builder.AutoDelete();
                }

                if (properties.DelayedExchange.GetValueOrDefault())
                {
                    builder.Delayed();
                }

                return(builder.Build());
            }
            catch (Exception e)
            {
                throw new ProvisioningException("Failed to create exchange object", e);
            }
        }
        private void AddAdditionalArgs(Dictionary <string, object> args, RabbitCommonOptions properties, bool isDlq)
        {
            var expires        = isDlq ? properties.DlqExpires : properties.Expires;
            var maxLength      = isDlq ? properties.DlqMaxLength : properties.MaxLength;
            var maxLengthBytes = isDlq ? properties.DlqMaxLengthBytes : properties.MaxLengthBytes;
            var maxPriority    = isDlq ? properties.DlqMaxPriority : properties.MaxPriority;
            var ttl            = isDlq ? properties.DlqTtl : properties.Ttl;
            var lazy           = isDlq ? properties.DlqLazy : properties.Lazy;
            var overflow       = isDlq ? properties.DlqOverflowBehavior : properties.OverflowBehavior;
            var quorum         = isDlq ? properties.DlqQuorum : properties.Quorum;
            var singleActive   = isDlq ? properties.DlqSingleActiveConsumer : properties.SingleActiveConsumer;

            if (expires != null)
            {
                args.Add("x-expires", expires.Value);
            }

            if (maxLength != null)
            {
                args.Add("x-max-length", maxLength.Value);
            }

            if (maxLengthBytes != null)
            {
                args.Add("x-max-length-bytes", maxLengthBytes.Value);
            }

            if (maxPriority != null)
            {
                args.Add("x-max-priority", maxPriority.Value);
            }

            if (ttl != null)
            {
                args.Add("x-message-ttl", ttl.Value);
            }

            if (lazy.GetValueOrDefault())
            {
                args.Add("x-queue-mode", "lazy");
            }

            if (!string.IsNullOrEmpty(overflow))
            {
                args.Add("x-overflow", overflow);
            }

            if (quorum != null && quorum.Enabled.Value)
            {
                args.Add("x-queue-type", "quorum");
                if (quorum.DeliveryLimit != null)
                {
                    args.Add("x-delivery-limit", quorum.DeliveryLimit.Value);
                }

                if (quorum.InitialQuorumSize != null)
                {
                    args.Add("x-quorum-initial-group-size", quorum.InitialQuorumSize.Value);
                }
            }

            if (singleActive.GetValueOrDefault())
            {
                args.Add("x-single-active-consumer", true);
            }
        }