示例#1
0
        protected virtual void TryCreateChannel()
        {
            DisposeChannel();

            try
            {
                var channel = ConnectionPool
                              .Get(ConnectionName)
                              .CreateModel();

                channel.ExchangeDeclare(
                    exchange: Exchange.ExchangeName,
                    type: Exchange.Type,
                    durable: Exchange.Durable,
                    autoDelete: Exchange.AutoDelete,
                    arguments: Exchange.Arguments
                    );

                channel.QueueDeclare(
                    queue: Queue.QueueName,
                    durable: Queue.Durable,
                    exclusive: Queue.Exclusive,
                    autoDelete: Queue.AutoDelete,
                    arguments: Queue.Arguments
                    );

                var consumer = new EventingBasicConsumer(channel);
                consumer.Received += async(model, basicDeliverEventArgs) =>
                {
                    await HandleIncomingMessage(channel, basicDeliverEventArgs);
                };

                channel.BasicConsume(
                    queue: Queue.QueueName,
                    autoAck: false,
                    consumer: consumer
                    );

                Channel = channel;
            }
            catch (Exception ex)
            {
                Logger.LogException(ex, LogLevel.Warning);
                AsyncHelper.RunSync(() => ExceptionNotifier.NotifyAsync(ex, logLevel: LogLevel.Warning));
            }
        }
示例#2
0
        protected virtual async Task TryCreateChannelAsync()
        {
            await DisposeChannelAsync();

            try
            {
                Channel = ConnectionPool
                          .Get(ConnectionName)
                          .CreateModel();
                Channel.ExchangeDeclare(
                    exchange: Exchange.ExchangeName,
                    type: Exchange.Type,
                    durable: Exchange.Durable,
                    autoDelete: Exchange.AutoDelete,
                    arguments: Exchange.Arguments
                    );

                Channel.QueueDeclare(
                    queue: Queue.QueueName,
                    durable: Queue.Durable,
                    exclusive: Queue.Exclusive,
                    autoDelete: Queue.AutoDelete,
                    arguments: Queue.Arguments
                    );

                var consumer = new AsyncEventingBasicConsumer(Channel);
                consumer.Received += HandleIncomingMessageAsync;

                Channel.BasicConsume(
                    queue: Queue.QueueName,
                    autoAck: false,
                    consumer: consumer
                    );
            }
            catch (Exception ex)
            {
                Logger.LogException(ex, LogLevel.Warning);
                await ExceptionNotifier.NotifyAsync(ex, logLevel : LogLevel.Warning);
            }
        }
示例#3
0
 protected virtual IModel CreateChannel(string channelName, string connectionName)
 {
     return(ConnectionPool
            .Get(connectionName)
            .CreateModel());
 }
示例#4
0
        protected virtual async Task TryCreateChannelAsync()
        {
            await DisposeChannelAsync();

            try
            {
                Channel = ConnectionPool
                          .Get(ConnectionName)
                          .CreateModel();

                Channel.ExchangeDeclare(
                    exchange: Exchange.ExchangeName,
                    type: Exchange.Type,
                    durable: Exchange.Durable,
                    autoDelete: Exchange.AutoDelete,
                    arguments: Exchange.Arguments
                    );

                if (!Exchange.DeadLetterExchangeName.IsNullOrWhiteSpace() &&
                    !Queue.DeadLetterQueueName.IsNullOrWhiteSpace())
                {
                    Channel.ExchangeDeclare(
                        Exchange.DeadLetterExchangeName,
                        Exchange.Type,
                        Exchange.Durable,
                        Exchange.AutoDelete
                        );

                    Channel.QueueDeclare(
                        Queue.DeadLetterQueueName,
                        Queue.Durable,
                        Queue.Exclusive,
                        Queue.AutoDelete);

                    Queue.Arguments["x-dead-letter-exchange"]    = Exchange.DeadLetterExchangeName;
                    Queue.Arguments["x-dead-letter-routing-key"] = Queue.DeadLetterQueueName;

                    Channel.QueueBind(Queue.DeadLetterQueueName, Exchange.DeadLetterExchangeName, Queue.DeadLetterQueueName);
                }

                var result = Channel.QueueDeclare(
                    queue: Queue.QueueName,
                    durable: Queue.Durable,
                    exclusive: Queue.Exclusive,
                    autoDelete: Queue.AutoDelete,
                    arguments: Queue.Arguments
                    );

                var consumer = new AsyncEventingBasicConsumer(Channel);
                consumer.Received += HandleIncomingMessageAsync;

                Channel.BasicConsume(
                    queue: Queue.QueueName,
                    autoAck: false,
                    consumer: consumer
                    );
            }
            catch (Exception ex)
            {
                if (ex is OperationInterruptedException operationInterruptedException &&
                    operationInterruptedException.ShutdownReason.ReplyCode == 406 &&
                    operationInterruptedException.Message.Contains("arg 'x-dead-letter-exchange'"))
                {
                    Exchange.DeadLetterExchangeName = null;
                    Queue.DeadLetterQueueName       = null;
                    Queue.Arguments.Remove("x-dead-letter-exchange");
                    Queue.Arguments.Remove("x-dead-letter-routing-key");
                    Logger.LogWarning("Unable to bind the dead letter queue to an existing queue. You can delete the queue or add policy. See: https://www.rabbitmq.com/parameters.html");
                }

                Logger.LogException(ex, LogLevel.Warning);
                await ExceptionNotifier.NotifyAsync(ex, logLevel : LogLevel.Warning);
            }
        }