問題描述
我有一個監控 RabbitMQ 隊列的 Java 客戶端.我可以使用此代碼獲取當前隊列中的消息數
I have a Java client which monitors RabbitMQ queue. I am able to get the count of messages currently in queue with this code
@Resource
RabbitAdmin rabbitAdmin;
..........
DeclareOk declareOk = rabbitAdmin.getRabbitTemplate().execute(new ChannelCallback<DeclareOk>() {
public DeclareOk doInRabbit(Channel channel) throws Exception {
return channel.queueDeclarePassive("test.pending");
}
});
return declareOk.getMessageCount();
我想了解更多詳細信息,例如 -
I want to get some more additional details like -
- 當前排隊項目的消息正文.
- 自隊列創建以來在隊列中排隊的消息總數.
有沒有辦法在 Java 客戶端中檢索這些數據?
Is there any way to retrieve these data in Java client?
推薦答案
使用 AMQP 協議(??包括 RabbitMQ 實現)您無法 100% 保證獲得此類信息.
With AMQP protocol (including RabbitMQ implementation) you can't get such info with 100% guarantee.
與消息計數最接近的數字是使用 queue.declare-ok
(AMQP.Queue.DeclareOk
在 java AMQP 客戶端庫中).
The closest number to messages count is messages count returned with queue.declare-ok
(AMQP.Queue.DeclareOk
in java AMQP client library).
雖然您使用 queue.declare-ok
收到的消息計數可能與隊列中的確切消息數量匹配,但您不能依賴它,因為它不計算等待確認或發布到隊列的消息事務但尚未提交.
Whilst messages count you receive with queue.declare-ok
may match exact messages number enqueues, you can't rely on it as it doesn't count messages which waiting acknowledges or published to queue during transaction but not committed yet.
這真的取決于你需要什么樣的精度.
It really depends what kind of precission do you need.
對于排隊的消息正文,您可能需要手動提取隊列中的所有消息,查看它們的正文并將它們放回隊列.這是做你想做的事的唯一方法.
As to enqueued messages body, you may want to manually extract all messages in queue, view their body and put them back to queue. This is the only way to do what you want.
您可以使用 管理插件,RabbitMQ 管理 HTTP API 和 rabbitmqctl 實用程序(參見 list_queues、list_channels).
You can get some information about messages count with Management Plugin, RabbitMQ Management HTTP API and rabbitmqctl util (see list_queues, list_channels).
自創建隊列以來,您無法獲得已發布消息的總數,而且我認為沒有人在它無用的情況下實現此類統計信息(僅供參考,消息流平均每秒 10k,您甚至不會在幾千年內達到 uint64).
You can't get total published messages count since queue was created and I think nobody implement such stats while it useless (FYI, with messages flow in average 10k per second you will not even reach uint64 in a few thousand years).
這篇關于RabbitMQ - 獲取排隊的消息總數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!