RabbitMQ – Getting Message Count in a Queue

Prerequisites :

having rabbitMQ and pika installed

sample.py

#!/usr/bin/env python
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(
‘localhost’))
channel = connection.channel()
channel.queue_declare(queue=’hello’)
channel.basic_publish(exchange=”, routing_key=’hello’, body=’Hello World!’)
print(” [x] Sent ‘Hello World!'”)
connection.close()

This program can be run using the command  python sample.py

Now when we run this program the message “Hellow world ”  would be sent to the queue hello

The count of message in the queue hello can be retrieved by executing the following terminal command :

sudo rabbitmqctl list_queues | grep ‘hello’ 

This command would output something like this :

hello 1

Advertisements