Esempio n. 1
0
        private (string Exchange, string RoutingKey) PublishHelper(string eventType, bool publishAsError, Dictionary <string, object> parameters)
        {
            string exchange   = _options.BrokerName;
            string routingKey = eventType;

            if (parameters == null)
            {
                return(exchange, routingKey);
            }

            if (parameters.ContainsKey(ExchangeWord))
            {
                exchange = parameters[ExchangeWord].ToString();
            }

            if (parameters.ContainsKey(RoutingKeyWord))
            {
                routingKey = parameters[RoutingKeyWord].ToString();
            }

            if (!publishAsError)
            {
                return(exchange, routingKey);
            }

            List <string> queueNamesFromBindings = _options.Bindings.Where(b => b.Source.Equals(exchange, StringComparison.CurrentCultureIgnoreCase) && b.RoutingKey.Equals(routingKey, StringComparison.CurrentCultureIgnoreCase)).Select(b => b.Destination).ToList();

            if (!queueNamesFromBindings.Any())
            {
                _logger.LogWarning("There should be at least one binding with given exchange and routing key to identify DLX and DLK.");
                return(exchange, routingKey);
            }

            if (queueNamesFromBindings.Count() > 1)
            {
                // TODO implement a way to handle multiple bindings, for now it will pick the first one
                _logger.LogWarning("There are more than one queue identified with this exchange and routingKey.  Will use first one");
            }

            Queue queue = _options.Queues.FirstOrDefault(q => queueNamesFromBindings.Contains(q.Name) &&
                                                         DeadLetterHelper.GetValues(q).DeadLetterExists);

            // ReSharper disable once InvertIf
            if (queue == null)
            {
                _logger.LogWarning("Identified queue {QueueNamesFromBindings} doesn't have DLX or DLK, can't identify any error routingKey", queueNamesFromBindings);
                return(exchange, routingKey);
            }

            (string dlx, string dlk, string dlq, bool dlExists) = DeadLetterHelper.GetValues(queue);
            // ReSharper disable once InvertIf
            if (dlExists)
            {
                exchange   = dlx;
                routingKey = dlk;
            }
            return(exchange, routingKey);
        }
Esempio n. 2
0
        public void EnsureDeclarations()
        {
            if (!_options.EnableDeclarations)
            {
                return;
            }

            if (!IsConnected)
            {
                TryConnect();
            }
            using (IModel channel = CreateModel())
            {
                foreach (Exchange exchange in _options.Exchanges)
                {
                    _logger.LogDebug("Creating exchange {Exchange}"
                                     , exchange.Name);
                    channel.ExchangeDeclare(exchange.Name
                                            , exchange.Type
                                            , exchange.Durable
                                            , exchange.AutoDelete
                                            , exchange.Arguments);
                }

                foreach (Queue queue in _options.Queues)
                {
                    (string dlx, string dlk, string dlq, bool dlExists) = DeadLetterHelper.GetValues(queue);
                    if (dlExists)
                    {
                        _logger.LogDebug("Declaring dead letter objects {Exchange} {RoutingKey} {Queue}"
                                         , dlx, dlk, dlq);

                        channel.ExchangeDeclare(dlx
                                                , DeadLetterHelper.DeadLetterExchangeType
                                                , queue.Durable
                                                , queue.AutoDelete
                                                , null);

                        channel.QueueDeclare(queue: dlq
                                             , durable: true
                                             , exclusive: false
                                             , autoDelete: false);

                        channel.QueueBind(queue: dlq
                                          , exchange: dlx
                                          , routingKey: dlk);
                    }

                    _logger.LogDebug("Creating queue {Queue}"
                                     , queue.Name);
                    channel.QueueDeclare(queue: queue.Name
                                         , durable: queue.Durable
                                         , exclusive: queue.Exclusive
                                         , autoDelete: queue.AutoDelete
                                         , arguments: queue.Arguments);
                }

                foreach (Binding binding in _options.Bindings)
                {
                    channel.QueueBind(queue: binding.Destination
                                      , exchange: binding.Source
                                      , routingKey: binding.RoutingKey);
                }
            }
        }