从java代码获取rabbitmq队列中的消息计数

use*_*719 2 java message-queue rabbitmq

我试图检查给定的rabbitmq队列是否为空.为此,我试图使用:

channel.queueDeclarePassive(queueName).getMessageCount().
Run Code Online (Sandbox Code Playgroud)

使用这个我总是得到0作为答案,不管显示的消息数量rabbitmqctl list_queues.据我所搜索,没有可用的API.

我想要给出相同的答案rabbitmqctl list_queues.请建议一种方法来做到这一点.

Jun*_*ius 5

您无法使用SDK获取邮件数量.channel.queueDeclarePassive(queueName).getMessageCount()通常不正确,因为它不会计算等待确认的消息.

您可以启用管理插件并通过REST API查询队列:

http://localhost:15672/api/queues/vhost/queue_name 
Run Code Online (Sandbox Code Playgroud)

响应包含总消息数以及处理/就绪状态中的消息.访问"localhost:15672/api"以查看有关如何调用它的更多详细信息.

以下是本地服务器上的示例响应:

{
    "memory":14680,
    "message_stats":{
        "publish":1,
        "publish_details":{
            "rate":0
        }
    },
    "messages":1,
    "messages_details":{
        "rate":0
    },
    "messages_ready":1,
    "messages_ready_details":{
        "rate":0
    },
    "messages_unacknowledged":0,
    "messages_unacknowledged_details":{
        "rate":0
    },
    "idle_since":"2014-02-21 18:01:54",
    "policy":"",
    "exclusive_consumer_tag":"",
    "consumers":0,
    "backing_queue_status":{
        "q1":0,
        "q2":0,
        "delta":[
            "delta",
            0,
            0,
            0
        ],
        "q3":0,
        "q4":1,
        "len":1,
        "pending_acks":0,
        "target_ram_count":"infinity",
        "ram_msg_count":1,
        "ram_ack_count":0,
        "next_seq_id":1,
        "persistent_count":0,
        "avg_ingress_rate":0,
        "avg_egress_rate":0,
        "avg_ack_ingress_rate":0,
        "avg_ack_egress_rate":0
    },
    "status":"running",
    "name":"01d99c41-7e08-4122-a7f3-c57d25a460f5",
    "vhost":"/",
    "durable":true,
    "auto_delete":false,
    "arguments":{
    },
    "node":"rabbit@SHACNG109WQPY"
}
Run Code Online (Sandbox Code Playgroud)