Search code examples
muleanypoint-studiomulesoft

Mule app - call same APIs multiple times in parallel


In a Mule app (using Mule 4), I am trying to invoke a single API multiple times for an input array of Strings as this:

"input_arr": [
    "val1", "val2", "val3"
]

All the invocations can run in parallel as they are independent, but I want to wait and collate the results once they all complete. Also, if one or more result in errors, I want to obtain that as well.

I tried couple different ways: 1. Simple foreach -- not efficient since it is sequential. 2. Batch - it is async and the main flow does not wait.

What would be the best way to achieve this efficiently in Mulesoft?


Solution

  • If you are using Mule 4.2 + then parallel-foreach might achieve what you are looking for.

    The Parallel For Each scope enables you to process a collection of messages by splitting the collection into parts that are simultaneously processed in separate routes within the scope of any limitation configured for concurrent-processing.

    NOTE: However, because this feature is not available in the Anypoint Studio Mule Palette view, you must manually configure Parallel For Each scope in the XML.

    Also there are some differences other than concurrency with the new scope, so make sure to read the documentation: https://docs.mulesoft.com/mule-runtime/4.2/parallel-foreach-scope