Search code examples
javarabbitmqspring-amqpspring-rabbit

How to get the consumer count of an already declared queue in Spring-AMQP?


Is there any way to find out the subscriber count of an already declared queue in Spring AMQP? I found a com.rabbitmq.client.Channel class using which I am able to do this:

int consumerCount = channel.queueDeclare().getConsumerCount();

However, this declares a new queue, with a random name, and since it has no consumer, it returns 0.

Is there any way to do it for an already declared queue?


Solution

  • You can use passive declaration.

    A passive declare simply checks that the entity with the provided name exists. If it does, the operation is a no-op. For queues successful passive declares will return the same information as non-passive ones, namely the number of consumers and messages in ready state in the queue.

    Queue.DeclareOk response = channel.queueDeclarePassive("queue-name");
    // returns the number of messages in Ready state in the queue
    response.getMessageCount();
    // returns the number of consumers the queue has
    response.getConsumerCount();