Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set custom headers on RabbitMQ message using Apache Camel?

I'm trying to add custom headers on my message, so whenever an exception occurs and it ends up in the dead-letter-queue, I can see what the exception was. However all my attempts at this have failed.

  • using .setHeader()
  • setting header on the outMessage
  • setting property of the exchange

Setting the exception as a property in the payload is not allowed.

@Component
public class ProcessRoute extends RouteBuilder {
    ...
    @Override
    public void configure() throws Exception {
        onException(Exception.class)
                .log("Error for ${body}! Requeue")
                .redeliveryDelay(2000)
                .maximumRedeliveries(3)
                .handled(true)
                .setHeader("TEST", constant("TEST"))
                .process(e -> {
                    e.getOut().setHeader("TEST", "TEST");
                    e.setProperty("TEST","TEST");
                });

        from(SOME_ROUTE)
          .doSomeStuff()
          .to(RABBITMQ);
    }
    ...
}

RABBITMQ-string:

rabbitmq://foo
?exchangeType=topic
&addresses=localhost:1234
&routingKey=#
&autoDelete=false
&queue=bar
&autoAck=false
&deadLetterExchange=DLX
&deadLetterQueue=bar.dlq
&deadLetterExchangeType=direct
&deadLetterRoutingKey=#
&username=foo
&password=bar

Resulting message on the dead-letter-queue: Resulting message on dead-letter-queue

like image 808
Edward Avatar asked Feb 08 '19 15:02

Edward


1 Answers

If you use a header key following the pattern that the Camel RabbitMQ component has established, then your custom header will get picked up when the message is published to RabbitMQ.

Taking from your code above, instead of:

.setHeader("TEST", constant("TEST"))

Do this:

.setHeader("rabbitmq.TEST", constant("TEST"))

The Camel RabbitMQ component seems to ignore all the other non- "rabbitmq.*" headers that might be on the Camel exchange, and probably for good reason. There could be quite a few and most of them wouldn't make sense in the context of a message published to RabbitMQ.

like image 66
Jason Holmberg Avatar answered Oct 05 '22 11:10

Jason Holmberg