title: Apache Camel - Split and aggregate - Old Exchange is always null
body: <p>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. </p>
  
  <p>I read an orders,xml file which looks like this </p>
  
  <pre><code>&lt;Orders xmlns="http://some/schema/Order"&gt;
      &lt;Order&gt;
              &lt;orderNum&gt;1&lt;/orderNum&gt;
      &lt;/Order&gt;
      &lt;Order&gt;
              &lt;orderNum&gt;2&lt;/orderNum&gt;
      &lt;/Order&gt;
      &lt;Order&gt;
              &lt;orderNum&gt;3&lt;/orderNum&gt;
      &lt;/Order&gt;
      &lt;Order&gt;
              &lt;orderNum&gt;5&lt;/orderNum&gt;
      &lt;/Order&gt;
      &lt;Order&gt;
              &lt;orderNum&gt;6&lt;/orderNum&gt;
      &lt;/Order&gt;
  </code></pre>
  
  <p></p>
  
  <p>My camel Context Looks like this 
  </p>
  
  <pre><code>&lt;camel:route&gt;
  &lt;camel:from uri="file:src/data/catask/test?noop=true"/&gt;
  &lt;camel:log message="${body}"&gt;&lt;/camel:log&gt;
  &lt;camel:split&gt;
  &lt;camel:xpath&gt;//te:Orders/*&lt;/camel:xpath&gt;
  &lt;camel:to uri="direct:logQueries"/&gt;
  &lt;camel:to uri="direct:aggegateQueries"/&gt;  
  &lt;/camel:split&gt;
  
  &lt;/camel:route&gt;
  
  &lt;camel:route&gt;
  &lt;camel:from uri="direct:logQueries"/&gt;
  &lt;camel:log message="After the call : \n ${body}"&gt;&lt;/camel:log&gt;  
  &lt;/camel:route&gt;
  
   &lt;camel:route&gt;
  &lt;camel:from uri="direct:aggegateQueries"/&gt;
  &lt;camel:aggregate strategyRef="aggrTask" completionInterval="8000" &gt;
  &lt;camel:correlationExpression&gt;
  &lt;camel:xpath&gt;//te:Order&lt;/camel:xpath&gt;
  &lt;/camel:correlationExpression&gt;
  &lt;camel:to uri="file:src/data/catask/output?fileName=output.xml"/&gt;  
  
  &lt;/camel:aggregate&gt;
  &lt;/camel:route&gt;  
  </code></pre>
  
  <p></p>
  
  <p>My Aggregation Strategy class looks like this </p>
  
  <pre><code>   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; 
          } 
  </code></pre>
  
  <p>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. </p>
  
  <p>i.e. </p>
  
  <pre><code>&lt;Order xmlns="http://some/schema/Order"&gt;
              &lt;orderNum&gt;6&lt;/orderNum&gt;
      &lt;/Order&gt;
  </code></pre>
  
  <p>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. </p>
  
  <p><strong>> Any suggestions??</strong></p>
  
  <p><strong>UPDATE 1:</strong> Per Claus I can use Splitter only to solve this issue. I did that and was able to successfully join all the messages. However I am still looking for a way to use Aggregator2. Here how I did it using Splitter only.</p>
  
  <pre><code>camel:route&gt;
  &lt;camel:from uri="file:src/data/catask/test?noop=true"/&gt;
  &lt;camel:log message="${body}"&gt;&lt;/camel:log&gt;
  &lt;camel:split strategyRef="aggrTask"&gt; 
  &lt;camel:xpath&gt;//te:Orders/*&lt;/camel:xpath&gt;
  &lt;camel:to uri="direct:logQueries"/&gt;
   &lt; 
  &lt;/camel:split&gt;
  
  &lt;/camel:route&gt;
  
  &lt;camel:route&gt;
  &lt;camel:from uri="direct:logQueries"/&gt;
  &lt;camel:log message="After the call : \n ${body}"&gt;&lt;/camel:log&gt;  
  &lt;/camel:route&gt;
  </code></pre>