Over a million developers have joined DZone.

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

Learn how API management supports better integration in Achieving Enterprise Agility with Microservices and API Management, brought to you in partnership with 3scale

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!

Unleash the power of your APIs with future-proof API management - Create your account and start your free trial today, brought to you in partnership with 3scale.

mule esb ,mule studio ,mulesoft ,mule ,muleesb

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}