The For Each scope splits a payload into elements and processes them one by one through the components that you place in the scope. It is similar to a for-each
/for
loop code block in most programming languages and can process any collection, including lists and arrays. The collection can be any supported content type, such as application/json
, application/java
, or application/xml
.
Following are some key points regarding For Each scope
<?xml version='1.0' encoding='UTF-8'?>
<sale>
<item itemId="2121" sale="31123">
<shipping>23231</shipping>
<desc>ok</desc>
<price>12</price>
<quantity>12</quantity>
</item>
<item itemId="3202" saleId="11111">
<shipping>11111</shipping>
<desc>ok</desc>
<price>08</price>
<quantity>08</quantity>
</item>
<item itemId="5635" saleId="99999">
<shipping>99999</shipping>
<desc>ok</desc>
<price>333</price>
<quantity>33</quantity>
</item>
</sale>
Add a logger which will print the received payload
Now add For Each activity
Where following parameters are passed
In our case – payload.sale will hold the collection
Add logger to print each payload
Run the application to see the results
First logger – will print the actual input payload
INFO 2021-12-29 12:10:12,090 [[MuleRuntime].uber.07: [foreach].foreachFlow.CPU_LITE @4af6527c] [processor: foreachFlow/processors/0; event: 2c52ff40-6872-11ec-a16b-00090faa0001] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: <?xml version='1.0' encoding='UTF-8'?>
<sale>
<item itemId="2121" sale="31123">
<shipping>23231</shipping>
<desc>ok</desc>
<price>12</price>
<quantity>12</quantity>
</item>
<item itemId="3202" saleId="11111">
<shipping>11111</shipping>
<desc>ok</desc>
<price>08</price>
<quantity>08</quantity>
</item>
<item itemId="5635" saleId="99999">
<shipping>99999</shipping>
<desc>ok</desc>
<price>333</price>
<quantity>33</quantity>
</item>
</sale>
INFO 2021-12-29 12:10:12,103 [[MuleRuntime].uber.07: [foreach].foreachFlow.CPU_LITE @4af6527c] [processor: foreachFlow/processors/1/processors/0; event: 2c52ff40-6872-11ec-a16b-00090faa0001] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: <?xml version='1.0' encoding='UTF-8'?>
<item itemId="2121" sale="31123">
<shipping>23231</shipping>
<desc>ok</desc>
<price>12</price>
<quantity>12</quantity>
</item>
For Each Logger (First iteration)
INFO 2021-12-29 12:10:12,103 [[MuleRuntime].uber.07: [foreach].foreachFlow.CPU_LITE @4af6527c] [processor: foreachFlow/processors/1/processors/0; event: 2c52ff40-6872-11ec-a16b-00090faa0001] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: <?xml version='1.0' encoding='UTF-8'?>
<item itemId="2121" sale="31123">
<shipping>23231</shipping>
<desc>ok</desc>
<price>12</price>
<quantity>12</quantity>
For Each Logger (Second iteration)
INFO 2021-12-29 12:10:12,111 [[MuleRuntime].uber.07: [foreach].foreachFlow.CPU_LITE @4af6527c] [processor: foreachFlow/processors/1/processors/0; event: 2c52ff40-6872-11ec-a16b-00090faa0001] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: <?xml version='1.0' encoding='UTF-8'?>
<item itemId="3202" saleId="11111">
<shipping>11111</shipping>
<desc>ok</desc>
<price>08</price>
<quantity>08</quantity>
</item>
For Each Logger (Third iteration)
INFO 2021-12-29 12:10:12,120 [[MuleRuntime].uber.07: [foreach].foreachFlow.CPU_LITE @4af6527c] [processor: foreachFlow/processors/1/processors/0; event: 2c52ff40-6872-11ec-a16b-00090faa0001] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: <?xml version='1.0' encoding='UTF-8'?>
<item itemId="5635" saleId="99999">
<shipping>99999</shipping>
<desc>ok</desc>
<price>333</price>
<quantity>33</quantity>
</item>
Use the Transform message in For Each Scope to convert XML to json into the Anypoint Studio Console.
If you like my post please follow me to read my latest post on programming and technology.
You are given a stream of elements that is too large to fit into memory.…
The formula for the area of a circle is given by πr². Use the Monte…
Given an integer k and a string s, write a function to determine the length…
There is a staircase with N steps, and you can ascend either 1 step or…
Build an autocomplete system that, given a query string s and a set of possible…
Design a job scheduler that accepts a function f and an integer n. The scheduler…