Yasiru Nilan
Yasiru Nilan

Reputation: 457

RabbitMQ Single Consumer, consuming messages from Multiple Queues

In my application I have a system which publishes messages to multiple queues. As an example I have 3 RabbitMQ queues and messages get published to those 3 queues. Currently I have 3 consumers for these 3 queues. Now I need to do a modification and I want to have a single consumer for two of the queues. Can we use one subscriber for several queues. How can this be done in RabbitMQ?

Upvotes: 3

Views: 2579

Answers (2)

RidgeA
RidgeA

Reputation: 1546

AFAIK it is impossible to use the same channel to consume several queues simultaneously. At least when I tried to do it did not work for me. Also it could be not safe to use the same channel across different threads.

It is better to create a separate channel to consume from each queue.

Channel it is enough lightweight entity - it is use the same tcp connection. The main purpose of channel - to multiplex different queries through the same tcp connection.

Upvotes: 0

Arpan Gupta
Arpan Gupta

Reputation: 98

One way this can be done is by using channel.basicConsume(QUEUE_NAME,,); (In case of Java, similar cases for other languages too) and specifying the queue from which you want to consume. So, in this case you can use two such commands specifying the 2 or more queues you want to consume from.

In case of php, the code would look something like this:

$channel->basic_consume('QUEUE_NAME', '', false, true, false, false, $callback);

Just call it for as many queues you want to consume from.

Upvotes: 0

Related Questions