One of the more common scenarios is the ability to combine different messages together based on certain relationships so they can then be processed as a composite. When we need to combine multiple messages together we will use the Aggregator pattern.
In the following examples, we will use a stateful filter (an Aggregator), to collect and persist individual messages until it receives a complete set of related messages. Once all messages have been received it will then publish the composite message.
XML DSL Simple Example
As an example, suppose we want to update our website every five minutes with the latest stock quotes. Since we can receive multiple quotes for the same stock within our designated time period, we only want to keep the last one (since it is the most up-to-date. We can accomplish this with the aggregator:
<route> <from uri="jms:topic:loan:qoute"/> <aggregate strategyRef="bestQuote"> <correlationExpression> <header>loanId</header> </correlationExpression> <completionPredicate> <simple>${header.CamelAggregatedSize} > 2</simple> </completionPredicate> </aggregate> <to uri="jsmith:bestQuote"/> </route> <bean id="bestQuote" class="com.mycompany.BestQuoteProcess"/>
NOTE: In this example, we are using simple language to declare the completion predicate. It is a basic language that only supports a primitive set of operators. The operation of ${header. CamelAggregatedSize} will get the header that contains the number of messages aggregated.
XML DSL Simple Example
As an example, suppose