For each and Parallel for each scope

Scopes in Mule 4 are powerful constructs that control how parts of a flow are executed, repeated, or grouped. Each scope serves a specific purpose and can be used to achieve complex integration logic with clarity and efficiency. This blog will explore the different types of scopes in Mule 4, their purpose, and examples of how to use them.
For each is used to perform similar set of activities on same payload of a collection. It will expect collection as input payload and it will iterate over that collection.
- If it’s a Java collection then it will split the payload by default
- Changes done to a payload within iteration will not change anything in the input payload
- For each work in sequential only. So if there is any error processing any record then it will not process the further records and break the loop immediately
- To overcome this problem we can have try and catch block with on error continue to keep on processing further records in case of any error
- Order will be maintained while processing
Implementation
Create mule application that accept the JSON collection as payload and Add a logger which will print the received payload.

Add For Each activity where following parameters are passed
- Collection – input payload which should be of type collection
- Counter variable – counter which will hold the value of current loop
- Batch Size – we can break the payload in our desired size in case we want to process more than 1 record in each loop
- Root Message – will hold the actual input payload
In our case – payload.Employees will hold the collection.
Add logger to print each payload.

Run the application.
First logger will print the actual input payload.
INFO 2020-04-13 12:29:20,306
[[MuleRuntime].cpuLight.15: [for-each-sample].for-each-sampleFlow.CPU_LITE
@4b51d624] [event: 4c7bad41-7d54-11ea-8bca-9aaf65ed66d8]
org.mule.runtime.core.internal.processor.LoggerMessageProcessor: received payload
{ |
"Employees": [ |
{ |
"userId":
"rirani", |
"jobTitleName":
"Developer", |
"firstName":
"Romin", |
"lastName":
"Irani", |
"preferredFullName":
"Romin Irani", |
"employeeCode":
"E1", |
"region":
"CA", |
"phoneNumber":
"408-1234567", |
"emailAddress":
"romin.k.irani@gmail.com" |
}, |
{ |
"userId":
"nirani", |
"jobTitleName":
"Developer", |
"firstName":
"Neil", |
"lastName":
"Irani", |
"preferredFullName":
"Neil Irani", |
"employeeCode":
"E2", |
"region":
"CA", |
"phoneNumber":
"408-1111111", |
"emailAddress":
"neilrirani@gmail.com" |
}, |
{ |
"userId":
"thanks", |
"jobTitleName":
"Program Directory", |
"firstName":
"Tom", |
"lastName":
"Hanks", |
"preferredFullName":
"Tom Hanks", |
"employeeCode":
"E3", |
"region":
"CA", |
"phoneNumber":
"408-2222222", |
"emailAddress":
"tomhanks@gmail.com" |
} |
] |
} |
For Each Logger
First iteration
INFO 2020-04-13 12:29:20,316
[[MuleRuntime].cpuLight.15: [for-each-sample].for-each-sampleFlow.CPU_LITE
@4b51d624] [event: 4c7bad41-7d54-11ea-8bca-9aaf65ed66d8]
org.mule.runtime.core.internal.processor.LoggerMessageProcessor: within for
each { |
"userId":
"rirani", |
"jobTitleName":
"Developer", |
"firstName":
"Romin", |
"lastName":
"Irani", |
"preferredFullName":
"Romin Irani", |
"employeeCode":
"E1", |
"region":
"CA", |
"phoneNumber":
"408-1234567", |
"emailAddress":
"romin.k.irani@gmail.com" |
} |
Second iteration
INFO 2020-04-13 12:29:20,318
[[MuleRuntime].cpuLight.15: [for-each-sample].for-each-sampleFlow.CPU_LITE
@4b51d624] [event: 4c7bad41-7d54-11ea-8bca-9aaf65ed66d8]
org.mule.runtime.core.internal.processor.LoggerMessageProcessor: within for
each { |
"userId":
"nirani", |
"jobTitleName":
"Developer", |
"firstName":
"Neil", |
"lastName":
"Irani", |
"preferredFullName":
"Neil Irani", |
"employeeCode":
"E2", |
"region":
"CA", |
"phoneNumber":
"408-1111111", |
"emailAddress":
"neilrirani@gmail.com" |
} |
Third iteration
INFO 2020-04-13 12:29:20,319
[[MuleRuntime].cpuLight.15: [for-each-sample].for-each-sampleFlow.CPU_LITE
@4b51d624] [event: 4c7bad41-7d54-11ea-8bca-9aaf65ed66d8]
org.mule.runtime.core.internal.processor.LoggerMessageProcessor: within for
each { |
"userId":
"thanks", |
"jobTitleName":
"Program Directory", |
"firstName":
"Tom", |
"lastName":
"Hanks", |
"preferredFullName":
"Tom Hanks", |
"employeeCode":
"E3", |
"region":
"CA", |
"phoneNumber":
"408-2222222", |
"emailAddress":
"tomhanks@gmail.com" |
} |
Like the For-each scope, the Parallel For-each scope also splits the collection of messages into elements. But, unlike the For-each scope, it processes each element simultaneously in separate routes and the result is the collection of all messages aggregated in the same sequence they were before the split.

Number of routes is equal to the size of the collection and, unlike the scatter-gather, we cannot see these separate routes visually in the canvas.The collection configured in the Parallel For-each scope is [1,2,3,4]. The number of routes Mule runtime will create is 4.
How does the Parallel For-each work

Each arrow is a separate route inside the Parallel For-each scope and M is the Mule message. As shown in the diagram, the collection gets split inside the Parallel For-each scope and each route is processed simultaneously. The output is the collection of Mule messages.
Sometimes we may have a scenario in which we want the flow control to go to the next processor even in case of errors. This functionality can be achieved by using the Try scope and On-Error Continue processor.

Conclusion
Scopes in Mule 4 are essential tools for implementing advanced integration patterns. By understanding their features and use cases, you can design efficient, maintainable, and resilient applications.