Java Reference
In-Depth Information
Solution
Use Spring Integration's support for an error channel, both implicit and explicitly via code. This solution
works only for solutions that employ channels whose messages are received out of the client's thread.
How It Works
Spring Integration provides the ability to catch exceptions and send them to an error channel of your
choosing. By default, it's a global channel called errorChannel . You can have components subscribe to
messages from this channel to override the exception handling behavior. You can create a class that will
be invoked whenever a message comes in on the errorChannel channel:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans=" http://www.springframework.org/schema/beans"
>
<context:annotation-config/>
<beans:bean id="defaultErrorHandlingServiceActivator"
class="com.apress.springenterpriserecipes.springintegration.DefaultError
HandlingServiceActivator"/>
<service-activator input-channel="errorChannel" ref="defaultErrorHandling
ServiceActivator"/>
</beans:beans>
The Java code is exactly as you'd expect it to be. Of course, the service-activator doesn't need
to be a service-activator . I just use it for convenience here. You could as easily send the error out on a
channel to anywhere you want. The code for the following service-activator depicts some of the
machinations you might go through to build a handler for the errorChannel :
package com.apress.springenterpriserecipes.springintegration;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.core.Message;
import org.springframework.integration.core.MessagingException;
public class DefaultErrorHandlingServiceActivator {
private static final Logger logger =
Logger.getLogger( DefaultErrorHandlingServiceActivator.class );
@ServiceActivator
public void handleThrowable(Message<Throwable> errorMessage) throws Throwable {
Throwable throwable = errorMessage.getPayload();
logger.debug(String.format("message: %s, stack trace :%s",
throwable.getMessage(),
ExceptionUtils.getFullStackTrace(throwable)));
if (throwable instanceof MessagingException) {
Message<?> failedMessage =
Search WWH ::




Custom Search