Difference between For Each and Parallel For Each Scope: Anypoint Studio (Mule 4)
- May 06, 2021
This article will help you differentiate between the For Each and Parallel For Each scopes available in Anypoint Studio. It’s important to know the difference when deciding which scope to use.
For Each Scope
Consider the following key points:
- It splits the payload and processes sequentially
- If an error occurs, it stops processing all together
- Returns the original payload after processing is complete
In a simple flow, we’ll get an HTTP request, log the initial payload, and then move into the For Each scope to process each record individually and synchronously. Finally, we’ll log the final payload.
The XML for this flow is shown below:
<flow name=”for-each-demoFlow” doc:id=”9c3bbf2a-4951-406d-9f2a-771d0d4bc8b4″ > <http:listener doc:name=”Listener” doc:id=”46570b94-5127-4ef0-b93f-7b320ce32a54″ config-ref=”HTTP_Listener_config” path=”/forEach” allowedMethods=”POST”/> <logger level=”INFO” doc:name=”payload” doc:id=”41a1864b-4902-4c71-ad71-d833697a9752″ message=”#[payload]”/> <foreach doc:name=”For Each” doc:id=”33f1a730-e551-4237-bba0-95ad92504a9a” > <ee:transform doc:name=”example” doc:id=”f507b205-2637-4df0-8ab9-b82304dfeac6″ > <ee:message > <ee:set-payload ><![CDATA[%dw 2.0 output application/json — payload ++ { “number”: 12345 }]]></ee:set-payload> </ee:message> </ee:transform> <logger level=”INFO” doc:name=”payload processed” doc:id=”f9f86d09-af2a-4a39-9456-6f16fee6ddb4″ message=”payload processed”/> </foreach> <logger level=”INFO” doc:name=”payload” doc:id=”01c47287-b4c7-44cc-a037-04788f92a1af” message=”#[payload]”/> </flow> |
Add a breakpoint after the Listener and debug the flow.
Once deployed, send a POST request from either Postman or an advanced REST client application to this app.
Here’s JSON data:
[ { “BillingStreet”: “111 Boulevard Hausmann”, “BillingCity”: “Paris”, “BillingCountry”: “France”, “BillingState”: “”, “Name”: “Dog Park Industries”, “BillingPostalCode”: 75008 }, { “BillingStreet”: “400 South St”, “BillingCity”: “San Francisco”, “BillingCountry”: “USA”, “BillingState”: “CA”, “Name”: “Iguana Park Industries”, “BillingPostalCode”: 91156 }, { “BillingStreet”: “777 North St”, “BillingCity”: “San Francisco”, “BillingCountry”: “USA”, “BillingState”: “CA”, “Name”: “Cat Park Industries”, “BillingPostalCode”: 91156 } ] |
Hit send and look at the initial payload. Then check how each record is processed one by one (synchronously).
Once the flow is complete, check the Logger. The original payload has been logged (without the changes we made using a Transform Message in for each scope).
The same can be seen in the Postman response referenced below.
Parallel For Each Scope
Consider the following key points:
- It splits the payload and processes parallelly
- If an error occurs, processing for other records continues and the error is clubbed with the final output
- Returns the modified payload after processing is complete; it’s in Java format
In a simple flow, we’ll get an HTTP request, log the initial payload, and then move into the Parallel For Each scope to process each record parallelly and convert the output to JSON. Finally, we’ll log the final payload.
The XML for this flow is shown below:
<flow name=”parallel-for-each-demoFlow” doc:id=”e53f5b2e-85a7-423d-b4c0-f1c90e03a7ed” > <http:listener doc:name=”Listener” doc:id=”b8d23dc0-08c3-42b1-872c-ba5a10f4694b” config-ref=”HTTP_Listener_config” path=”/parallelForEach” allowedMethods=”POST”/> <logger level=”INFO” doc:name=”payload” doc:id=”f689f728-fe30-4d8d-9a82-d57ed54d7759″ message=”#[payload]”/> <parallel-foreach doc:name=”Parallel For Each” doc:id=”fbefb024-95b4-42b6-bc88-f5c1c823cd67″ > <ee:transform doc:name=”example” doc:id=”8ae61850-3b84-477b-88cb-ca5a70de0676″> <ee:message> <ee:set-payload><![CDATA[%dw 2.0 output application/json — payload ++ { “number”: 12345 }]]></ee:set-payload> </ee:message> </ee:transform> <logger level=”INFO” doc:name=”payload processed” doc:id=”c56f8522-4cdb-42c8-b71d-7cb92952bbbc” message=”payload processed” /> </parallel-foreach> <ee:transform doc:name=”Transform Message” doc:id=”4c1528d5-76f2-41f1-9040-51246d315156″> <ee:message> <ee:set-payload><![CDATA[%dw 2.0 output application/json — payload]]></ee:set-payload> </ee:message> </ee:transform> <logger level=”INFO” doc:name=”payload” doc:id=”d90980f2-b08d-49cf-bbd3-0aea2fd2540f” message=”#[payload]”/> </flow> |
Add a breakpoint just after the Listener and debug the flow.
Once deployed, send a POST request from either Postman or an advanced REST client application to this app.
The request should look something like the input in the above example.
Hit Send in Postman and check the initial payload. Then, check how each record is processed together (parallelly). Finally, check the payload’s output type; it should be in Java format.
Resume the flow and check the payload logged on the console. The changes made in each scope should have been preserved.
You’ll see the same thing in the Postman response.
That’s about all on the differences between the For Each and Parallel For Each scopes. Hope this was helpful.
— By Sanket Kangle