Skip to content

获取 RabbitMQ 的消息数量

🏷️ RabbitMQ

这篇博客的基础上了做了少许修改。具体代码如下:

java
private final RabbitTemplate rabbitTemplate;

try (Connection connection = rabbitTemplate.getConnectionFactory().createConnection();
     Channel channel = connection.createChannel(false);) {
    // 设置消息交换机
    channel.exchangeDeclare("amp.topic", "topic", true, false, null);
    AMQP.Queue.DeclareOk declareOk = channel.queueDeclarePassive(QUEUE_NAME);
    //获取队列中的消息个数
    int queueCount = declareOk.getMessageCount();
    return queueCount;
}