3

Using the pika client, I want to display all the messages currently in the queue, without consuming them. Just to know how busy is the queue and display the jobs.

So far, I can only read one message as it arrives:

channel.queue_declare(queue='queue1', durable=True)
channel.basic_consume(on_message, queue='queue1')
channel.start_consuming()

def on_message(channel, method, properties, message):
    channel.basic_ack(delivery_tag=method.delivery_tag)
    print("Message: %s", message)

How can I read the whole queue?

user3599803
  • 6,435
  • 17
  • 69
  • 130
  • Check this out!. https://stackoverflow.com/questions/22647268/in-rabbitmq-how-to-consume-multiple-message-or-read-all-messages-in-a-queue-or-a – bumblebee Feb 14 '19 at 07:06

1 Answers1

1

To read messages "without consuming them", don't acknowledge delivery of the message. In your case above, get rid of

channel.basic_ack(delivery_tag=method.delivery_tag)

or set auto_ack to False:

def callback(ch, method, properties, body):
    print(body)
   
channel.basic_consume(queue='your_queue', on_message_callback=callback, auto_ack=False)

The messages will be read and marked as unacked in rabbitMQ but will still be available in the queue.

Victor Kironde
  • 193
  • 1
  • 10
  • `auto_ack` doesn't seem to work for me (using `pika.BlockingConnection`): `TypeError: basic_consume() got an unexpected keyword argument 'auto_ack'` – Kostas Mouratidis Mar 29 '22 at 14:51