user1999397
user1999397

Reputation:

How to get exchange values in camel split

I know there are many questions on the same has been raised already, and none of them helped my case.

Here I am trying to set some value into Header inside the first iteration of Split, then in the second iteration of the split, i need old exchange value to append it to the current value.

Code: SomeDummyClass2.java

List<String> valueHeader = (List<String>) exchange.getIn().getHeader(SOME_HEADER_KEY);

// first iteration it will be null
if (CollectionUtils.isEmpty(valueHeader)) {

    exchange.getIn().setHeader(SOME_HEADER_KEY, "dummyValue");

} else { 
   // in the second iteration it should come here , but it is not comming as 'valueHeader' is null.
   someValue.addAll(valueHeader);
   exchange.getIn().setHeader(SOME_HEADER_KEY, "dummyValue");

}

Camel route:

<routes xmlns="http://camel.apache.org/schema/spring" id="route_id">
       <route>
          <from uri="somedummy_route" />
          <to uri="SomeDummyClass1" />
          <split>
             <simple>${body}</simple>
             <to uri="bean:SomeDummyClass2" />
          </split>
       </route>
    </routes>

I have gone through some of the other questions where they have asked to use CamelSplitIndex or AggregationStrategy, but I am new to these, can someone help me out.

Upvotes: 0

Views: 3625

Answers (1)

pvpkiran
pvpkiran

Reputation: 27048

Yes, We can use AggregationStrategy for this. I have created a sample working route for your case. It is in java dsl. But the strategy would be same.

 from("direct://somedummy_route").to("bean:SomeDummyClass1?method=SomeDummyMethod")
            .split(simple("${body}"))
            .aggregationStrategy(new MyStrategy())
            .to("bean:SomeDummyClass2?method=SomeDummyMethod2")
            ;

In XML

<routes xmlns="http://camel.apache.org/schema/spring" id="route_id">
   <route>
      <from uri="somedummy_route" />
      <to uri="bean:SomeDummyClass1?method=SomeDummyMethod" />
      <split>
         <simple>${body}</simple>
         <aggregate strategyRef="aggregatorStrategy">
             <to uri="bean:SomeDummyClass2?method=SomeDummyMethod2" />
         </aggregate>
      </split>
   </route>
</routes>

<bean id="aggregatorStrategy" class="org.own.package.MyStrategy"/>

And the Strategy Class would look like this

public class MyStrategy implements AggregationStrategy {

  @Override
  public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
    List<String> someValue  = Lists.newArrayList();

    if(oldExchange == null) {
      List<String> valueHeader = (List<String>) newExchange.getIn().getHeader("SomeKey");
      if (CollectionUtils.isEmpty(valueHeader)) {
        newExchange.getIn().setHeader("SomeKey", Lists.newArrayList("dummyValue"));
      }
      return newExchange;
    }
    List<String> valueHeader = (List<String>) oldExchange.getIn().getHeader("SomeKey");
    someValue.addAll(valueHeader);
    return oldExchange;
  }
}

Upvotes: 2

Related Questions