Prakhar
Prakhar

Reputation: 1075

Apache Camel : How to setHeader value as null

How do we set header value as null in apache camel exchange message from a processor. I am delivering message to a RabbitMQ exchange and it expects one of the header value to be set as null.

I have tried below approaches from my processor just before delivering the message

exchange.getOut().setHeader("headername","");

But this sets up an empty string to the header.

I also tried

exchange.getOut().setHeader("headername",null);

But in this case the header itself is not visible.

Please let me know if any more information is needed.

Upvotes: 4

Views: 2919

Answers (2)

Prakhar
Prakhar

Reputation: 1075

Camel has implemented a fix for this which is backported to the 2.22.1 and 2.21.3 versions and will be available there onwards. For those who are interested to know how this can be achieved, please have a look at Camel-12654 Jira issue.

camel-rabbitmq component and endpoint now support a URI option allowNullHeaders which is false by default. If you want to send custom headers with value as null, set its value to true. For example

from("rabbitmq://hostname:port/exchangeName?allowNullHeaders=true").....

This will configure camel-rabbitmq converter to set headers with null values. Now from your processor, you can do something like this

exchange.getOut().setHeader("headername",null);

This will instruct camel-rabbitmq producer, not to skip and headers which have null values.

Upvotes: 0

Claus Ibsen
Claus Ibsen

Reputation: 55525

The camel-rabbitmq component does not support headers with null values. They are filtered out in the source code.

https://github.com/apache/camel/blob/fab7a58e56e128286f327aba16c09553b26cb846/components/camel-rabbitmq/src/main/java/org/apache/camel/component/rabbitmq/RabbitMQMessageConverter.java#L171

Its a odd requirement/use-case to have to send a null value. And hence why I ask you to explain this more. There must be very good reason to consider changing Camel.

Upvotes: 1

Related Questions