javarabbitmqspring-amqpspring-rabbitrabbitmq-exchange

How to specify additional info on a rabbit message when it's dead lettered


I have a rabbit queue with messages for consuming. I also have a listener that can fail. The queue is configured with a dead letter exchange (along with a dead letter queue). What I want is to see an exception info in the messages sitting in the dead letter queue.

Here is how it works currently:

  1. I send a corrupted message to my normal queue.
  2. My listener (I'm using Java's org.springframework.amqp.core.MessageListener) fails with something like: "java.lang.RuntimeException: corrupted message"
  3. The message gets rejected and goes to the dead letter queue thru the dead letter exchange.
  4. When I look at the dead-lettered message in the Rabbit Admin UI, I see: headers:
    x-death:
    reason: rejected

But what I want is to see the "java.lang.RuntimeException: corrupted message" somewhere on UI. I assume it should be a custom header?

Is it possible to, for example, put a general try-catch to my listener and enhance the headers with the exception info?


Solution

  • No; RabbitMQ (actually the AMQP specification) provides no mechanism for the consumer to to enhance rejected messages with additional information. The protocol only supports acknowledging or rejecting messages.

    Spring AMQP, together with a retry interceptor, provides a mechanism to republish the message to a different queue (which can be the same as the DLQ) with additional information in the headers (exception stack trace etc).

    See RepublishMessageRecoverer in the section about error handling with asynchronous consumers.