Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting number of messages in a RabbitMQ queue

We're using amqplib to publish/consume messages. I want to be able to read the number of messages on a queue (ideally both acknowledged and unacknowledged). This will allow me to show a nice status diagram to the admin users and detect if a certain component is not keeping up with the load.

I can't find any information in the amqplib docs about reading queue status.

Can someone point me in the right direction?

like image 848
Basic Avatar asked May 22 '13 11:05

Basic


People also ask

How do you check message count in RabbitMQ?

You can get some information about messages count with Management Plugin, RabbitMQ Management HTTP API and rabbitmqctl util (see list_queues, list_channels).

How do I check my RabbitMQ queue size?

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 . Show activity on this post. I was able to get the size/depth of queue from python program.

How many messages can a RabbitMQ queue hold?

Queues are single-threaded in RabbitMQ, and one queue can handle up to about 50 thousand messages.


3 Answers

Using pika:

import pika

pika_conn_params = pika.ConnectionParameters(
    host='localhost', port=5672,
    credentials=pika.credentials.PlainCredentials('guest', 'guest'),
)
connection = pika.BlockingConnection(pika_conn_params)
channel = connection.channel()
queue = channel.queue_declare(
    queue="your_queue", durable=True,
    exclusive=False, auto_delete=False
)

print(queue.method.message_count)

Using PyRabbit:

from pyrabbit.api import Client
cl = Client('localhost:55672', 'guest', 'guest')
cl.get_messages('example_vhost', 'example_queue')[0]['message_count']

Using HTTP

Syntax:

curl -i -u user:password http://localhost:15672/api/queues/vhost/queue

Example:

curl -i -u guest:guest http://localhost:15672/api/queues/%2f/celery           

Note: Default vhost is / which needs to be escaped as %2f

Using CLI:

$ sudo rabbitmqctl list_queues | grep 'my_queue'
like image 73
Pandikunta Anand Reddy Avatar answered Oct 17 '22 03:10

Pandikunta Anand Reddy


following the answer of ChillarAnand you can get the value easily. the data is in the object.

import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(
            host='localhost',
            port=5672,
            credentials=pika.credentials.PlainCredentials('guest', 'guest'),
        )
channel = connection.channel()
print(channel.queue_declare(queue="your_queue", durable=True,  exclusive=False,
                  auto_delete=False).method.message_count)

and you will get the exact message number

like image 8
OhadBasan Avatar answered Oct 17 '22 03:10

OhadBasan


Using the Java API, you can do the following :

channel.queueDeclarePassive(queueName).getMessageCount()

I believe this is also available with amqplib (according to https://code.google.com/p/py-amqplib/source/browse/amqplib/client_0_8/channel.py#1356 it seems that queue_declare() returns a tuple with the message count)

If you need more precise metrics (especially nack message count), you need to use rabbitmqctl or rabbitmq_management. Rabbitmq_management is probably a good choice due to its HTTP API. More info : http://www.rabbitmq.com/management.html

like image 2
mguillermin Avatar answered Oct 17 '22 03:10

mguillermin