Multicasting of messages with Mulesoft can be achieved using Scatter-Gather, which concurrently casts messages to each route. It collects results from all the routes and aggregates them into a single message.
During Scatter-Gather processing, if any of the routes fail, it doesn't stop sending messages to other configured routes — so it is possible that one or more configured routes can fail concurrently.
By default, if any of the routers fail during Scatter-Gather processing, the following action will be performed:
Set an exception payload for each failed route.
MessagingException to aggregate exceptions from different routes in the context of a single message router. Exceptions are correlated to each route through a sequential ID.
Scatter-Gather applications will not start if you have configured with less than two routes. It should always contain two or more routes.
Let's walk through setting up Scatter-Gather with Mule application.
Setting Up HTTP Listener With Mule Flow
First, you need to place the HTTP listener to the source of Mule flow and configure it. You can invoke the Mule flow by sending a request over HTTP.
Configuring Scatter-Gather Route With Mule Flow
Now, you need to drop the Scatter-Gather route in the Message Processor of the Mule Flow. You need to send a message concurrently to multiple routes via HTTP, JMS, Web Service Consumer, File, etc. To achieve this, we will add different connectors under the Scatter-Gather route, place them in parallel, and configure all the connectors in the Scatter-Gather route.
In the above screenshot, you can see that
TransformMessage has been used after Web Service Consumer and HTTP Requestor. Basically, we are getting results back from both endpoints and we are transforming both responses to a single generic format as a response from both routes will be in a different format.
One more transformation has been placed at the end of the Scatter-Gather route. This transformation aggregates the result from all the routes to the single message.
Testing the Application
You can use Postman to test the application. We will use the HTTP
GET method to send a request to invoke the Mule flow.
Scatter-Gather is used to send messages to multiple destinations concurrently and aggregate the response from all into a single message. It is a very simple technique provided by Mulesoft to multicast messages.
I hope that this article clarifies all of the doubt that you may have with the Scatter-Gather route.
Here is the video tutorial: