Aggregator
This applies for Camel version 2.3 or newer. If you use an older version then use this Aggregator link instead.
The Aggregator from the EIP patterns allows you to combine a number of messages together into a single message.
A correlation Expression is used to determine the messages which should be aggregated together. If you want to aggregate all messages into a single message, just use a constant expression. An AggregationStrategy is used to combine all the message exchanges for a single correlation key into a single message exchange. The default strategy just chooses the latest message; so its ideal for throttling messages.
For example, imagine a stock market data system; you are receiving 30,000 messages per second; you may want to throttle down the updates as, say, a GUI cannot cope with such massive update rates. So you may want to aggregate these messages together so that within a window (defined by a maximum number of messages or a timeout), messages for the same stock are aggregated together; by just choosing the latest message and discarding the older prices. (You could apply a delta processing algorithm if you prefer to capture some of the history).
Aggregator options
The aggregator supports the following options:
Option |
Default |
Description |
---|---|---|
correlationExpression |
|
Mandatory Expression which evaluates the correlation key to use for aggregation. The Exchange which has the same correlation key is aggregated together. If the correlation key could not be evaluated an Exception is thrown. You can disable this by using the |
aggregationStrategy |
|
Mandatory |
aggregationStrategyRef |
|
A reference to lookup the |
completionSize |
|
Number of messages aggregated before the aggregation is complete. |
completionTimeout |
|
Time in millis that an aggregated exchange should be inactive before its complete. Camel has a background task that runs once a minute to check for inactive aggregated exchanges. |
completionPredicate |
|
A Predicate to indicate when an aggregated exchange is complete. |
completionFromBatchConsumer |
|
This option is if the exchanges is coming from a Batch Consumer. Then when enabled the Aggregator2 will use the batch size determined by the Batch Consumer in the message header |
eagerCheckCompletion |
|
Whether or not to eager check for completion when a new incoming Exchange has been received. This option influences the behavior of the |
groupExchanges |
|
If enabled then Camel will group all aggregated Exchanges into a single combined |
ignoreBadCorrelationKeys |
|
Whether or not to ignore correlation keys which could not be evaluated to a value. By default Camel will thrown an Exception, but you can enable this option and ignore the situation instead. |
closeCorrelationKeyOnCompletion |
|
Whether or not too late Exchange should be accepted or not. You can enable this to indicate that if a correlation key has already been completed, then any new exchanges with the same correlation key be denied. Camel will then throw a |
aggregationRepository |
|
Allows you to plugin you own implementation of |
aggregationRepositoryRef |
|
Reference to lookup a |
parallelProcessing |
|
When aggregated are completed they are being send out of the aggregator. This option indicates whether or not Camel should use a thread pool with multiple threads for concurrency. If not custom thread pool has been specified then Camel creates a default pool with 10 concurrent threads. |
executorService |
|
If using |
executorServiceRef |
|
Reference to lookup a |
Exchange Properties
The following properties is set on each Exchange that are aggregated:
header |
type |
description |
---|---|---|
|
int |
The total number of Exchanges aggregated into this combined Exchange. |
About AggregationStrategy
The AggregationStrategy
is used for aggregate the old (lookup by its correlation id) and the new exchanges together into a single exchange. Possible implementations include performing some kind of combining or delta processing, such as adding line items together into an invoice or just using the newest exchange and removing old exchanges such as for state tracking or market data prices; where old values are of little use.
Notice the aggregation strategy is a mandatory option and must be provided to the aggregator.
About completion
When aggregation Exchanges at some point you need to indicate that the aggregated exchanges is complete, so they can be send out of the aggregator. Camel allows you to indicate completion in various ways as follows:
- completionTimeout - Is an inactivity timeout in which is triggered if no new exchanges has been aggregated for that particular correlation key within the period.
- completionSize - Is a number indicating that after X aggregated exchanges its complete.
- completionPredicate - Runs a Predicate when a new exchange is aggregated to determine if we are complete or not
- completionFromBatchConsumer - Special option for Batch Consumer which allows you to complete when all the messages from the batch has been aggregated. |
Notice that all the completion ways are per correlation key. And you can combine them in any way your like. Its basically the first which triggers that wins. So you can use a completion size together with a completion timeout.
Notice the completion is a mandatory option and must be provided to the aggregator.
Examples
See some examples from the old Aggregator which is somewhat similar to this new aggregator.
Using completionTimeout
In this example we want to aggregate all incoming messages and after 3 seconds of inactivity we want the aggregation to complete. This is done using the completionTimeout
option as shown:
And the same example using Spring XML:
Using completionSize
In this example we want to aggregate all incoming messages and when we have 3 messages aggregated (in the same correlation group) we want the aggregation to complete. This is done using the completionSize
option as shown:
And the same example using Spring XML:
Using completionPredicate
In this example we want to aggregate all incoming messages and use a Predicate to determine when we are complete. The Predicate can be evaluated using either the aggregated exchange (default) or the incoming exchange. We will so both situations as examples. We start with the default situation as shown:
And the same example using Spring XML:
And the other situation where we use the eagerCheckCompletion
option to tell Camel to use the incoming Exchange. Notice how we can just test in the completion predicate that the incoming message is the END message:
And the same example using Spring XML:
Using This Pattern
If you would like to use this EIP Pattern then please read the Getting Started, you may also find the Architecture useful particularly the description of Endpoint and URIs. Then you could try out some of the Examples first before trying this pattern out.
See also
- The Loan Broker Example which uses an aggregator
- Blog post by Torsten Mielke about using the aggregator correctly.
- The old Aggregator