0

I have a task in which I try to get all the messages in a rabbit queue. I only need to GET, and not CONSUME. So here is the code, I am using

def some_function_name() :
    connection = rabbitObj.get_connection()
    channel = rabbitObj.get_channel(connection)
    while True : 
        method_frame, header_frame, body = channel.basic_get(queue='error_queue', no_ack=False)
        if method_frame:
            #do some work
        else :
             break #breaking the loop

while(True):
    some_function_name()

when I run this code, it works properly,first time.I get all the messages in queue and and all messages remain in 'Ready' state, but when I run the loop second time, all messages turn change to 'Unacknowledged' state.

Requirement : Every time I should only GET messages,and they should not go Unacknowledged.

First Loop:

First Loop

Second Loop :

enter image description here

Can anyone help me with, what I am doing wrong, or what changes should I make.

Thanks in advance :)

Edit 1: As for @BarrensZeppelin 's answer, all msgs are lost, if I set no_ack=True. Check the below screenshot : enter image description here

kadamb
  • 1,532
  • 3
  • 29
  • 55

2 Answers2

0

When you set no_ack=False you specifically tell the broker to expect a reply, which is why all the messages become Unacknowledged. Try setting no_ack=True.

BarrensZeppelin
  • 345
  • 1
  • 4
  • 15
  • In that case, all the messages get lost. Adding a screenshot in question for this case. – kadamb Sep 12 '17 at 09:17
  • Aha, so you want to browse the queue. This is not supported in RabbitMQ, but there are work-arounds. Take a look at this question: https://stackoverflow.com/questions/4700292/using-rabbitmq-is-there-a-way-to-look-at-the-queue-contents-without-a-dequeue – BarrensZeppelin Sep 12 '17 at 09:21
  • There is no GET action in RabbitMQ as you think of it. What you are really doing is consuming all the messages. Put simply: basic_consume is for consuming multiple messages in a row efficiently and blocks until messages become available, basic_get always returns a single message (if available - otherwise None). – BarrensZeppelin Sep 12 '17 at 09:29
0

I got a workaround, and it is working. Closing the rabbit connection, after consuming did the trick.(though now it is taking time to create and close a connection everytime)

def some_function_name() :
    connection = rabbitObj.get_connection()
    channel = rabbitObj.get_channel(connection)
    while True : 
        method_frame, header_frame, body = channel.basic_get(queue='error_queue', no_ack=False)
        if method_frame:
            #do some work
        else :
             break #breaking the loop
    rabbitObj.close_connection(connection)

while(True):
    some_function_name()
kadamb
  • 1,532
  • 3
  • 29
  • 55