从客户端检查 RabbitMQ 队列大小

发布于 2024-07-25 04:01:01 字数 72 浏览 9 评论 0原文

有谁知道是否有办法从客户端应用程序检查 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

泪冰清 2024-08-01 04:01:01

您实际上可以通过客户端检索此信息。

当您执行 queue_declare 操作时,RabbitMQ 返回一个包含三个值的元组:(,,)queue_declarepassive 参数允许您在不修改服务器状态的情况下检查队列是否存在,因此您可以将 queue_declare一起使用被动选项来检查队列长度。

不确定 .NET,但在 Python 中,它看起来像这样:

name, jobs, consumers = chan.queue_declare(queue=queuename, passive=True)

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>). The passive argument to queue_declare allows you to check whether a queue exists without modifying the server state, so you can use queue_declare with the passive option to check the queue length.

Not sure about .NET, but in Python, it looks something like this:

name, jobs, consumers = chan.queue_declare(queue=queuename, passive=True)
幻梦 2024-08-01 04:01:01

我晚了 2 年,但我自己寻找它,发现rabbitmq 为您提供了与 erlang 节点通信的简单脚本..它位于 RabbitMQ 的启动脚本所在的 sbin 文件夹中..所以你基本上可以说

./rabbitmqctl list_queues

这将显示队列以及等待这些队列的消息计数
同样,你也可以说

./rabbitmqctl list_channels
./rabbitmqctl list_connections

等等。
如需了解更多信息,您可以访问此处

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

./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

分分钟 2024-08-01 04:01:01

如果您想在 .NET 中执行此操作,请检查您正在使用哪个版本的客户端库。

我使用的是 2.2.0 版本,并且必须使用 BasicGet(queue, noAck)。
在此版本的库中,QueueDeclare() 仅返回包含队列名称的字符串。

BasicGetResult result = channel.BasicGet("QueueName", false);
uint count = result != null ? result.MessageCount : 0;

我知道从2.6.1版本开始,QueueDeclare()返回一个QueueDeclareOk类型的对象。

QueueDeclareOk result = channel.QueueDeclare();
uint count = result.MessageCount;

或者,您可以从命令行调用:

<InstallPathToRabbitMq>\sbin\rabbitmqctl.bat list_queues

您会看到以下输出:

列出队列...
QueueName 1
...完成。

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.

BasicGetResult result = channel.BasicGet("QueueName", false);
uint count = result != null ? result.MessageCount : 0;

I know from the 2.6.1 version, QueueDeclare() returns an object of type 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.

当梦初醒 2024-08-01 04:01:01

我正在使用 .NET 客户端库的 3.3.1 版本。

我使用以下内容,这与 Ralph Willgoss 的第二个建议非常相似,但您可以提供队列名称作为参数。

QueueDeclareOk result = channel.QueueDeclarePassive(queueName);
uint count = result != null ? result.MessageCount : 0;

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.

QueueDeclareOk result = channel.QueueDeclarePassive(queueName);
uint count = result != null ? result.MessageCount : 0;
[旋木] 2024-08-01 04:01:01

我的小片段基于 Myydrralls 的回答。 我想如果他的答案中有代码,我可能会更快地注意到它。

public uint GetMessageCount(string queueName)
{
    using (IConnection connection = factory.CreateConnection())
    using (IModel channel = connection.CreateModel())
    {
        return channel.MessageCount(queueName);
    }
}

my little snippet based on Myydrralls' answer. I think if he had code in his answer I might have noticed it much quicker.

public uint GetMessageCount(string queueName)
{
    using (IConnection connection = factory.CreateConnection())
    using (IModel channel = connection.CreateModel())
    {
        return channel.MessageCount(queueName);
    }
}
陈年往事 2024-08-01 04:01:01

您可以使用此处记录的 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.

任谁 2024-08-01 04:01:01

更新:自从 mmalone 发布非常有用的帖子以来,pika 的 queue_declare(..) 实现似乎已经发生了变化。

在 python/pika (v0.9.5) 中,仍然可以通过 pika 检查队列深度,但它需要稍微更间接的方法。

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)

希望这有帮助,请对我宽容一点,我是新来的:-)

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':

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 :-)

如此安好 2024-08-01 04:01:01

我能够从 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')
  1. 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")))

IP 地址只是一个示例。

编辑:

3

我找到了更好的方法来做到这一点。 卷曲似乎是更方便、更快捷的方法


curl -s -i -u $user:$password http://$host_ip_address:15672/api/queues/$vhost_name/$queue_name | sed 's/,/\n/g' | grep '"messages"' | sed 's/"messages"://g'

I was able to get the size/depth of queue from python program.

  1. using 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')
  1. kombu [a python package usually comes with celery installation]
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.

Edit:

3

I have found a better way to do this . curl appears to be more convenient and faster way to do it


curl -s -i -u $user:$password http://$host_ip_address:15672/api/queues/$vhost_name/$queue_name | sed 's/,/\n/g' | grep '"messages"' | sed 's/"messages"://g'
ζ澈沫 2024-08-01 04:01:01

至少从 RabbitMQ 3.3.5 开始,您可以在没有任何 RabbitMQ 客户端库的 C# 程序中通过调用 RabbitMQ 管理 HTTP API 来执行此操作:

// 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);

用户名和密码与登录 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 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.

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 .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文