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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Why and When to Use GraphQL
  • Python Async/Sync: Understanding and Solving Blocking (Part 1)
  • Low-Level Optimizations in ClickHouse: Utilizing Branch Prediction and SIMD To Speed Up Query Execution
  • Mastering Concurrency: An In-Depth Guide to Java's ExecutorService

Trending

  • Multi-Agent Orchestration on AWS With AgentCore Runtime and A2A
  • Agentic Systems and Design Patterns
  • Building a Software Factory With AI Agents and Workflows
  • AI-Era Cybersecurity, Part 1: Mapping Glasswing, Daybreak, Lightwell and the Tools Racing to Find and Fix Bugs

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Mule Scatter-Gather Shallow Diving: Part I

Mule Scatter-Gather Shallow Diving: Part I

The Mule Scatter-Gather component can be used to solve typical message multicasting scenarios. The flow is pretty simple.

By 
Anupam Gogoi
·
Jan. 27, 17 · Opinion
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
5.1K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Let's assume that we have a common integration scenario. You are querying the price of an iPhone. The system should return you all possible prices of the iPhone, consulting various sites. This turns out to be a typical message multicasting scenario.

Mule Solution

To solve this kind of integration scenario, Mule provides an awesome component called Scatter-Gather. The most important points of this component are as follows.

Parallel Processing

The Scatter-Gather component can send messages to all the routes concurrently. It maintains a thread pool to concurrently execute the provided routes. Thus, the time needed to execute all routes is less, in contrast to its previous version, which executes the routes in a parallel fashion. This means that when a route is being processed the remaining routes must have to wait for its completion.

Error Handling

Each route executes in its own thread and thus, if an exception occurs, it does not prevent the execution of other routes that execute in their own threads. So, the final result may contain responses from the successful routes as well as from the failed ones. 

Configuration

By default, Scatter-Gather uses the default Mule implementation for aggregating messages. However, this behavior can be overridden by using the custom Java implementation. 

Getting Your Hands Dirty

In this post, I will try to explain the use of the Scatter-Gather component with a simple example.

The scenario is as follows: "Let's suppose we have two websites called "Wallmarty" and "Amazona" selling iPhones. Now, I want a list of the iPhones available in both sites in descending order of price."

Mock Services

I have created a mock service exposing the following two REST URLs:

  • http://localhost:9090/amazona?type=iphone.

  • http://localhost:9090/wallmarty?type=iphone.

Please download the JAR named market-0.0.1-SNAPSHOT.jar and execute it. Make sure that port 9090 is not being used on your machine. After running the service, you can verify the above-quoted links. 

Creating the Flow

Please find the complete source code here. I have created a simple flow named scatter-gather-example.xml .Here is a screenshot.

Flow Diagram

The flow is simple. The Scatter-Gather component simply gathers information from the sub-flows getWallmartyProductFlow and getAmazonaProductFlow and merges the information. The transformers simply transform the response based on some JSON schema and orders the output in descending order of price.

Simple Output

Here is the screenshot of a simple output.

[
  {
    "price": 185.5,
    "product_name": "IPhone6",
    "vendor": "www.wallmarty.com"
  },
  {
    "price": 186.5,
    "product_name": "IPhone5",
    "vendor": "www.wallmarty.com"
  },
  {
    "price": 187.5,
    "product_name": "IPhone6",
    "vendor": "www.amazona.com"
  },
  {
    "price": 188.5,
    "product_name": "IPhone6",
    "vendor": "www.amazona.com"
  },
  {
    "price": 188.5,
    "product_name": "IPhone5",
    "vendor": "www.amazona.com"
  },
  {
    "price": 188.5,
    "product_name": "IPhone6",
    "vendor": "www.amazona.com"
  },
  {
    "price": 189.5,
    "product_name": "IPhone6",
    "vendor": "www.wallmarty.com"
  },
  {
    "price": 189.5,
    "product_name": "IPhone6",
    "vendor": "www.wallmarty.com"
  },
  {
    "price": 189.5,
    "product_name": "IPhone6",
    "vendor": "www.amazona.com"
  },
  {
    "price": 190.5,
    "product_name": "IPhone5",
    "vendor": "www.wallmarty.com"
  },
  {
    "price": 190.5,
    "product_name": "IPhone6",
    "vendor": "www.wallmarty.com"
  },
  {
    "price": 190.5,
    "product_name": "IPhone6",
    "vendor": "www.amazona.com"
  },
  {
    "price": 191.5,
    "product_name": "IPhone5",
    "vendor": "www.wallmarty.com"
  },
  {
    "price": 191.5,
    "product_name": "IPhone5",
    "vendor": "www.amazona.com"
  },
  {
    "price": 194.5,
    "product_name": "IPhone6",
    "vendor": "www.wallmarty.com"
  },
  {
    "price": 194.5,
    "product_name": "IPhone5",
    "vendor": "www.wallmarty.com"
  },
  {
    "price": 194.5,
    "product_name": "IPhone6",
    "vendor": "www.amazona.com"
  },
  {
    "price": 194.5,
    "product_name": "IPhone5",
    "vendor": "www.amazona.com"
  },
  {
    "price": 450,
    "product_name": "Mac Pro",
    "vendor": "www.amazona.com"
  },
  {
    "price": 451,
    "product_name": "IPhone6",
    "vendor": "www.amazona.com"
  },
  {
    "price": 453,
    "product_name": "IPhone6",
    "vendor": "www.amazona.com"
  },
  {
    "price": 453,
    "product_name": "IPhone6",
    "vendor": "www.amazona.com"
  },
  {
    "price": 453,
    "product_name": "IPhone6",
    "vendor": "www.amazona.com"
  },
  {
    "price": 455,
    "product_name": "IPhone6",
    "vendor": "www.amazona.com"
  },
  {
    "price": 457,
    "product_name": "IPhone6",
    "vendor": "www.amazona.com"
  },
  {
    "price": 458,
    "product_name": "Mac Pro",
    "vendor": "www.amazona.com"
  },
  {
    "price": 458,
    "product_name": "Mac Pro",
    "vendor": "www.amazona.com"
  }
]

In the next post, I will try to explain the Scatter-Gather using custom aggregation strategy. 

Thread pool Flow (web browser) Implementation POST (HTTP) Integration Schema Execution (computing) Web Protocols

Opinions expressed by DZone contributors are their own.

Related

  • Why and When to Use GraphQL
  • Python Async/Sync: Understanding and Solving Blocking (Part 1)
  • Low-Level Optimizations in ClickHouse: Utilizing Branch Prediction and SIMD To Speed Up Query Execution
  • Mastering Concurrency: An In-Depth Guide to Java's ExecutorService

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook