ProstDev ProstDev
Guides Aug 6, 2020 · 2 min read

Parallel For-Each Scope (Mule 4)

Learn how to implement the new ForEach MuleSoft component that was released in Mule 4. Read this post for information about this integration technique: how does it work, how can you implement it, how to install in your Mule palette, and how to configure Error Handling for this scope.

By Vikalp Bhalia
Parallel For-Each Scope (Mule 4)

Processing a single element of a collection is a very common scenario and for that, we have the For-each scope and Batch processing in MuleSoft.

In this article, we will talk about a new scope that is called the Parallel For-each scope.

Parallel For-Each Scope

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.

Anypoint Studio flow with a Parallel For Each scope wrapping a Try with Is-number and Transform Message

Looking at the above picture you might be wondering, where are the separate routes? And how are these routes created?

The 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.

For example:

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.

Note

The processing of one element is invisible to other elements and creation or modification to an existing variable inside the scope is not visible outside the scope.

JSON output M1 showing one Mule message with payload number 1 and empty attribute arrays

Configuration

The configuration details are well documented in the MuleSoft documentation: https://docs.mulesoft.com/mule-runtime/4.3/parallel-foreach-scope#configuration

Error Handling

All routes continue processing even if there is an exception in one of the routes and then runtime executes the code in the error handler.

Note

The flow control will not go to the processor placed next to the Parallel For-each scope.

Diagram comparing success and error scenarios, where an error routes to the Error Handler

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.

Parallel For Each scope using a Try with an On Error Continue handler so flow reaches the next processor

Conclusion

If you want to execute elements of a collection simultaneously without using a batch processing, the Parallel For-each scope is another out of the box solution available in Mule 4.

References

https://docs.mulesoft.com/mule-runtime/4.3/parallel-foreach-scope

FAQs

Frequently asked questions about this post.

  • What's the difference between the For-each scope and the Parallel For-each scope?

    Both split a collection of messages into elements, but the For-each scope processes them one at a time while the Parallel For-each scope processes each element simultaneously in separate routes, then aggregates the result into a collection of all messages in the same sequence they were before the split.

  • How many routes does the Parallel For-each scope create?

    The number of routes is equal to the size of the collection, so a collection configured as [1,2,3,4] makes the Mule runtime create 4 routes. Unlike the scatter-gather, you cannot see these separate routes visually in the canvas.

  • Can a variable created inside the Parallel For-each scope be used outside the scope?

    No. The processing of one element is invisible to other elements, and creation or modification of an existing variable inside the scope is not visible outside the scope.

  • How does the Parallel For-each scope handle an error in one of the routes?

    All routes continue processing even if there is an exception in one of the routes, and then the runtime executes the code in the error handler. By default the flow control will not go to the processor placed next to the Parallel For-each scope.

  • How do I make the flow continue to the next processor even when a route fails?

    Wrap the processing in a Try scope with an On-Error Continue processor, which lets the flow control reach the processor placed next to the Parallel For-each scope even in case of errors.

Search

Loading search…