DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Error Handling Inside Kumologica Subflow
  • Step-by-Step Guide to Use Anypoint MQ: Part 2
  • AI Agents For Automated Claims Processing
  • SmartXML: An Alternative to XPath for Complex XML Files

Trending

  • Performing and Managing Incremental Backups Using pg_basebackup in PostgreSQL 17
  • MySQL to PostgreSQL Database Migration: A Practical Case Study
  • Integration Isn’t a Task — It’s an Architectural Discipline
  • Beyond ChatGPT, AI Reasoning 2.0: Engineering AI Models With Human-Like Reasoning

Parallel Processing in Mule

In parallel processing, a list of actions is executed concurrently but independently. Learn to achieve this with Scatter-Gather in Mule in this tutorial.

By 
Baranee Manoharan user avatar
Baranee Manoharan
·
Updated Jun. 18, 18 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
25.2K Views

Join the DZone community and get the full member experience.

Join For Free

Parallel processing is a technique where a list of actions can be executed concurrently but independently and the parent flow will not continue until all actions are completed. This can be done in Mule using Scatter and Gather flow control. 

Consider an example: a flow should call 2 REST services concurrently and combine the result and return.

Background

We are going to use the below REST API in our flow, which returns a post per call and takes Post Id in the URI. Below is a screenshot from POSTMAN tool.

Image title

Code Sample

Start your flow with an HTTP Listener and configure it with 8081 port; following that, drag a Scatter-Gather Flow control. In each flow, get an HTTP connector like below to call a REST Service.

Image title

After the Scatter-Gather flow control, get a DataWeave Transform connector which does the actual transformation. Our transform shape would look like below. We could access the output of Scatter-Gather like an arraylist in the transformation.

Image title

Our flow would look like this:

Image title

Our successful run of this flow from POSTMAN would look like this:

Image title

Code View

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
    <http:request-config name="HTTP_Request_Configuration" host="jsonplaceholder.typicode.com" port="80" doc:name="HTTP Request Configuration"/>
    <flow name="scattergathersampleFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/samples" allowedMethods=" GET" doc:name="HTTP"/>
        <scatter-gather doc:name="Scatter-Gather">
            <http:request config-ref="HTTP_Request_Configuration" path="/posts/1" method="GET" doc:name="Get Post1"/>
            <http:request config-ref="HTTP_Request_Configuration" path="/posts/2" method="GET" doc:name="Get Post2"/>
        </scatter-gather>
        <dw:transform-message doc:name="Transform Message">
            <dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
flatten payload]]></dw:set-payload>
        </dw:transform-message>
    </flow>
</mule>

Scatter Gather is a costly operation because it uses multiple threads to complete the list of actions, but it executes the actions concurrently and independently. It will not complete until all the actions are completed in all the branches. 

Using the transform shape, we could create a single output from different types of message and we could access them like an ArrayList:

%dw 1.0
%output application/json
---
{
post1: payload[0].body,
post2: payload[1].body

}

Or we could just combine all the ResultSets using the below code snippet:

%dw 1.0
%output application/json
---
flatten payload


Handling exceptions inside scatter gather

if you have a direct call or a subflow inside the scatter gather your parent flow's exception handling will take effect. If you need to take care of the exceptions individually have a flow in the place of subflow like below.

Image title

In the "combine-all-3-responses" transform component now either I will receives each component's failure or successful payload. My each flow will be similar to this.

Image title

Flow (web browser) Processing

Opinions expressed by DZone contributors are their own.

Related

  • Error Handling Inside Kumologica Subflow
  • Step-by-Step Guide to Use Anypoint MQ: Part 2
  • AI Agents For Automated Claims Processing
  • SmartXML: An Alternative to XPath for Complex XML Files

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: