I finally figured it out. my initial understanding was that we can have two messages in hand and merge them for Ex - Route 1 final message -
Dinesh
Route 2 final message -
<Address>
<city>New York</city>
</Address>
My understanding was that after having the two messages above I could just build an aggregationStartegy and merge them. Well my assumption was wrong. The way enricher works is that it has one message in hand and before it gets the message from second route, we need to tell Camel that - "Hey before you get the message the Route 2, here's the message from Route 1. When you fetch the message from Route 1, use my aggregation strategy class to merge them"."
So I was expecting the following out after using the Enricher
<Person>
<name>Dinesh</name>
</Person>
<Address>
<city>New York</city>
</Address>
BUT I was not sure how to do it. Here's what I was doing and it was wrong approach
<route id="getPerson">
<from uri="file:src/data/catask/person?noop=true" />
<to uri="direct:enrich"/>
</route>
<route id="getAddress">
<from uri="file:src/data/catask/address?noop=true" />
<to uri="direct:enrich"/>
</route>
<route id="enrich">
<from uri="direct:enrich"/>
<enrich strategyRef="aggregationBean"/>
<log message="After Merge ... ${body}"/>
</route>
<bean id="aggregationBean" class="com.mycompany.camel.canadatask.AggregationStrategy"/>
My Java class looked like this
public class AggregationStrategy implements
org.apache.camel.processor.aggregate.AggregationStrategy{
@Override
public Exchange aggregate(Exchange message,Exchange resource) {
String old = resource.getIn().getBody(String.class);
System.out.println("OLD:: \n"+old);
String newMsg = message.getIn().getBody(String.class);
System.out.println("NEW:: \n"+newMsg);
System.out.println("MERGED::" + old + newMsg);
message.getIn().setBody(old+newMsg);
return message;
}
}
Now of course the above code did not work. I later realized the mistake, my understanding about enricher was wrong.
The correct implementation is like this -
<route id="getPerson">
<from uri="file:src/data/catask/person?noop=true" />
<pollEnrich strategyRef="aggregationBean" uri="file:src/data/catask/address?noop=true"/>
<log message="After Merge ... ${body}"/>
</route>
<bean id="aggregationBean" class="com.mycompany.camel.canadatask.AggregationStrategy"/>
The java code remained the same -
public class AggregationStrategy implements
org.apache.camel.processor.aggregate.AggregationStrategy{
@Override
public Exchange aggregate(Exchange message,Exchange resource) {
String old = resource.getIn().getBody(String.class);
System.out.println("OLD:: \n"+old);
String newMsg = message.getIn().getBody(String.class);
System.out.println("NEW:: \n"+newMsg);
System.out.println("MERGED::" + old + newMsg);
message.getIn().setBody(old+newMsg);
return message;
}
}