Trying to GET rabbit msg without ack, but no success

1.1k views Asked by At

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

2

There are 2 answers

1
kadamb On BEST ANSWER

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()
3
BarrensZeppelin On

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.