Split and Aggregate in Apache Camel

Junior5413 picture Junior5413 · Nov 1, 2017 · Viewed 9.1k times · Source

I'd like to split exchange message body (it's list of MyCustomClass object), process them (one by one) and them aggregate the all exchanges together. Split is ok, process one by one also ok, but I can't figure out how to aggregate them.

from("mysource")
    .unmarshal(new ListJacksonDataFormat(MyClass.class))
    .split().body()
    .process(new Processor() {
        @Override
        public void process(Exchange exchange) throws Exception {
            // process MyClass item
            exchange.getIn().setBody(processedItem);
        }
    })
    .to("destinationForProcessedItem")
    .aggregate(new GroupedExchangeAggregationStrategy()) <== Seems like problem is here
    .process(new Processor() {
            // handle result of aggregation
    })

I don't need complicated aggregation, just collect list of splitted Exchanges and handle them in the final processor.

Answer

rathna picture rathna · Nov 2, 2017

write like this

         .aggregate(new AggregationStrategy() {
                @Override
                public Exchange aggregate(Exchange exchange, Exchange exchange1) {
                    //logic for aggregation using exchnage and exchange1
                }
            })