How to set default data format for a component in Apache Camel?
I have a number of routes interacting with different ActiveMQ queues. At the moment all of them look like
from("...")
.process(...)
.marshal().json() // (1)
.to("activemq:queue:...");
or
from("activemq:queue:...")
.unmarshal().json() // (2)
.process(...)
.to("...");
- I would like to replace lines (1) and (2) with either component or context level configuration. Basically saying only once ‘message payload going through ActiveMQ has to be a JSON string’.
- I don’t like to add any additional routes, processors, headers, URI parameters, etc.
- Ideally, it would be applicable for other components and formats besides ActiveMQ and JSON
2
Answers
Using interceptors (Based on Claus Ibsen's comment)
Notes:
RouteBuilder
. OtherwiseIllegalArgumentException
is thrown explaining situation.RouteBuilder
.You could marshall/unmarshall using a named reference to a data format that you can define once (here as “myDefaultFormat”) in your Camel Registry:
This way, you dont have to repeat .json() everywhere
(but ok, you have yet to repeat the named reference :-$ )