1

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!

ReactiveRaven
  • 7,203
  • 2
  • 29
  • 38
Clowning
  • 178
  • 4
  • 16

2 Answers2

1

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.

Andrés Andrade
  • 2,213
  • 2
  • 18
  • 23
-4

nack or reject either discard the message or re-queue the message.

For your requirement following could be suitable,

Once the consumer receives the message, just before start processing it, send ack() back to rabbitmq server.

Process the message then after, If found any error in the process then send ( publish ) the same message into the same queue. This will put the message at the back of the queue.

On successful processing do nothing. ack() has been already sent to rabbitmq server. Just take the next message and process it.

  • 2
    Why should ack() no processed messages and with errors? The acknowledgment should be bone after the message is processed, otherwise if something goes wrong before or while it's being processed or published again this message will be lost and removed from the queue. You should reject the message and make the decision if you want to requeue or discard the message. – Andrés Andrade Dec 29 '16 at 13:07
  • Its all depends on the business requirement when to ack() the message before or after the processing. We can send the message back into the queue if any error/exception occurs as the last failover code to execute before the script gets die/exit. If there are few scenarios on which we want to put the message at the back of the queue and not for all then, we can keep ack() / nack() / republish after processing. we can decide at the end either ack / nack or republish the message at the end of the processing. – Bhimrao Gadge Dec 30 '16 at 12:51