示例#1
0
        //------------------------------------------BROKER RECONNECTION------------------------------------------//

        // Called when a channel is notified of an connection issue.  Once the connection is
        // reestablished, the centrally saved exchanges and queues need to be recreated on the
        // new connection.  This is important when using a broker fail-over behind a load-balancer
        // since the backup broker my not have the exchanges.
        private void RestablishConnection(MessageConsumer messageConsumer, ShutdownEventArgs shutdownEvent)
        {
            if (_connMgr.IsBrokerConnected(messageConsumer.BrokerName))
            {
                return;
            }

            if (_connMgr.IsUnexpectedShutdown(shutdownEvent))
            {
                _logger.LogError(RabbitMqLogEvents.BROKER_CONFIGURATION, "Connection to broker was shutdown.  Reconnection will be attempted.");

                string brokerName = messageConsumer.BrokerName;
                IEnumerable <BrokerMeta> brokerMeta = _exchangeRep.LoadAsync(brokerName).Result;

                _connMgr.ReconnectToBroker(brokerName);

                // Restore the exchanges and queues on what might be a new broker.
                using (IModel channel = _connMgr.CreateChannel(brokerName))
                {
                    ReCreatePublisherExchanges(channel, brokerMeta);
                    ReCreateConsumerQueues(channel, brokerMeta);

                    _exchangeConsumer.BindConsumersToQueues(_messageConsumers, messageConsumer.BrokerName);
                    _rpcExchangeConsumer.BindConsumersToRpcQueues();
                }

                // Watch for future issues.
                AttachBokerMonitoringHandlers();

                _logger.LogDebug(RabbitMqLogEvents.BROKER_CONFIGURATION, "Connection to broker was reestablished.");
            }
        }
        public static EventingBasicConsumer GetBasicConsumer(this IModel channel,
                                                             MessageConsumer eventConsumer)
        {
            Check.NotNull(channel, nameof(channel));
            Check.NotNull(eventConsumer, nameof(eventConsumer));

            var basicConsumer = new EventingBasicConsumer(channel);

            channel.BasicConsume(eventConsumer.QueueName, eventConsumer.QueueSettings, basicConsumer);
            return(basicConsumer);
        }
        public static void QueueDeclare(this IModel channel,
                                        MessageConsumer eventConsumer)
        {
            Check.NotNull(channel, nameof(channel));
            Check.NotNull(eventConsumer, nameof(eventConsumer));

            if (eventConsumer.IsBrokerAssignedName)
            {
                eventConsumer.QueueName = channel.QueueDeclare().QueueName;
                return;
            }

            channel.QueueDeclare(eventConsumer.QueueName,
                                 eventConsumer.QueueSettings.IsDurable,
                                 eventConsumer.QueueSettings.IsExclusive,
                                 eventConsumer.QueueSettings.IsAutoDelete, null);
        }
        public static void QueueBind(this IModel channel, MessageConsumer eventConsumer)
        {
            Check.NotNull(channel, nameof(channel));
            Check.NotNull(eventConsumer, nameof(eventConsumer));

            if (eventConsumer.RouteKeys.Empty())
            {
                channel.QueueBind(eventConsumer.QueueName, eventConsumer.ExchangeName, "");
                return;
            }

            foreach (string routeKey in eventConsumer.RouteKeys)
            {
                if (routeKey.IsNullOrWhiteSpace())
                {
                    throw new InvalidOperationException(
                              $"The queue named: {eventConsumer.QueueName} on the exchange named: {eventConsumer.ExchangeName} " +
                              $"has a null route-key value specified.");
                }

                channel.QueueBind(eventConsumer.QueueName, eventConsumer.ExchangeName, routeKey.ToUpper());
            }
        }