Hi,
I see that this question has been asked a number of times but none of post
helped or had a conclusive solution. I am splitting a message and then
aggregating it using Aggregator2. The code was throwing exception because
oldExchange was always null. So to test I designed a small code.
I read an orders,xml file which looks like this
<Orders xmlns="http://some/schema/Order">
<Order>
<orderNum>1</orderNum>
</Order>
<Order>
<orderNum>2</orderNum>
</Order>
<Order>
<orderNum>3</orderNum>
</Order>
<Order>
<orderNum>5</orderNum>
</Order>
<Order>
<orderNum>6</orderNum>
</Order>
</Orders>
My camel Context Looks like this
<camel:camelContext xmlns="http://camel.apache.org/schema/spring"
xmlns:te="http://acn/schema/Order">
<camel:route>
<camel:from uri="file:src/data/catask/test?noop=true"/>
<camel:log message="${body}"></camel:log>
<camel:split>
<camel:xpath>//te:Orders/*</camel:xpath>
<camel:to uri="direct:logQueries"/>
<camel:to uri="direct:aggegateQueries"/>
</camel:split>
</camel:route>
<camel:route>
<camel:from uri="direct:logQueries"/>
<camel:log message="After the call : \n ${body}"></camel:log>
</camel:route>
<camel:route>
<camel:from uri="direct:aggegateQueries"/>
<camel:aggregate strategyRef="aggrTask" completionInterval="8000" >
<camel:correlationExpression>
<camel:xpath>//te:Order</camel:xpath>
</camel:correlationExpression>
<camel:to uri="file:src/data/catask/output?fileName=output.xml"/>
</camel:aggregate>
</camel:route>
</camel:camelContext>
My Aggregation Strategy class looks like this
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
if (oldExchange == null) {
System.out.println("Returning new exchange");
return newExchange;
String oldBody = oldExchange.getIn().getBody(String.class);
String newBody = newExchange.getIn().getBody(String.class);
oldExchange.getIn().setBody(oldBody + "+" + newBody);
return oldExchange;
The problem is that when the aggregated result is saved in output.xml file
it contains only the last record it read from Orders.xml.
i.e.
<Order xmlns="http://some/schema/Order">
<orderNum>6</orderNum>
</Order>
I looked into it further and found that this was happening because after the
first call oldExchange should have some value but it turns out it is always
null. I think that because it is reading everything from a single file and
splitting it, there is only exchange.
Any suggestions??
I see that this question has been asked a number of times but none of post
helped or had a conclusive solution. I am splitting a message and then
aggregating it using Aggregator2. The code was throwing exception because
oldExchange was always null. So to test I designed a small code.
I read an orders,xml file which looks like this
<Orders xmlns="http://some/schema/Order">
<Order>
<orderNum>1</orderNum>
</Order>
<Order>
<orderNum>2</orderNum>
</Order>
<Order>
<orderNum>3</orderNum>
</Order>
<Order>
<orderNum>5</orderNum>
</Order>
<Order>
<orderNum>6</orderNum>
</Order>
</Orders>
My camel Context Looks like this
<camel:camelContext xmlns="http://camel.apache.org/schema/spring"
xmlns:te="http://acn/schema/Order">
<camel:route>
<camel:from uri="file:src/data/catask/test?noop=true"/>
<camel:log message="${body}"></camel:log>
<camel:split>
<camel:xpath>//te:Orders/*</camel:xpath>
<camel:to uri="direct:logQueries"/>
<camel:to uri="direct:aggegateQueries"/>
</camel:split>
</camel:route>
<camel:route>
<camel:from uri="direct:logQueries"/>
<camel:log message="After the call : \n ${body}"></camel:log>
</camel:route>
<camel:route>
<camel:from uri="direct:aggegateQueries"/>
<camel:aggregate strategyRef="aggrTask" completionInterval="8000" >
<camel:correlationExpression>
<camel:xpath>//te:Order</camel:xpath>
</camel:correlationExpression>
<camel:to uri="file:src/data/catask/output?fileName=output.xml"/>
</camel:aggregate>
</camel:route>
</camel:camelContext>
My Aggregation Strategy class looks like this
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
if (oldExchange == null) {
System.out.println("Returning new exchange");
return newExchange;
String oldBody = oldExchange.getIn().getBody(String.class);
String newBody = newExchange.getIn().getBody(String.class);
oldExchange.getIn().setBody(oldBody + "+" + newBody);
return oldExchange;
The problem is that when the aggregated result is saved in output.xml file
it contains only the last record it read from Orders.xml.
i.e.
<Order xmlns="http://some/schema/Order">
<orderNum>6</orderNum>
</Order>
I looked into it further and found that this was happening because after the
first call oldExchange should have some value but it turns out it is always
null. I think that because it is reading everything from a single file and
splitting it, there is only exchange.
Any suggestions??