有什么方法可以找出 Spring AMQP 中已声明队列的订阅者数量?我找到了一个com.rabbitmq.client.Channel可以做到这一点的类:
int consumerCount = channel.queueDeclare().getConsumerCount();
但是,这声明了一个具有随机名称的新队列,并且由于它没有消费者,因此它返回0.
有没有办法为已经声明的队列做这件事?
有什么方法可以找出 Spring AMQP 中已声明队列的订阅者数量?我找到了一个com.rabbitmq.client.Channel可以做到这一点的类:
int consumerCount = channel.queueDeclare().getConsumerCount();
但是,这声明了一个具有随机名称的新队列,并且由于它没有消费者,因此它返回0.
有没有办法为已经声明的队列做这件事?
您可以使用被动声明。
被动声明只是检查具有提供名称的实体是否存在。如果是,则该操作是空操作。对于队列而言,成功的被动声明将返回与非被动声明相同的信息,即队列中的消费者数量和就绪状态的消息。
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();