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.
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 :-)
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);
}
}
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
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 .
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;
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')
conn = kombu.Connection('amqp://userid:password@10.111.123.54: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.