Over a million developers have joined DZone.

Individual Activity Exceptions Inside the Scatter-Gather Scope

DZone's Guide to

Individual Activity Exceptions Inside the Scatter-Gather Scope

Learn how you can more easily manage your exceptions in Mule. All it takes is a bit of time, planning, and Java.

· Integration Zone ·
Free Resource

WSO2 is the only open source vendor to be named a leader in The Forrester Wave™: API Management Solutions, Q4 2018 Report. Download the report now or try out our product for free.

Ever wondered how to get hold of an individual activity exception inside a set of activities inside Scatter-Gather scope, as #[exception] would always return:


Consider the following scenario, where we have three activities in a Scatter-Gather scope (Groovy, HTTP, SFTP). Let's say we get errors in HTTP and SFTP and we would like to handle only HTTP errors (eg: "class org.mule.module.http.internal.request.ResponseValidatorException," in our case below) and send a custom error message to a queue. 

Scenario flow for example

We cannot directly use something like "exception.causedBy(class org.mule.module.http.
 to catch the HTTP-related exception. In the Execute When tab, the root exception class is "org.mule.routing.CompositeRoutingException."

Image title

The Workaround

We will create a Java class, ScatterGatherExceptionFilter.java, under the package com.util.exception inside the src/main/java mule folder.

See the Java snippet below:

package com.util.exception;

import java.util.Map;
import org.apache.log4j.Logger;
import org.mule.routing.CompositeRoutingException;

public class ScatterGatherExceptionFilter {

      private static final Logger logger = Logger.getLogger(ScatterGatherExceptionFilter.class);

      public static boolean checkExceptionClass(CompositeRoutingException exception){

            logger.info("Inside Java Class 'ScatterGatherExceptionFilter.java' ");
            boolean flag=false;

            for (Map.Entry<Integer, Throwable> routeException : exception.getExceptions().entrySet()) {
                  logger.info(String.format("An exception was found for route %d: ", routeException.getKey()), routeException.getValue());
                  Throwable cause = (Throwable) routeException.getValue();
                  logger.info("Cause= "+cause.getCause().toString());
                  String exceptionClass = cause.getCause().getClass().toString();
                 if(exceptionClass.equalsIgnoreCase("class org.mule.module.http.internal.request.ResponseValidatorException"))
                        {     flag = true;
            return flag;


Call this Java code in the Catch Exception Strategy block placed under Choice Exception Strategy,  as shown in the picture below.

So, execute when: 


Image title

The function checkExceptionClass in the Java code compares the HTTP exception with all child exception classes available in CompositeRoutingException and returns a Boolean flag with value as "true," if found in the list.

The Catch Exception Strategy block is executed only when the expression is evaluated to be true, and we can eventually handle our exception, say to send some error message through a JMS outbound activity.

Image title

Note:  We can also tweak the Java file as well the Expression inside Catch Exception Strategy (eg: to pass the exception class as an additional parameter to the Java class) as per our handling requirements.

Hope this helps. Thanks for reading!

IAM is now more than a security project. It’s an enabler for an integration agile enterprise. If you’re currently evaluating an identity solution or exploring IAM, join this webinar.

mule esb ,mule studio ,mulesoft ,mule ,muleesb

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}