An introduction to Parallel For Each
- February 14, 2020
MuleSoft has the capability to process a collection of objects in an iterative manner. This is useful for modifying records inside a collection. It’s already provided concepts like Batch Process and For Each to achieve this. In this technical article, we’ll talk about a new component introduced in Mule 4.2.0 called Parallel For Each.
This article assumes you’re familiar with For Each scope and batch scope in Mule. Please make sure you have Mule 4.2.0 runtime and Anypoint Studio 7+ in your system.
Currently the component isn’t available in Mule Palette. We need to add the modifications directly in the configuration xml. Don’t worry if you see the component as not recognized in the Mule Flow.
What does Parallel For Each do?
Like For Each Scope in Mule, the Parallel For Each scope splits the payload into elements and processes them one by one using the components you place in the scope.
The payload inside the scope is each of the split elements on each iteration.
Unlike the sequential processing in For Each scope, here the processing happens in parallel. Here, each iteration is given to a specific thread from thread pool.
The output of this scope is the collection of the results of each route, and the aggregated results will be in the same order they were before the split.
Currently, the Parallel For Each component isn’t available. Please modify it directly in the configuration xml page.
What is the output format of the parallel foreach scope?
[
{
“inboundAttachmentNames”:[],
“exceptionPayload”: null,
“inboundPropertyNames”: [],
“outboundAttachmentNames”: [],
“payload”: “beta”,
“outboundPropertyNames”:[],
“attributes”: null
}
]
The output will be an array of objects where each object represents an iteration.
Error Handling in Parallel For Each
- Parallel For Each will continue to process all the routes even if an exception occurs in an iteration and will invoke the error handler only after all the routes completes its processing with aggregated results and errors from each iteration.
- Throws
Format of error message
{
“inboundAttachmentNames”: [],
“exceptionPayload”: null,
“inboundPropertyNames”: [],
“outboundAttachmentNames”: [],
“payload”: { -> This gives the aggregated result
“failures”: { -> The aggregated failure messages are listed
“1”: { -> This indicates in which iteration the error occurred
“errorType”: { -> Error Details
“parentErrorType”: {
“parentErrorType”: null,
“identifier”: “ANY”,
“namespace”: “MULE”
},
“identifier”: “EXPRESSION”,
“namespace”: “MULE”
},
“childErrors”: [],
“errorMessage”: null,
“cause”: cause details over here,
“description”: error desciption over here
}
},
“results”: { -> This gives the aggregated success results.
“0”: {
“inboundAttachmentNames”: [],
“exceptionPayload”: null,
“inboundPropertyNames”: [],
“outboundAttachmentNames”: [],
“payload”: “apple”,
“outboundPropertyNames”: [],
“attributes”: null
}
}
},
“outboundPropertyNames”: [],
“attributes”: null
}
How is it different from For Each?
Foreach | Parallel Foreach |
The split collection is processed sequentially. | The split collection is processed parallelly. |
Single thread manages each iteration. After one iteration completes, the same thread moves to the next iteration | Multi thread processing, each iteration is given to available thread and all the threads execute parallelly. |
Counter variables exist. | Counter variable not available |
Attributes modifications from the previous iteration will be available to the next iteration. | Each route will have access to data given before the start of the scope. There is no persistence of modifications across routes. |
The output of the scope will be same as the input | The output will be an aggregated result from each iteration. |
Execution gets interrupted when an exception is thrown, and error handler is invoked | Execution will continue even if an exception occurs and error handler is invoked only after all the iteration routes has completed its processing |
Conclusion
Mule 4 has added many new ideas. Parallel For Each will increase the performance of the For Each scope due to its parallel processing capability.
— By Rithul Kumar