user5822650
user5822650

Reputation:

RabbitMQ/Spring AMQP - Leave message in a queue

I created a SpringBoot/Spring AMQP project where I configured a listener on a RabbitMQ queue. Question: Is there any way to leave the message in the queue? Let me explain: I consume the message and do some things (eg save on db), if something goes wrong I would like to be able to reconsume the message.

Thanks in advance

Upvotes: 1

Views: 1061

Answers (2)

istepaniuk
istepaniuk

Reputation: 4269

I don't know about the "Spring" way of accomplishing this, but what you describe is the normal behavior for AMQP consumers that do not automatically acknowledge.

From the documentation:

In automatic acknowledgement mode, a message is considered to be successfully delivered immediately after it is sent.

When you turn off automatic acknowledgment, your consumer must explicitly acknowledge the message, otherwise it will not be dequeued (or as you put it, it will be left "in the queue"). You will then need to simply ACK the message at the very end of your operation, when you are certain that your operation succeeded (and perhaps coordinated with your database transaction).

There is always the question of what to do first; acknowledge first or commit your database transaction first? Without adding complexity, you must choose what's best depending on what failure mode is less problematic for you, i.e. Would you rather tolerate a duplicated message or a missing message?

Upvotes: 0

Artem Bilan
Artem Bilan

Reputation: 121552

You need to think about configuring your listener container with transactions, so when DB call fails, the transaction is going to be rolled back and an AMQP message will not be acked on RabbitMQ.

See docs for more info: https://docs.spring.io/spring-amqp/docs/current/reference/html/#transactions

Upvotes: 0

Related Questions