Mulesoft has the capability to process a collection of objects in an iterative manner. This is useful for modifying records inside a collection. Processing a single element of a collection is a very common scenario and for that, we have the For-each scope and Batch processing in MuleSoft.
In this article, we will talk about a new scope that is called the Parallel For-each scope.
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. After all messages are processed, the results are aggregated following the same order they were in before the split, and then the flow continues.
Let us see the same using an example now. I have created a simple flow as bellow:
Create mule application the JSON collection as payload
[
{
"item": {
"shipping": "23231",
"desc": "ok",
"price": "12",
"quantity": "12"
}
},
{
"item": {
"shipping": "11111",
"desc": "ok",
"price": "08",
"quantity": "08"
}
},
{
"item": {
"shipping": "99999",
"desc": "ok",
"price": "333",
"quantity": "33"
}
}
]
Add logger to print each payload
Run the application to see the results
The payload enters the parallel for each scope then the scope will splits the message into individual elements. Then the scope sends each element to the processor at the same time in separate routers.
For example:
The collection configured in the Parallel For-each scope is [1,2,3,4]. The number of routes Mule runtime will create is 4.
**Each arrow is a separate route inside the Parallel For-each scope and M is the Mule message.
As shown in the diagram, the collection gets split inside the Parallel For-each scope and each route is processed simultaneously. The output is the collection of Mule messages.
Note: The processing of one element is invisible to other elements and creation or modification to an existing variable inside the scope is not visible outside the scope.
The final output would look like:
¬í sr java.util.ArrayListxÒ™Ça I sizexp w sr Rorg.mule.runtime.core.internal.message.DefaultMessageBuilder$MessageImplementationeJ蹨åÍ L
inboundMapt Ljava/util/Map;L outboundMapq ~ L typedAttributest *Lorg/mule/runtime/api/metadata/TypedValue;xpsr 3org.mule.runtime.api.util.CaseInsensitiveMapWrappervrD=wny3 L baseMapq ~ xpsr java.util.HashMapÚÁÃ`Ñ F
loadFactorI thresholdxp?@ w xsq ~ sq ~ ?@ w xsr (org.mule.runtime.api.metadata.TypedValueÐiÃÒš,ÞÉ L dataTypet (Lorg/mule/runtime/api/metadata/DataType;L valuet Ljava/lang/Object;xpsr 6org.mule.runtime.core.internal.metadata.SimpleDataTypeÀJdç~ð• Z
streamTypeL mimeTypet )Lorg/mule/runtime/api/metadata/MediaType;L typet Ljava/lang/Class;xp sr 'org.mule.runtime.api.metadata.MediaTypeͬU³qðS¯ Z definedInAppL paramsq ~ L primaryTypet Ljava/lang/String;L subTypeq ~ xpsr java.util.Collections$EmptyMapY6…ZÜçÐ xpt *q ~ pxvr java.lang.Object xppwÿÿÿÿÿÿÿÿxwk f{
"item": {
"shipping": "23231",
"desc": "ok",
"price": "12",
"quantity": "12"
}
}sq ~ sq ~ q ~ t applicationt jsont UTF-8xvr [B¬óøTà xpsr -org.mule.runtime.api.util.collection.SmallMap ¿¥£sì L delegatet 7Lorg/mule/runtime/api/util/collection/SmallMapDelegate;xpsr :org.mule.runtime.api.util.collection.EmptySmallMapDelegateÜǽ!“§²x xr 5org.mule.runtime.api.util.collection.SmallMapDelegateí3wí÷µ„ xpsq ~ #sq ~ &xsq ~ sq ~ sq ~ ?@ w xsq ~ sq ~ ?@ w xq ~ wk f{
"item": {
"shipping": "11111",
"desc": "ok",
"price": "08",
"quantity": "08"
}
}q ~ sq ~ #sq ~ &sq ~ #sq ~ &xsq ~ sq ~ sq ~ ?@ w xsq ~ sq ~ ?@ w xq ~ wl g{
"item": {
"shipping": "99999",
"desc": "ok",
"price": "333",
"quantity": "33"
}
}q ~ sq ~ #sq ~ &sq ~ #sq ~ &xsq ~ sq ~ sq ~ ?@ w xsq ~ sq ~ ?@ w xq ~ wm h{
"item": {
"shipping": "11000",
"desc": "ok",
"price": "3000",
"quantity": "54"
}
}q ~ sq ~ #sq ~ &sq ~ #sq ~ &xx
If you want to retrieve only the payload then you can simply use flatten to merge the payload as shown below:
%dw 2.0
output application/json
flatten(payload..payload)
The output would look like:
[
{
"item": {
"shipping": "23231",
"desc": "ok",
"price": "12",
"quantity": "12"
}
},
{
"item": {
"shipping": "11111",
"desc": "ok",
"price": "08",
"quantity": "08"
}
},
{
"item": {
"shipping": "99999",
"desc": "ok",
"price": "333",
"quantity": "33"
}
},
{
"item": {
"shipping": "11000",
"desc": "ok",
"price": "3000",
"quantity": "54"
}
}
]
Important Points:
If you like my post please follow me to read my latest post on programming and technology.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…