Previous - Splitter | Index | Next - Resequencer
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:
<OrderItem>
<Id>1</Id>
<OrderId>001</OrderId>
<NextOrderItemId>2</NextOrderItemId>
</OrderItem>
<OrderItem>
<Id>2</Id>
<OrderId>001</OrderId>
<NextOrderItemId>3</NextOrderItemId>
</OrderItem>
<OrderItem>
<Id>3</Id>
<OrderId>001</OrderId>
</OrderItem>
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).
Integration Flow

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:
Tab |
Parameter |
Value |
Correlation |
Correlation Expression |
/OrderItem/OrderId |
Aggregation Strategy |
Incoming Format |
XML (Same Format) |
Aggregation Strategy |
Aggregation Algorithm |
Combine |
Aggregation Strategy |
Last Message Condition |
not(boolean(/OrderItem/NextOrderItemId)) |
Aggregation Strategy |
Completion Timeout (in min) |
60 |
Aggregation Strategy |
Data Store Name |
Orders |
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.
Output
The output is an aggregated message:
<multimap:Messages xmlns:multimap="http://sap.com/xi/XI/SplitAndMerge">
<multimap:Message1>
<OrderItem>
<Id>1</Id>
<OrderId>001</OrderId>
<NextOrderItemId>2</NextOrderItemId>
</OrderItem>
<OrderItem>
<Id>2</Id>
<OrderId>001</OrderId>
<NextOrderItemId>3</NextOrderItemId>
</OrderItem>
<OrderItem>
<Id>3</Id>
<OrderId>001</OrderId>
</OrderItem>
</multimap:Message1>
</multimap:Messages>
Conclusion
The Aggregator Pattern is used to combine multiple related messages into a single message. In CPI, Aggregator can be implemented using the
Aggregator Component.
References/Further Readings
Hope this helps,
Bala
Previous - Splitter | Index | Next - Resequencer