Exemple #1
0
        public void Dispose()
        {
            if (_consumerChannel != null)
            {
                _consumerChannel.Dispose();
            }

            _subsManager.Clear();
        }
 public void Dispose()
 {
     _channel?.Dispose();
     _channel = null;
     _connection?.Dispose();
     _connection = null;
 }
Exemple #3
0
        private IModel CreateConsumerChannel()
        {
            if (!_persistentConnection.IsConnected)
            {
                _persistentConnection.TryConnect();
            }

            _logger.LogTrace("Creating RabbitMQ consumer channel");

            var channel = _persistentConnection.CreateModel();

            channel.ExchangeDeclare(exchange: BROKER_NAME,
                                    type: "direct");

            channel.QueueDeclare(queue: _queueName,
                                 durable: true,
                                 exclusive: false,
                                 autoDelete: false,
                                 arguments: null);

            channel.CallbackException += (sender, ea) =>
            {
                _logger.LogWarning(ea.Exception, "Recreating RabbitMQ consumer channel");

                _consumerChannel.Dispose();
                _consumerChannel = CreateConsumerChannel();
                StartBasicConsume();
            };

            return(channel);
        }
        private IModel CreateConsumerChannel()
        {
            if (!_persistentConnection.IsConnected)
            {
                _persistentConnection.TryConnect();
            }

            var channel = _persistentConnection.CreateModel();

            channel.ExchangeDeclare(exchange: BROKER_NAME,
                                    type: "direct");

            _queueName = channel.QueueDeclare().QueueName;

            var consumer = new EventingBasicConsumer(channel);

            consumer.Received += async(model, ea) =>
            {
                var eventName = ea.RoutingKey;
                var message   = Encoding.UTF8.GetString(ea.Body);

                try
                {
                    await ProcessEvent(eventName, message);

                    // to avoid losing messages
                    channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
                }
                catch
                {
                    // try to process the message again.

                    var policy = Policy.Handle <InvalidOperationException>()
                                 .Or <Exception>()
                                 .WaitAndRetryAsync(_retryCount, retryAttempt => TimeSpan.FromSeconds(1),
                                                    (ex, time) => { _logger.LogWarning(ex.ToString()); });

                    await policy.ExecuteAsync(() => ProcessEvent(eventName, message));
                }
            };

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

            channel.CallbackException += (sender, ea) =>
            {
                _consumerChannel.Dispose();
                _consumerChannel = CreateConsumerChannel();
            };

            return(channel);
        }
        private IModel CreateConsumerChannel()
        {
            if (!_persistentConnection.IsConnected)
            {
                _persistentConnection.TryConnect();
            }

            var channel = _persistentConnection.CreateModel();

            InitializeRabbitMqPocMessageHub(channel);
            InitializeInputExchange(channel);
            InitializeOutputExchange(channel);
            InitializeDeadLetterExchange(channel);

            InitializeWorkerQueue(channel);
            InitializeDeadLetterQueue(channel);

            var consumer = new EventingBasicConsumer(channel);

            consumer.Received += async(model, ea) =>
            {
                var eventName = ea.RoutingKey;
                var message   = Encoding.UTF8.GetString(ea.Body);

                try
                {
                    await ProcessEvent(eventName, message, GetMessageRetryCount(ea.BasicProperties.Headers));

                    channel.BasicAck(ea.DeliveryTag, multiple: false);
                }
                catch (Exception e)
                {
                    channel.BasicNack(ea.DeliveryTag, multiple: false, requeue: false);
                }
            };

            channel.BasicQos(0, 25, false);
            channel.BasicConsume(queue: $"{_serviceName}.Queue.{Env}",
                                 autoAck: false,
                                 consumer: consumer);

            channel.CallbackException += (sender, ea) =>
            {
                _consumerChannel.Dispose();
                _consumerChannel = CreateConsumerChannel();
            };

            return(channel);
        }
Exemple #6
0
        private IModel CreateConsumerChannel()
        {
            if (!_persistentConnection.IsConnected)
            {
                _persistentConnection.TryConnect();
            }

            var channel = _persistentConnection.CreateModel();

            channel.ExchangeDeclare(exchange: BROKER_NAME,
                                    type: "direct");

            _queueName = channel.QueueDeclare().QueueName;

            var consumer = new EventingBasicConsumer(channel);

            consumer.Received += async(model, ea) =>
            {
                var eventName = ea.RoutingKey;
                var message   = Encoding.UTF8.GetString(ea.Body);

                await ProcessEvent(eventName, message);
            };

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

            channel.CallbackException += (sender, ea) =>
            {
                _consumerChannel.Dispose();
                _consumerChannel = CreateConsumerChannel();
            };

            return(channel);
        }
Exemple #7
0
        } // MakeNewConnection

        private void DestroyConnection(ref RmqCl.IConnection connection, ref RmqCl.IModel channel)
        {
            // This method disposes the AMQP-connection-related object

            try
            {
                if (channel != null)
                {
                    if (m_messageConsumer != null)
                    {
                        m_messageConsumer.Received -= MessageReceived;
                    }

                    try
                    {
                        channel.ModelShutdown -= ModelShutdown;
                        channel.Close();
                    }
                    catch { }
                    channel.Dispose();
                    channel = null;
                }

                if (connection != null)
                {
                    try
                    {
                        connection.Close();
                    }
                    catch { }
                    connection.Dispose();
                    connection = null;
                }
            }
            catch {}
        } // DestroyConnection
 public void Dispose()
 {
     _consumerChannel?.Dispose();
     _subsManager.Clear();
 }
 public void Dispose()
 {
     _consumerChannel?.Dispose();
 }