Apache Camel™ is one of the most popular open source integration framework that allows conditional routing in any of defined domain-specific languages, be it Java, XML or Scala. A Route contains flow and integration logic using a specific DSL.
![]() | |||||||
| Composed Message Processing |
Sample Route with a dynamic content based router, which on arrival of composite request splits it into the request of the applicable system System 1, 2 and 3, aggreagates response from all three systems and sends it to another system, from where the result is sent back to requester.
The same can also be implemented for returning aggregated response to requester by simply sending the response back without redirecting to another service url.
Below is sample Apache Camel route in Spring DSL.
Sample Route with a dynamic content based router, which on arrival of composite request splits it using custom splitting stategy defined in bean customSplitter. Here the three sytems are invoked in parallel aand the responses are aggregated on the go using custom aggregation strategy defined in bean customAggregator.
Breaks a single message into many sub-messages. Here is an example of how a splitter can be implemented.
The request is split into three sub mesagges and directed to separate routes namely
routeSystem1, routeSystem2 and routeSystem3. The responses from these route are then passed into the aggregator for combining them.
Attribute : strategyRef
Sets a reference to the AggregationStrategy to be used to assemble the replies from the splitted messages.Here we are using a POJO as the AggregationStrategy. Here is an example of how an aggregator can be implemented.
The aggregator supports two main features of parallel processing and stopping the route on exception as explained below.
Here we are processing requests parallely instead of sequentially. So, the aggregator should be thread safe and there should be no dependency between the Systems 1, 2 and 3.
Attribute : parallelProcessing
If enabled then processing each splitted messages occurs concurrently. Note the caller thread will still wait until all messages has been fully processed, before it continues. Its only processing the sub messages from the splitter which happens concurrently.
Default value: false
Attribute : stopOnException
Will stop further processing if an exception or failure occurred during processing of an org.apache.camel.Exchange and the caused exception will be thrown. Will also stop if processing the exchange failed (has a fault message) or an exception was thrown and handled by the error handler (such as using onException). In all situations the splitter will stop further processing.
Default value: false
The route doesn't end here as you can see this aggregated response is sent to the final web service, and the response received from there is forwarded to serviceProcessor bean if in case you want any processing on the incoming response after that the response will be redirected to the caller. As we can see, the interaction with 4 different systems with complex route design can be achieved very easily with the help of Apache camel. One thing to keep in mind is that the code handling bulk requests should have robust logging and error handling.
Also, do keep in mind the timeouts of various systems and transaction handling if required.
