The errorHandler()
clause provides similar features to the
onException
clause, except that this mechanism is not
able to discriminate between different exception types. The errorHandler()
clause is the original exception handling mechanism provided by Fuse Mediation Router and was available
before the onException
clause was implemented.
The errorHandler()
clause is defined in a RouteBuilder
class
and applies to all of the routes in that RouteBuilder
class. It is triggered
whenever an exception of any kind occurs in one of the applicable
routes. For example, to define an error handler that routes all failed exchanges to the
ActiveMQ deadLetter
queue, you can define a RouteBuilder
as
follows:
public class MyRouteBuilder extends RouteBuilder { public void configure() { errorHandler(deadLetterChannel("activemq:deadLetter")); // The preceding error handler applies // to all of the following routes: from("activemq:orderQueue") .to("pop3://[email protected]"); from("file:src/data?noop=true") .to("file:target/messages"); // ... } }
Redirection to the dead letter channel will not occur, however, until all attempts at redelivery have been exhausted.
In the XML DSL, you define an error handler within a camelContext
scope
using the errorHandler
element. For example, to define an error handler that
routes all failed exchanges to the ActiveMQ deadLetter
queue, you can define
an errorHandler
element as follows:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:camel="http://camel.apache.org/schema/spring" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> <camelContext xmlns="http://camel.apache.org/schema/spring"> <errorHandler type="DeadLetterChannel" deadLetterUri="activemq:deadLetter"/> <route> <from uri="activemq:orderQueue"/> <to uri="pop3://[email protected]"/> </route> <route> <from uri="file:src/data?noop=true"/> <to uri="file:target/messages"/> </route> </camelContext> </beans>
Table 2.1 provides an overview of the different types of error handler you can define.
Table 2.1. Error Handler Types
Java DSL Builder | XML DSL Type Attribute | Description |
---|---|---|
defaultErrorHandler() | DefaultErrorHandler | Propagates exceptions back to the caller and supports the redelivery policy, but it does not support a dead letter queue. |
deadLetterChannel() | DeadLetterChannel | Supports the same features as the default error handler and, in addition, supports a dead letter queue. |
loggingErrorChannel() | LoggingErrorChannel | Logs the exception text whenever an exception occurs. |
noErrorHandler() | NoErrorHandler | Dummy handler implementation that can be used to disable the error handler. |
TransactionErrorHandler | An error handler for transacted routes. A default transaction error handler instance is automatically used for a route that is marked as transacted. |