0

I have written a MessagePostProcessor which gets called before any consumer onMessage is called.

Now if any exception occurs in my MessagePostProcessor i need to catch the exception otherwise the message will get infinitely requed in Rabbitmq server Q getting back to and fro again. So fix this problem i need to catch an exception in my component MessagePostProcessor , But because of that the consumers are not getting correct problem for the issue at their end.

What is best practice of handling such scenarios.

Pseudo MessagePostProcessor

@Service 
public class TestPostProcessor implements MessagePostProcessor {

    /**
 * {@inheritDoc}
 */
@Override
public Message postProcessMessage(Message message) {
    try {
        // some logic
    } catch (Exception exception) {
        // log error
    }
    return message;
}

}

Akshat
  • 575
  • 2
  • 12
  • 28

1 Answers1

2

But because of that the consumers are not getting correct problem for the issue at their end.

I am not sure what you mean by that.

If the post processor can't process the message for some reason, you can throw an AmqpRejectAndDontRequeuException and the message will be discarded (or routed to a DLX/DLQ if so configured).

If you want the message to be passed to the consumer, but somehow communicate the problem to the consumer, you can return a completely new Message, or add some data to the headers.

message.getMessageProperties().getHeaders().set("badMessageInfo", ...);
Gary Russell
  • 166,535
  • 14
  • 146
  • 179