Simple AggregationStrategy Apache Camel

1.7k views Asked by At

I would like to know if Camel provides any standard aggregation strategies out-of-the-box. I have been researching but I have only be able to find some of the in the unit test. Those ones cannot be used from the actual code. I am trying to use it with Apache Camel 2.12.1 and Spring DSL.

In particular, I am looking for this one:

public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
    if (oldExchange == null) {
        return newExchange;
    }

    String oldBody = oldExchange.getIn().getBody(String.class);
    String newBody = newExchange.getIn().getBody(String.class);
    oldExchange.getIn().setBody(oldBody + "+" + newBody);
    return oldExchange;
}

If they are not included in the package, is there any solution to do the same in Spring DSL without having to create the AggregationStrategy in code?

Thanks for the help!

2

There are 2 answers

2
SergeyB On BEST ANSWER

A generic aggregator would not work. The example you have works if both bodies are Strings and assumes they don't need to be separated with any delimiters. But what about JSON or XML? Simple concatenating those would not work as you'd end up with 2 top level elements. I believe that's why Camel has you implement your own.

0
François Beausoleil On

At least in version 2.13.1 (the one I'm using now), there exists a GroupedExchangeAggregationStrategy which extends AbstractListAggregationStrategy<Exchange>. The GroupedExchangeAggregationStrategy concatenates all members into a List<T>. After that, it's a matter of converting a list to another type, probably by using a POJO.