Search code examples
javaapache-camelapache-servicemix

How can I merge the response from an external service call with the original message in camel


From a logical perspective this is the kind of routing behaviour I wish to implement:

Logical view of routing info

I want to be able to merge the response of an external service with the original request.

I have been able to implement this using multicasting, an aggregator, and a mock endpoint but I was wondering if there is a cleaner way. My current implementation looks like this:

        <multicast strategyRef="serviceAggregator" stopOnException="false">
            <to uri="mock:foo" />
            <to uri="http://0.0.0.0:9999/service/?throwExceptionOnFailure=false" />
        </multicast>
        <camel:to uri="log:uk.co.company.aggregated?showAll=true" />
        <to uri="http://0.0.0.0:9999/anotherService/ 

The part I particularly don't like is using a mock endpoint but I also don't think that this is a very readable way to express the above diagram. So I was wondering if there was a more elegant way of doing this?


Solution

  • I suggest to read about the EIP patterns, for example the content enricher http://camel.apache.org/content-enricher.html

    Where you can merge the reply message with the request message.

    Mind the Content Enricher has 2 modes - enrich - pollEnrich

    Make sure to notice the difference, from the docs in the link above.

    <route>
      <from uri="...">
      <enrich uri="http://0.0.0.0:9999/service/?throwExceptionOnFailure=false" strategyRef="serviceAggregator"/>
      <to uri="log:uk.co.company.aggregated?showAll=true" />
      <to uri="http://0.0.0.0:9999/anotherService/>
      ...
    </route>
    

    And yes you diagram is showing splitter, but the sample code is using multicast EIP.