問(wèn)題描述
我有一個(gè)監(jiān)控 RabbitMQ 隊(duì)列的 Java 客戶端.我可以使用此代碼獲取當(dāng)前隊(duì)列中的消息數(shù)
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();
我想了解更多詳細(xì)信息,例如 -
I want to get some more additional details like -
- 當(dāng)前排隊(duì)項(xiàng)目的消息正文.
- 自隊(duì)列創(chuàng)建以來(lái)在隊(duì)列中排隊(duì)的消息總數(shù).
有沒(méi)有辦法在 Java 客戶端中檢索這些數(shù)據(jù)?
Is there any way to retrieve these data in Java client?
推薦答案
使用 AMQP 協(xié)議(??包括 RabbitMQ 實(shí)現(xiàn))您無(wú)法 100% 保證獲得此類信息.
With AMQP protocol (including RabbitMQ implementation) you can't get such info with 100% guarantee.
與消息計(jì)數(shù)最接近的數(shù)字是使用 queue.declare-ok
(AMQP.Queue.DeclareOk
在 java AMQP 客戶端庫(kù)中).
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
收到的消息計(jì)數(shù)可能與隊(duì)列中的確切消息數(shù)量匹配,但您不能依賴它,因?yàn)樗挥?jì)算等待確認(rèn)或發(fā)布到隊(duì)列的消息事務(wù)但尚未提交.
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.
對(duì)于排隊(duì)的消息正文,您可能需要手動(dòng)提取隊(duì)列中的所有消息,查看它們的正文并將它們放回隊(duì)列.這是做你想做的事的唯一方法.
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 實(shí)用程序(參見(jiàn) 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).
自創(chuàng)建隊(duì)列以來(lái),您無(wú)法獲得已發(fā)布消息的總數(shù),而且我認(rèn)為沒(méi)有人在它無(wú)用的情況下實(shí)現(xiàn)此類統(tǒng)計(jì)信息(僅供參考,消息流平均每秒 10k,您甚至不會(huì)在幾千年內(nèi)達(dá)到 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).
這篇關(guān)于RabbitMQ - 獲取排隊(duì)的消息總數(shù)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!