How to put a message at the end of MQRabbit Queue

3.3k views Asked by At

I'm working on a worker which is able to treat message from a RabbitMQ.

However, I am unsure of how to accomplish this.

If I receive a message and during my treating an error occurs, how can I put the message into the end of the queue?

I'm trying to using nack or reject, but the message is always re-put in the first position, and other messages stay frozen!

I don't understand why the message has to be put in the first position, I'm trying to "play" with other options like requeue or AllupTo but none of them seem to work.

Thank you in advance!

2

There are 2 answers

2
Bhimrao Gadge On BEST ANSWER

RabbitMQ always tries to keep the messages in the publication order.

If you want to put the received message at the back of the queue from the consumer then, send ack() back to rabbitmq server, RabbitMQ will remove the existing message who’s acknowledgement it's waiting for. Send the message data to the same queue, it will get put into the back of the queue.

0
Andrés Andrade On

Documentation says:

Messages can be returned to the queue using AMQP methods that feature a requeue parameter (basic.recover, basic.reject and basic.nack), or due to a channel closing while holding unacknowledged messages. Any of these scenarios caused messages to be requeued at the back of the queue for RabbitMQ releases earlier than 2.7.0. From RabbitMQ release 2.7.0, messages are always held in the queue in publication order, even in the presence of requeueing or channel closure.

With release 2.7.0 and later it is still possible for individual consumers to observe messages out of order if the queue has multiple subscribers. This is due to the actions of other subscribers who may requeue messages. From the perspective of the queue the messages are always held in the publication order.

Remember to ack your successful messages, otherwise they will not be removed from the queue.

If you need more control over your rejected messages you should take a look to dead letter exchanges.