Tag Archives: soa

Handling Exceptions in Mule

Proper exception handling is a requirement of any integration application. Handling errors with Mule seems to be challenging for a beginner. So here i present various ways of dealing with exceptions to the best of my knowledge.

Note: check the hello example that comes bundled with mule. The below example is based upon that with little bit more explanation

Problem:

Send a friendly error message back to the user in case of any business exceptions

Solution:

In request-response MEP (Message Exchange Pattern), the service provider has to send a meaningful error message back to the service consumer when a business exception occurs. The suggested way of dealing with this scenario is to use Exception based filtering (payload-type-filter) in Mule.

MEP: Request-Response

Flow: Idea is to expose the existing BusinessService class over http so that it can receive requests through browser. I have written a NEW BusinessServiceUMO class that acts as a mediator between the requestor and BusinessService. If the BusinessService class throws an exception, BusinessServiceUMO catches it and returns the exception as the NEW payload. I have also configured a payload type filter on the outbound endpoint of BusinessServiceUMO to route the exceptions to UserErrorHandler service. UserErrorHandler service just returns a message contained in the exception and Mule sends this message as a response to the user using the response transformer. All this will be pretty clear if we go through the mule-config.

Usage:

Important things to be noted w.r.t this example:

  • BusinessServiceUMO returing exception itself as the payload
  • synchronous=”true” on all the participating inbound endpoints enabling Request-Response MEP (so that the response is sent back to the caller)
  • use of payload-type-filter
  • use of responseTransformer-refs=”ExceptionToString” on the UserErrorHandler

Components:
BusinessService: Class implementing the business logic. This class can throw two kinds of exceptions: ValidationException and BusinessException. This class has no mule specific logic.

BusinessServiceUMO: receives input events from Mule and delegates the processing to

BusinessService.process(String req) method. This class has the logic specific to Mule. Look at how each of the exceptions are handled. For the payload-type-filter to work, the onEvent method should return the exception as the payload.


public Object onEvent(final Object req)
{
 Object payload = null;

 try
 {
  payload = service.process((String) req);
 }
 catch (BusinessException be)
 {
  payload = be;
 }
 catch (ValidationException ve)
 {
  payload = ve;
 }

 return payload;
}

UserErrorHandler: Important thing to note here is the responseTransformer-refs=”ExceptionToString”. All the transformer does is to return the message contained in the exception. Mule then takes this message and returns back to the browser as response

mule-config:


<spring:beans>
 <spring:bean id="businessService" class="com.aravind.mule.errorhandling.BusinessService" />
 <spring:bean id="businessUMO" class="com.aravind.mule.errorhandling.BusinessServiceUMO">
 <spring:property name="service" ref="businessService"></spring:property>
 </spring:bean>
</spring:beans>

<!-- Global Transformers -->
<custom-transformer name="HttpRequestToString" class="org.mule.example.hello.HttpRequestToNameString" />
<custom-transformer name="ExceptionToString" class="com.aravind.mule.errorhandling.ExceptionToString" />

<model name="my-service-model">
 <service name="BusinessService">
 <inbound>
 <http:inbound-endpoint address="http://localhost:9988"
 transformer-refs="HttpRequestToString" synchronous="true">
 <not-filter>
 <wildcard-filter pattern="/favicon.ico" />
 </not-filter>
 </http:inbound-endpoint>
 </inbound>
 <component>
 <spring-object bean="businessUMO" />
 </component>
 <outbound>
 <filtering-router>
 <vm:outbound-endpoint path="userErrorHandler" />
 <payload-type-filter expectedType="java.lang.Exception" />
 </filtering-router>
 </outbound>
 </service>

<!-- User error handling returns an error message to the end user -->
 <service name="UserErrorHandler">
 <inbound>
 <vm:inbound-endpoint path="userErrorHandler"
 responseTransformer-refs="ExceptionToString" synchronous="true" />
 </inbound>
 </service>
</model>

Dada Harir’s Well in Gujarat, 1880s.

— My Country, My People

Advertisements
Tagged , ,