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

2

5

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?

Constitutive answered 19/11, 2015 at 17:24 Comment(0)
T
7

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.

Turtleback answered 19/11, 2015 at 17:52 Comment(2)
This is the pattern I use as well. I like to think about basic.reject and/or failure to send basic.ack as an indication that the consumer crashed or had some other problem unrelated to the message or anything downstream. In this case, it is appropriate for the broker to requeue the message unchanged, and allow another consumer to take it immediately. If the consumer process is healthy enough to write sensible information to the message headers, it might as well republish it to an "undeliverable" or "retry" exchange and basic.ack the original message.Tenor
This answer is outdated. See https://mcmap.net/q/2006945/-how-to-specify-additional-info-on-a-rabbit-message-when-it-39-s-dead-letteredDenominator
D
0

Yes, it's possible. When consuming from a quorum queue, the consumer can settle the message with the AMQP modified outcome with field undeliverable-here = true and field message-annotations containing your Java exception. RabbitMQ will then dead-letter the message as described in the blog post.

Denominator answered 9/10 at 21:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.