.net 从客户端检查 RabbitMQ 队列大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1038318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check RabbitMQ queue size from client
提问by Pablote
Does anyone know if there's a way to check the number of messages in a RabbitMQ queue from a client application?
有谁知道是否有办法从客户端应用程序检查 RabbitMQ 队列中的消息数量?
I'm using the .NET client library.
我正在使用 .NET 客户端库。
回答by mmalone
You actually can retrieve this via the client. When perform a queue_declareoperation RabbitMQ returns a three tuple containing (<queue name>, <message count>, <consumer count>). The passiveargument to queue_declare allows you to check whether a queue exists without modifying the server state. So you can use queue_declarewith the passiveoption to check queue length. Not sure about .NET, but in Python it looks something like this:
您实际上可以通过客户端检索它。当执行一个queue_declare操作 RabbitMQ 返回一个包含(<queue name>, <message count>, <consumer count>). passivequeue_declare的参数允许您在不修改服务器状态的情况下检查队列是否存在。所以,你可以使用queue_declare与passive选项来检查队列长度。不确定 .NET,但在 Python 中它看起来像这样:
name, jobs, consumers = chan.queue_declare(queue=queuename, passive=True)
回答by Rafi
I am 2 years too late but I was searching for it myself and found that rabbitmq gives u simple script to communicate to erlang nodes..its in sbin folder where the starting script for RabbitMQ is located..so you can basically say
我已经晚了 2 年,但我自己在寻找它,发现rabbitmq 给了你一个简单的脚本来与 erlang 节点通信......它在 RabbitMQ 的起始脚本所在的 sbin 文件夹中......所以你基本上可以说
./rabbitmqctl list_queues
this will display the queues along with the count of messages pending to those queues similarly you can also say
这将显示队列以及待处理到这些队列的消息计数,同样你也可以说
./rabbitmqctl list_channels
./rabbitmqctl list_connections
etc. For more info you can visit here
等有关更多信息,您可以访问这里
回答by Ralph Willgoss
If you wanted to do this in .Net, check which version of the Client library you are using.
如果您想在 .Net 中执行此操作,请检查您使用的是哪个版本的客户端库。
I'm using the 2.2.0version and I had to use BasicGet(queue, noAck).
In this version of the Library QueueDeclare() only returns a string containing the queue name.
我使用的是2.2.0版本,我不得不使用 BasicGet(queue, noAck)。
在这个版本的库中 QueueDeclare() 只返回一个包含队列名称的字符串。
BasicGetResult result = channel.BasicGet("QueueName", false);
uint count = result != null ? result.MessageCount : 0;
I know from the 2.6.1version, QueueDeclare() returns an object of type QueueDeclareOk.
我从2.6.1版本知道,QueueDeclare() 返回一个 QueueDeclareOk 类型的对象。
QueueDeclareOk result = channel.QueueDeclare();
uint count = result.MessageCount;
Alternatively, you can call from the command line:
或者,您可以从命令行调用:
<InstallPathToRabbitMq>\sbin\rabbitmqctl.bat list_queues
And you see the following output:
您会看到以下输出:
Listing queues...
QueueName 1
...done.
列出队列...
QueueName 1
...完成。
HTH
HTH
回答by Mike
I'm using version 3.3.1of the .Net Client Library.
我正在使用.Net 客户端库的3.3.1版。
I use the following, which is very similar to Ralph Willgoss second suggestion but you can supply the queue name as an argument.
我使用以下内容,这与 Ralph Willgoss 的第二个建议非常相似,但您可以提供队列名称作为参数。
QueueDeclareOk result = channel.QueueDeclarePassive(queueName);
uint count = result != null ? result.MessageCount : 0;
回答by Mike
my little snippet based on Myydrralls' answer. I think if he had code in his answer I might have noticed it much quicker.
我的小片段基于 Myydrralls 的回答。我想如果他的答案中有代码,我可能会更快地注意到它。
public uint GetMessageCount(string queueName)
{
using (IConnection connection = factory.CreateConnection())
using (IModel channel = connection.CreateModel())
{
return channel.MessageCount(queueName);
}
}
回答by Myddraall
You can use the IModel's MessageCount method, documented here
您可以使用 IModel 的 MessageCount 方法,记录在此处
edit: I know this is a very old post, but it is the first google response, and I hope it will help people looking for this answer in the future.
编辑:我知道这是一篇很老的帖子,但它是第一个谷歌回复,我希望它会帮助人们在未来寻找这个答案。
回答by tohster
Update: it appears that the pika implementation of queue_declare(..) has changed since mmalone's very helpful post.
更新:似乎 queue_declare(..) 的 pika 实现自 mmalone 的非常有用的帖子以来发生了变化。
In python/pika (v0.9.5) it's still possible to check the queue depth via pika, but it requires a slightly more indirect approach.
在 python/pika (v0.9.5) 中,仍然可以通过 pika 检查队列深度,但它需要稍微间接的方法。
queue_declare(...) passes a method object into its callback function, which you can then inspect. For example, to check the number of messages and consumers in the queue named 'myQueue':
queue_declare(...) 将一个方法对象传递到它的回调函数中,然后您可以对其进行检查。例如,检查名为 的队列中消息和消费者的数量'myQueue':
def cbInspect(qb):
messagesInQueue = qb.method.message_count
print "There are %d messages in myQueue" % messagesInQueue
consumersInQueue = qb.method.consumer_count
print "There are %d consumers in myQueue" % consumersInQueue
return
myChannel = channel.queue_declare(callback=cbInspect, queue='myQueue', passive=True)
Hope this helps, and please go easy on me, I'm new around here :-)
希望这会有所帮助,请放轻松,我是新来的:-)
回答by Jon Schneider
At least as of RabbitMQ 3.3.5, you can do this in a C# program without any RabbitMQ client library by calling the RabbitMQ Management HTTP API:
至少从 RabbitMQ 3.3.5 开始,您可以通过调用 RabbitMQ 管理 HTTP API 在没有任何 RabbitMQ 客户端库的 C# 程序中执行此操作:
// The last segment of the URL is the RabbitMQ "virtual host name".
// The default virtual host name is "/", represented urlEncoded by "%2F".
string queuesUrl = "http://MY_RABBITMQ_SERVER:15672/api/queues/%2F";
WebClient webClient = new WebClient { Credentials = new NetworkCredential("MY_RABBITMQ_USERNAME", "MY_RABBITMQ_PASSWORD") };
string response = webClient.DownloadString(queuesUrl);
The username and password are the same as those you use to log into the RabbitMQ management console UI.
用户名和密码与您用于登录 RabbitMQ 管理控制台 UI 的用户名和密码相同。
Response will be a JSON string with the list of queues, including their message counts, among other properties. (If you like, you can deserialize that JSON into a C# object using a library like Json.NET.)
响应将是一个带有队列列表的 JSON 字符串,包括它们的消息计数以及其他属性。(如果您愿意,可以使用Json.NET 之类的库将该 JSON 反序列化为 C# 对象。)
The API documentation is installed along with the RabbitMQ management console and should be available on that server at http://MY_RABBITMQ_SERVER:15672/api.
API 文档与 RabbitMQ 管理控制台一起安装,应该在该服务器上提供,网址为http://MY_RABBITMQ_SERVER:15672/api。
回答by Mir Al-Masud
I was able to get the size/depth of queue from python program. 1. using py_rabbit
我能够从 python 程序中获取队列的大小/深度。1.使用py_rabbit
from pyrabbit.api import Client
cl = Client('10.111.123.54:15672', 'userid', 'password',5)
depth = cl.get_queue_depth('vhost', 'queue_name')
- kombu [a python package usually comes with celery installation]
- kombu [一个 python 包通常随 celery 安装一起提供]
conn = kombu.Connection('amqp://userid:[email protected]:5672/vhost')
conn.connect()
client = conn.get_manager()
queues = client.get_queues('vhost')
for queue in queues:
if queue == queue_name:
print("tasks waiting in queue:"+str(queue.get("messages_ready")))
print("tasks currently running:"+str(queue.get("messages_unacknowledged")))
the ip address is just an example.
IP地址只是一个例子。

