Check RabbitMQ queue size from client

2019-03-08 10:11发布

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.

8条回答
戒情不戒烟
2楼-- · 2019-03-08 10:23

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

查看更多
太酷不给撩
3楼-- · 2019-03-08 10:27

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

查看更多
狗以群分
4楼-- · 2019-03-08 10:31

You actually can retrieve this via the client. When perform a queue_declare operation RabbitMQ returns a three tuple containing (<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 queue length. Not sure about .NET, but in Python it looks something like this:

name, jobs, consumers = chan.queue_declare(queue=queuename, passive=True)
查看更多
smile是对你的礼貌
5楼-- · 2019-03-08 10:31

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);
    }
}
查看更多
放我归山
6楼-- · 2019-03-08 10:32

I'm using version 3.3.1 of the .Net Client Library.

I use the following, which is very similar to Ralph Willgoss second suggestion but you can supply the queue name as an argument.

QueueDeclareOk result = channel.QueueDeclarePassive(queueName);
uint count = result != null ? result.MessageCount : 0;
查看更多
欢心
7楼-- · 2019-03-08 10:37

If you wanted 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.

HTH

查看更多
登录 后发表回答