Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

Build APIs from SQL and NoSQL or Salesforce data sources in seconds. Read the Creating REST APIs white paper, brought to you in partnership with CA Technologies.

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:

"org.mule.routing.CompositeRoutingException."  

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.
internal.request.ResponseValidatorException)"
 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;
                              break;
                        }
            }
            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: 

 #[com.util.exception.ScatterGatherExceptionFilter.checkExceptionClass(exception)] 

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!

The Integration Zone is brought to you in partnership with CA Technologies.  Use CA Live API Creator to quickly create complete application backends, with secure APIs and robust application logic, in an easy to use interface.

Topics:
mule esb ,mule studio ,mulesoft ,mule ,muleesb

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

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

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}