Lho Ben
Lho Ben

Reputation: 2149

RabbitMQ : add exception to message header before send to DLQ with DLX policy

Can you please suggest a way to add the exception to my original message header before sending it to DLQ with DLX policy in RabbitMQ

i tried to do it with aspect but my altered message is not the one pushed to DLQ, which is normal, the original message in the broker is always published instead.

@Service
@RabbitListener(queues = "queueName")
public class Listener {


    @RabbitHandler
    public void receive(BasicMessage message) {
        try {
            doSomeWork();
            
        } catch(Exception e) {

        }
    }
}

Upvotes: 1

Views: 1476

Answers (1)

Gary Russell
Gary Russell

Reputation: 174664

The message has to be republished programmatically; you can't modify the message if the broker just re-routes it to the DLQ.

The framework provides a mechanism to do this.

See the documentation.

The RepublishMessageRecoverer publishes the message with additional information in message headers, such as the exception message, stack trace, original exchange, and routing key. Additional headers can be added by creating a subclass and overriding additionalHeaders(). The deliveryMode (or any other properties) can also be changed in the additionalHeaders().

Upvotes: 2

Related Questions