Mulesoft Aggregator Module

In this blog we will discuss about the Aggregator module, its significance and types.
About Aggregator module
MuleSoft provides a aggregator module that can be used in the flow to aggregate messages. This is often used in scenarios where messages from different sources or processing steps need to be combined into a single message for further processing.
Benefits
- To consolidate data from multiple messages into a single message. It is useful when you have parallel processing or multiple branches in your integration flow and want to combine the results.
- Allows you to design integration flows where different parts of the message processing can occur in parallel. After parallel processing, the Collection Aggregator brings the results together, improving overall processing efficiency.
- MuleSoft allows you to define dynamic aggregation strategies based on certain conditions. This flexibility enables you to implement different aggregation logic depending on the specific requirements of your integration flow.
An aggregator scope functions as a component for storage and release of values and gathering these values until a specified condition is met. Upon meeting the condition, it initiates the execution of a processor's chain of components, utilizing the aggregated elements as the payload. The Aggregators Module encompasses different types of aggregators with predefined configurations. They are,
- Size Based Aggregator
- Time Based Aggregator
- Group Based Aggregator
The fundamental configuration for an aggregator involves specifying the value to aggregate, obtained through expression evaluation from the message. It also includes a condition indicating when the aggregation is deemed complete, along with a processor chain that activates upon meeting that condition.
Aggregators serve as pass-through routers, meaning the data received by an aggregator scope undergoes processing by subsequent components in the flow. The only alteration occurs in variable propagation if any variables are established during the execution of aggregation routes.
Let us explain the different types of aggregator with a sample Mulesoft application.
Size Based Aggregator
The Size-Based Aggregator Scope allows the aggregation of elements until a specified size is reached, completing the aggregation process.
First of all you need to download the aggregator module from Exchange. You could find the aggregator module in Mule palette.

Then Drag and drop the HTTP listener in the canvas and configure the HTTP listener to run in a local environment.

Add a transform message to the flow after the HTTP listener and set up the payload.
%dw 2.0
output application/json
- - -
{
"Source Name": "mulecraft size based",
"Source ID": uuid()
}
Then add size based aggregator and setup according to your requirements. Configure the size based aggregator as per the image.

Run and Deploy the Mule application. Then hit the HTTP requests 5 times through Postman. You will see the below response in logs.


Time Based Aggregator
The Time-Based Aggregator Scope within the Aggregators module allows you to aggregate elements within a specified time limit. The implementation of Time based aggregator is similar to the size based aggregator.
Set up the HTTP listener and add the Transform message as per image. Then set up a below payload.
%dw 2.0
output application/json
- - -
{
"Source Name": "mulecraft time-based",
"Source ID": (random() as String)
}
Then add the time based aggregator module and configure the parameters as per the image.

Run and Deploy the application. Hit the Postman too.
You could see only three payload is processed and completed for the given specific time period.

Group Based Aggregator
The Group-Based Aggregator Scope within the Aggregators module allows you to aggregate elements by assigning them to specific groups based on a defined group ID.
The setup is similar to the Time based aggregator. You need to add the transform message with the below payload.
%dw 2.0
output application/json
- - -
{
"Source Name": "mulecraft-group-based",
"GroupId": message.attributes['queryParams'].groupId,
"Source ID": (random() as String)
}
Set a group based aggregator flow and configure the parameters as per image.


Hit the postman with the queryParams- groupId with values 1 and 2.

According to the group size, you could find the response in logs.

Conclusion
Hope you had good understanding about the aggregator module. If there is any scenario on your project required to aggregate the payload based on the time, size and groupId, you can make use of the aggregator module in Mulesoft.