IBM BPM, V8.0.1, All platforms > Programming IBM BPM > Enterprise Service Bus programming > Aggregation > Aggregation patterns
Nested aggregation
This scenario is based on a supermarket company, where each outlet sends multiple restocking orders to a central company-wide system that batches the requests into a single message. This single message is submitted to the mediation flow where, for each order and for each branch, a separate invocation to the order system should occur.
Design the aggregation
The ordering system only accepts a single record, whereas the input into the mediation module is a nested batch message. This introduces the need for aggregation, and in this case nested aggregation, because the batch request message is repeated on an outlet basis, and then repeated on an order basis. Orders are sent individually; the response from each order is stored into a single response message, which can be aggregated later. This single response message shows the status of all the elements in the batch request. To explore how this basic scenario is built, first examine the business objects used, as shown in Figure 1 .
Figure 1. Business objects for batch order example
![]()
The GroupsOrders request is a batched array of Outlets tagged by an ID, and Outlets contains a batched array of Orders. Figure 2.shows the result.
Figure 2. Result of batched order example
![]()
Shared context
The structure of the Shared context is essential to a successful aggregation design, and it is even more critical within a nested aggregation. The goal is to store the response from each Order into a structure that can then be converted into a single response message. The initial request is at a company-wide scope, the first aggregation is at the Outlet scope, and the second aggregation is at the Order scope, as depicted in Figure 3.
Figure 3. Aggregation levels
![]()
Understanding the aggregation structure helps you define the correct Shared context. The logic pattern within the mediation flow is:
- Aggregate on an Outlet basis.
- Aggregate on an Order basis.
- Call the Order service.
- Store the current Order service within the current Outlet response.
- Repeat for each Order associated with the Outlet.
- Store the current Outlet in the company-wide aggregation store.
- Repeat for each Outlet.
- Convert all results into a response message.
With this pattern you can store the overall company-wide response, the current Outlet response, and the current Order response in the Shared context, as shown in Figure 4 .
Figure 4. Shared outlet responses
![]()
The flow detail
Now look at the flow in detail. It is separated into three sections:
- Company-wide scope
- Outlet or Store scope
- Order scope
This is shown in Figure 5 .
Figure 5. Aggregation block
![]()