如何获取 Spring-AMQP 中已声明队列的消费者数量?

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

有什么方法可以在 Spring AMQP 中找出已声明队列的订阅者数量?我找到了一个 com.rabbitmq.client.Channel class,我可以使用它来做到这一点:

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

然而,这声明了一个新队列,具有随机名称,并且由于它没有消费者,它 returns 0.

对于已经声明的队列,有什么方法可以做到吗?

您可以使用 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();