Categories: Mule 4Mulesoft

Parallel For-Each Scope in MuleSoft (Mule 4)

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.

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:

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.

How does the Parallel For-each work?

**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:

  • Parallel For Each buffers all processing routes’ results in a list to return it after the scope finishes processing, which can cause out-of-memory errors when processing a high number of entries. To process large payloads, use Batch Processing instead.
  • Anypoint Studio versions prior to 7.6 do not provide this feature in the Mule Palette view. To use Parallel for Each in those versions, you must manually configure Parallel For Each scope in the XML.
  • Creation or modification of an existing variable inside the scope is not accessible outside.
Follow Me

If you like my post please follow me to read my latest post on programming and technology.

Instagram

Facebook

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago