For Each Parallel Loop

 

For each parallel loop is similar to For each activity but only difference is that it’s iteration will be process by different thread. For each is sequential execution whereas For each parallel as name suggest is parallel execution.

For each parallel is still not available in Mule Pallet and if you want to use it then you have to add and update the XML tags manually.

To illustrate the difference between two – we will create a POC having both flow

In For Loop iteration we will perform two activities

  • Add a logger to print current payload
  • Wait for 5 seconds

For Each Parallel Loop

Dataweave expression

%dw 2.0
import * from dw::Runtime
output application/json
---
{
} wait 5000

 

For Each Parallel Loop

 

Last we add logger to complete the execution

For Each Parallel Loop

 

Similarly, we add the same activities in For Each Parallel flow

<parallel-foreach collection="#[payload.Employees]" xmlns="http://www.mulesoft.org/schema/mule/core">
		<logger level="INFO" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" doc:name="Logger" doc:id="02123f76-5ee6-4630-ae8a-097eaa1a3ae9" message="within for each #[payload]"/>
		<ee:transform xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" doc:name="wait" doc:id="17b9c033-89fe-419e-9b1f-b928710261a0" xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core">
			<ee:message>
				<ee:set-payload><![CDATA[%dw 2.0 import * from dw::Runtime output application/json --- { } wait 5000]]>
				</ee:set-payload>
			</ee:message>
		</ee:transform>
</parallel-foreach>

 

Now we will run and test both flow through SOAP UI

For Each Flow execution

  • Processing is sequential as we can see there is a 5 sec gap in each payload processing
  • In case of sequential processing, single thread will execute the whole For Each processing e.g. cpuLight.06
  • So overall flow took more than 15 secs to complete
INFO  2020-04-13 16:37:16,123 [[MuleRuntime].cpuLight.06: [for-each-sample].for-each-sampleFlow.CPU_LITE @5bd5a737] [event: ef3767a0-7d76-11ea-8fe4-9aaf65ed66d8] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: received payload {
  "Employees": [
    {
      "userId": "rirani"
    },
    {
      "userId": "nirani"
    },
    {
      "userId": "thanks"
    }
  ]
}
INFO  2020-04-13 16:37:16,126 [[MuleRuntime].cpuLight.06: [for-each-sample].for-each-sampleFlow.CPU_LITE @5bd5a737] [event: ef3767a0-7d76-11ea-8fe4-9aaf65ed66d8] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: within for each {
  "userId": "rirani"
}
INFO  2020-04-13 16:37:21,134 [[MuleRuntime].cpuIntensive.04: [for-each-sample].for-each-sampleFlow.CPU_INTENSIVE @3ecfc783] [event: ef3767a0-7d76-11ea-8fe4-9aaf65ed66d8] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: within for each {
  "userId": "nirani"
}
INFO  2020-04-13 16:37:26,145 [[MuleRuntime].cpuIntensive.05: [for-each-sample].for-each-sampleFlow.CPU_INTENSIVE @3ecfc783] [event: ef3767a0-7d76-11ea-8fe4-9aaf65ed66d8] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: within for each {
  "userId": "thanks"
}
INFO  2020-04-13 16:37:31,150 [[MuleRuntime].cpuIntensive.06: [for-each-sample].for-each-sampleFlow.CPU_INTENSIVE @3ecfc783] [event: ef3767a0-7d76-11ea-8fe4-9aaf65ed66d8] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: processing complete

 

For Each Parallel Execution

  • Processing is parallel as we can see there is no 5 sec gap in each payload processing
  • All For each iteration are processed by different threads e.g. cpuLight.16, cpuLight.15 and cpuLight.01
  • So overall it took less than 6 seconds to complete the processing
  • Loop will wait for completion of all different threads before exiting the for activity
INFO  2020-04-13 16:40:36,321 [[MuleRuntime].cpuLight.06: [for-each-sample].for-each-parallel-sampleFlow.CPU_LITE @6780c0db] [event: 668b0af0-7d77-11ea-8fe4-9aaf65ed66d8] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: received payload {
  "Employees": [
    {
      "userId": "rirani"
    },
    {
      "userId": "nirani"
    },
    {
      "userId": "thanks"
    }
  ]
}
INFO  2020-04-13 16:40:36,323 [[MuleRuntime].cpuLight.16: [for-each-sample].for-each-parallel-sampleFlow.CPU_LITE @6780c0db] [event: 668b0af0-7d77-11ea-8fe4-9aaf65ed66d8] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: within for each {
  "userId": "rirani"
}
INFO  2020-04-13 16:40:36,324 [[MuleRuntime].cpuLight.01: [for-each-sample].for-each-parallel-sampleFlow.CPU_LITE @6780c0db] [event: 668b0af0-7d77-11ea-8fe4-9aaf65ed66d8] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: within for each {
  "userId": "nirani"
}
INFO  2020-04-13 16:40:36,324 [[MuleRuntime].cpuLight.15: [for-each-sample].for-each-parallel-sampleFlow.CPU_LITE @6780c0db] [event: 668b0af0-7d77-11ea-8fe4-9aaf65ed66d8] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: within for each {
  "userId": "thanks"
}
INFO  2020-04-13 16:40:41,328 [[MuleRuntime].cpuIntensive.07: [for-each-sample].for-each-parallel-sampleFlow.CPU_INTENSIVE @76b8ab26] [event: 668b0af0-7d77-11ea-8fe4-9aaf65ed66d8] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: processing complete

 

Sample Application – for-each-parallel-sample

Sample SOAP UI project – For-Each-Parallel-Sample-soapui-project

 

 

  
Thank you for taking out time to read the above post. Hope you found it useful. In case of any questions, feel free to comment below. Also, if you are keen on knowing about a specific topic, happy to explore your recommendations as well.
 
For any latest updates or posts on our website, you can follow us on LinkedIn. Look forward to connecting with you there.


Share this:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments