Author: Sanket Kangle
This article will help you realize the difference between For Each and Parallel For Each scope available in Anypoint Studio. It is important to know that difference to make an appropriate decision regarding when to use which scope.
For Each Scope
Following are some key points regarding For Each scope
- splits payload and processes sequentially
- if an error occurs, stops processing all together
- original payload is returned after processing is completed
Let us see the same using an example now.
I have created a simple flow as bellow:
We will get an HTTP request, and we will log the initial payload, then move into the for each scope and process each record individually and synchronously. Finally, we are logging the final payload.
XML for this flow is as 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 just after the listener and debug the flow. Once deployed, send a POST request from the postman or advanced rest client application to this app. Your request should look something like below.
the JSON data passed is as below:
[ { “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 } ] |
And hit send
Look at the initial payload
Then check how synchronously each record is processed one by one.
And once the flow is completed, check the logger, the original payload is logged (without the changes that we did using transform message in for each scope)
The same can be seen in the Postman response:
Parallel For Each Scope
Following are some key points regarding Parallel For Each scope
- splits payload and processes parallelly
- if an error occurs, the processing is continued for other records, and the error is clubbed with the final output
- Modified payload is returned after processing is completed, and it is in Java format
Let us see the same using an example now.
I have created a simple flow as bellow:
We will get an HTTP request, we will log the initial payload then move into the for each scope and process each record parallelly. After coming out of parallel for each scope, we convert the output into JSON format. Finally, we are logging the final payload.
XML for this flow is as 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 the postman or advanced rest client application to this app. Your request should look something like below.
The input is given the same as the above example. Send hit in postman.
Check the initial payload, and move into the parallel for each scope. Check how parallelly each record is processed together.
Finally, check the output type of the payload once it comes out of parallel for each loop. It should be in Java format.
Resume the flow and check the payload logged on the console. The changes that we have done in the parallel for each scope are preserved.
The same can be seen in the postman response.
That was all about the difference between for each and parallel for each scope. Hope this was helpful.