Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Consumer "received" event not firing

Tags:

c#

rabbitmq

I'm trying to set up a subscription to a RabbitMQ queue and pass it a custom event handler. So I have a class called RabbitMQClient which contains the following method:

public void Subscribe(string queueName, EventHandler<BasicDeliverEventArgs> receivedHandler)
{
    using (var connection = factory.CreateConnection())
    {
        using (var channel = connection.CreateModel())
        {
            channel.QueueDeclare(
                queue: queueName,
                durable: false,
                exclusive: false,
                autoDelete: false,
                arguments: null
            );

            var consumer = new EventingBasicConsumer(channel);

            consumer.Received += receivedHandler;

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

I'm using dependency injection, so I have a RabbitMQClient (singleton) interface for it.

In my consuming class, I have this method which I want to act as the EventHandler

public void Consumer_Received(object sender, BasicDeliverEventArgs e)
{
    var message = e.Body.FromByteArray<ProgressQueueMessage>();
}

And I'm trying to subscribe to the queue like this:

rabbitMQClient.Subscribe(Consts.RabbitMQ.ProgressQueue, Consumer_Received);

I can see that the queue starts to get messages, but the Consumer_Received method is not firing.

What am I missing here?

like image 535
Liran Friedman Avatar asked Sep 04 '18 06:09

Liran Friedman


People also ask

How do you consume multiple messages in RabbitMQ?

To increase the performance and to consume more messages at a time, do as follows: Open the "RabbitMQ connection" and go to the Event sources tab. In Advanced Settings > "Other Attributes:", add “concurrentConsumers” property. For instance: concurrentConsumers=10.

How consumer Works in RabbitMQ?

It accepts messages from publishers, routes them and, if there were queues to route to, stores them for consumption or immediately delivers to consumers, if any. Consumers consume from queues. In order to consume messages there has to be a queue.

Can a queue have multiple consumers in RabbitMQ?

No it's not, single queue/multiple consumers with each consumer handling the same message ID isn't possible. Having the exchange route the message onto into two separate queues is indeed better.

How do I close a consumer in RabbitMQ?

You can kill connections to the RabbitMQ broker using the rabbitmqctl tool (see the man page) or by using the Web UI. You could also purge and delete the queue which belonged to the rogue consumer. However, you can't kill the consumer process itself using those tools.


1 Answers

"Using" calls dispose on your connection and your event wont be triggered. Just remove your "using" block from the code so that it doesn't close the connection.

var connection = factory.CreateConnection();

var channel = connection.CreateModel();

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

var consumer = new EventingBasicConsumer(channel);

consumer.Received += receivedHandler;

channel.BasicConsume(
    queue: queueName,
    autoAck: false,
    consumer: consumer);
like image 175
vhr Avatar answered Nov 03 '22 21:11

vhr