[rabbitmq-discuss] How check for existing queue consumer

Simon MacMullen simon at rabbitmq.com
Wed Feb 12 15:03:54 GMT 2014


On 12/02/14 14:59, cw storm wrote:
> I have a situation where someone executed the service that calls the
> "basicConsume" which basically assigns a queue consumer to that queue on
> a different channel.  This cause multiple queue consumer for that queue.
>   Is there someway in that service where I can check to see if an
> existing queue consumer already exist for that queue before issue'ing
> the "basicConsume"?

You can invoke queue.declare. The queue.declare-ok that comes back will 
include a count of the messages in the queue, and the number of 
consumers. e.g. in Java:

   Channel channel = ...;
   int consumers = 
channel.queueDeclarePassive("my-queue").getConsumerCount();

Note that RabbitMQ before 3.1.0 returned the number of "active" 
consumers here, which is a rather difficult concept.

HOWEVER:

This approach is racy. If what you really want is to ensure that a queue 
only ever has at most one consumer, the best thing to do is set the 
"exclusive" flag when invoking basic.consume:

   Channel channel = ...;
   channel.basicConsume("my-queue", autoAck, null, false, true, null, 
consumer);

Cheers, Simon

-- 
Simon MacMullen
RabbitMQ, Pivotal


More information about the rabbitmq-discuss mailing list