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?
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.
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.
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.
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.
"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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With