从客户端检查 RabbitMQ 队列大小
有谁知道是否有办法从客户端应用程序检查 RabbitMQ 队列中的消息数量?
我正在使用 .NET 客户端库。
Does anyone know if there's a way to check the number of messages in a RabbitMQ queue from a client application?
I'm using the .NET client library.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您实际上可以通过客户端检索此信息。
当您执行
queue_declare
操作时,RabbitMQ 返回一个包含三个值的元组:(,,)
。queue_declare
的passive
参数允许您在不修改服务器状态的情况下检查队列是否存在,因此您可以将queue_declare
与一起使用被动选项来检查队列长度。
不确定 .NET,但在 Python 中,它看起来像这样:
You can actually retrieve this via the client.
When you perform a
queue_declare
operation, RabbitMQ returns a tuple with three values:(<queue name>, <message count>, <consumer count>)
. Thepassive
argument toqueue_declare
allows you to check whether a queue exists without modifying the server state, so you can usequeue_declare
with thepassive
option to check the queue length.Not sure about .NET, but in Python, it looks something like this:
我晚了 2 年,但我自己寻找它,发现rabbitmq 为您提供了与 erlang 节点通信的简单脚本..它位于 RabbitMQ 的启动脚本所在的 sbin 文件夹中..所以你基本上可以说
这将显示队列以及等待这些队列的消息计数
同样,你也可以说
等等。
如需了解更多信息,您可以访问此处
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
this will display the queues along with the count of messages pending to those queues
similarly you can also say
etc.
For more info you can visit here
如果您想在 .NET 中执行此操作,请检查您正在使用哪个版本的客户端库。
我使用的是 2.2.0 版本,并且必须使用 BasicGet(queue, noAck)。
在此版本的库中,QueueDeclare() 仅返回包含队列名称的字符串。
我知道从2.6.1版本开始,QueueDeclare()返回一个QueueDeclareOk类型的对象。
或者,您可以从命令行调用:
您会看到以下输出:
If you want to do this in .NET, check which version of the client library you are using.
I'm using the 2.2.0 version and I had to use BasicGet(queue, noAck).
In this version of the library, QueueDeclare() only returns a string containing the queue name.
I know from the 2.6.1 version, QueueDeclare() returns an object of type QueueDeclareOk.
Alternatively, you can call from the command line:
And you see the following output:
我正在使用 .NET 客户端库的 3.3.1 版本。
我使用以下内容,这与 Ralph Willgoss 的第二个建议非常相似,但您可以提供队列名称作为参数。
I'm using version 3.3.1 of the .NET client library.
I use the following, which is very similar to Ralph Willgoss's second suggestion, but you can supply the queue name as an argument.
我的小片段基于 Myydrralls 的回答。 我想如果他的答案中有代码,我可能会更快地注意到它。
my little snippet based on Myydrralls' answer. I think if he had code in his answer I might have noticed it much quicker.
您可以使用此处记录的 IModel 的 MessageCount 方法
http://www.rabbitmq.com/releases/rabbitmq- dotnet-client/v3.6.4/rabbitmq-dotnet-client-3.6.4-client-htmldoc/html/type-RabbitMQ.Client.IModel.html#method-M:RabbitMQ.Client.IModel.MessageCount(System.String)
编辑:我知道这是一篇很老的帖子,但这是谷歌的第一个回复,我希望它能帮助人们将来寻找这个答案。
You can use the IModel's MessageCount method, documented here
http://www.rabbitmq.com/releases/rabbitmq-dotnet-client/v3.6.4/rabbitmq-dotnet-client-3.6.4-client-htmldoc/html/type-RabbitMQ.Client.IModel.html#method-M:RabbitMQ.Client.IModel.MessageCount(System.String)
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.
更新:自从 mmalone 发布非常有用的帖子以来,pika 的 queue_declare(..) 实现似乎已经发生了变化。
在 python/pika (v0.9.5) 中,仍然可以通过 pika 检查队列深度,但它需要稍微更间接的方法。
queue_declare(...) 将方法对象传递到其回调函数中,然后您可以检查该回调函数。 例如,要检查名为
'myQueue'
的队列中的消息和消费者的数量:希望这有帮助,请对我宽容一点,我是新来的:-)
Update: it appears that the pika implementation of queue_declare(..) has changed since mmalone's very helpful post.
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.
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'
:Hope this helps, and please go easy on me, I'm new around here :-)
我能够从 python 程序获取队列的大小/深度。
IP 地址只是一个示例。
编辑:
3
我找到了更好的方法来做到这一点。 卷曲似乎是更方便、更快捷的方法
I was able to get the size/depth of queue from python program.
the ip address is just an example.
Edit:
3
I have found a better way to do this . curl appears to be more convenient and faster way to do it
至少从 RabbitMQ 3.3.5 开始,您可以在没有任何 RabbitMQ 客户端库的 C# 程序中通过调用 RabbitMQ 管理 HTTP API 来执行此操作:
用户名和密码与登录 RabbitMQ 管理控制台 UI 时使用的用户名和密码相同。
响应将是一个 JSON 字符串,其中包含队列列表,包括它们的消息计数以及其他属性。 (如果您愿意,可以使用 Json.NET 等库将该 JSON 反序列化为 C# 对象。 )
API 文档与 RabbitMQ 管理控制台一起安装,并且应该可以在该服务器上找到,网址为 http://MY_RABBITMQ_SERVER:15672/ API 。
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:
The username and password are the same as those you use to log into the RabbitMQ management console 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.)
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 .