This week, we'll study a Message Routing pattern known as Aggregator.
When do I use this pattern?
The Aggregator is used when multiple related messages are combined to form a single message. For example, an order can be divided into an order header and multiple order items, however, the receiver expects entire order as one message. In this case, an Aggregator can wait for all order items to reach middleware, combine them, and forward the aggregated message to the next step.
Aggregator in CPI
In CPI, Aggregator component is used to implement Aggregator pattern. For the demonstration, I'll combine these 3 order items into one message:
The Order Items have Id (a unique identifier), Order Id (the unique identifier of the order they belong to), and next order item's id (the unique identifier of next order item in the sequence).
The integration flow exposes the Aggregator as an HTTPS service using HTTPS Adapter. The Aggregator is responsible for aggregating all the order items into a single message. Finally, Log is a Groovy Script step to log the aggregated message.
The Aggregator's configuration is as follows:
XML (Same Format)
Last Message Condition
Completion Timeout (in min)
Data Store Name
Here, the correlation expression is used to identify order items that belong together. This value is an example of a Correlation Identifier. In the example, all the order items belonging to a single order are related.
The Last Message Condition informs the Aggregator to stop waiting for further messages and forward the aggregated message to the next step. In the example, when NextOrderItemId is not present, all the order items have been received.