Aggregator Module In Mule 4
Aggregator module in mule 4 is used to club the data on the basis of some condition, we are going to demonstrate size based aggregator in this tutorial
Different Aggregator Types
Size Based Aggregator: This aggregator aggregate the payload/data until a predefined size is reached
Time Based Aggregator: This aggregator aggregate the payload/data until a predefined time is reached
Group Based Aggregator : This aggregator aggregate the payload/data on the basis of a group like company name , so we can aggregate different employee of different company and work on that
Size Based Aggregator
We will create a sample mule application in anypoint studio to demonstrate the size based aggregator
Drag and drop the size based aggregator from mule palette( if you do not find the aggregator in mule palette you can add the module from exchange see more details

Configure the HTTP listener to trigger the request, there are multiple ways to send the request , we can consume the data from JMS Queue, Anypoint MQ, Database Table but here just to create a POC I am going to trigger request 5 times as I defined the size as 5

Provide the name of the aggregator and size

Drag and drop the aggregator listener from palette to listen the aggregated payload , aggregated in last step and process the aggregated payload, here we are transforming it to application/json and logging

Deploy the project and trigger the below request 5 times with different payload, we can see that 5 different payloads are aggregated and print in the logs, which is an array of employee

INFO 2021-04-13 12:42:16,703 [[MuleRuntime].uber.07: [aggregator-module].aggregator-module-size-based-aggregator.CPU_LITE @7fe21a3d] [processor: aggregator-module-size-based-aggregator/processors/0/route/0/processors/0; event: 4c089da0-9c4d-11eb-8725-9aaf65ed174b] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: aggregator complete [TypedValue[value: '[B@571b663a', dataType: 'SimpleDataType{type=[B, mimeType='application/json; charset=UTF-8'}'], TypedValue[value: '[B@56ec0205', dataType: 'SimpleDataType{type=[B, mimeType='application/json; charset=UTF-8'}'], TypedValue[value: '[B@3c90c01e', dataType: 'SimpleDataType{type=[B, mimeType='application/json; charset=UTF-8'}'], TypedValue[value: '[B@29e731d5', dataType: 'SimpleDataType{type=[B, mimeType='application/json; charset=UTF-8'}'], TypedValue[value: '[B@2541a865', dataType: 'SimpleDataType{type=[B, mimeType='application/json; charset=UTF-8'}']]
INFO 2021-04-13 12:42:16,710 [[MuleRuntime].uber.06: [aggregator-module].aggregator-moduleFlow.CPU_INTENSIVE @8cfcf0b] [processor: aggregator-moduleFlow/processors/1; event: 1f00e7e0-9c4d-11eb-8725-9aaf65ed174b] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: aggregated payload is
[
{
"employee": {
"First Name": "Mulesy1",
"Last Name": "Mulesy1",
"EMP ID": "123456789",
"EMP EMAIL": "[email protected]",
"PHONE NUMBER": "2132432449"
}
},
{
"employee": {
"First Name": "Mulesy2",
"Last Name": "Mulesy2",
"EMP ID": "123456789",
"EMP EMAIL": "[email protected]",
"PHONE NUMBER": "2132432449"
}
},
{
"employee": {
"First Name": "Mulesy3",
"Last Name": "Mulesy2",
"EMP ID": "123456789",
"EMP EMAIL": "[email protected]",
"PHONE NUMBER": "2132432449"
}
},
{
"employee": {
"First Name": "Mulesy4",
"Last Name": "Mulesy2",
"EMP ID": "123456789",
"EMP EMAIL": "[email protected]",
"PHONE NUMBER": "2132432449"
}
},
{
"employee": {
"First Name": "Mulesy5",
"Last Name": "Mulesy2",
"EMP ID": "123456789",
"EMP EMAIL": "[email protected]",
"PHONE NUMBER": "2132432449"
}
}
Sample application: size based aggregator sample