Error Handler has been edited by Claus Ibsen (Nov 20, 2008).

Change summary:

CAMEL-1105: RedeliveryPolicy now supports ref

(View changes)

Content:

Error Handler

Camel supports pluggable ErrorHandler strategies to deal with errors processing an Event Driven Consumer. An alternative is to specify the error handling directly in the DSL using the Exception Clause.

Some current implementations include

Transaction

If the route is transactional then the Dead Letter Channel is disabled. The exchange.isTransacted() is used to determine if an Exchange is transacted or not.
So if you are using transacted routes then you should configure the TransactionErrorHandler instread of DeadLetterChannel. See Transactional Client for further details and samples.

Exception Clause

Using Error Handler combined with Exception Clause is a very powerful ally. We encourage end-users to use this combination in your error handling strategies. See samples and Exception Clause.

These error handlers can be applied in the DSL to an entire set of rules or a specific routing rule as we show in the next examples. Error handling rules are inherited on each routing rule within a single RouteBuilder

Short Summary of the provided Error Handlers

Default Error Handler (Dead Letter Channel)

The default error handler is the Dead Letter Channel which is automatically configured for you. By default Camel will redeliver at most 6 times using 1 second delay, and if the exchange failed it will be logged at ERROR level.

You can configure the default dead letter endpoint to use:

RouteBuilder builder = new RouteBuilder() {
    public void configure() {
        errorHandler(deadLetterChannel("seda:errors"));
        from("seda:a").to("seda:b");
    }
};

Logging Error Handler

The logging error handler will log (by default at ERROR level) whenever an uncaught exception is thrown. The logging category, logger and level may all be defined in the builder.

errorHandler(loggingErrorHandler("mylogger.name").level(LoggingLevel.INFO));

This would create an error handler which logs exceptions using the category mylogger.name and uses the level INFO for all log messages created.

from("seda:a").errorHandler(loggingErrorHandler("mylogger.name").level(LoggingLevel.DEBUG)).to("seda:b");

Loggers may also be defined for specific routes.

No Error Handler

The no error handler is to be used for disabling error handling.

errorHandler(noErrorHandler());

Setting global error handlers

The following example shows how you can register a global error handler (in this case using the logging handler)

RouteBuilder builder = new RouteBuilder() {
    public void configure() {
        errorHandler(loggingErrorHandler("FOO.BAR"));
        from("seda:a").to("seda:b");
    }
};

Setting error handlers on a specific route

The following example shows how you can register a local error handler; the customized logging handler is only registered for the route from Endpoint seda:a

RouteBuilder builder = new RouteBuilder() {
    public void configure() {
        from("seda:a").errorHandler(loggingErrorHandler("FOO.BAR")).to("seda:b");
        // this route will use the default error handler,
        // DeadLetterChannel
        from("seda:b").to("seda:c");
    }
};

Spring based configuration

In Camel 1.4 the error handler can be configured as a spring bean and referenced as either:

  • global (the camelContext tag)
  • per route (the route tag)
  • per policy (the policy tag)

The error handler is configured with the errorHandlerRef attribute.

Error Handler Hierarchy

The error handlers is inherited, so if you only have set a global error handler then its use everywhere. But you can override this in a route and use another error handler.

Spring based configuration sample

In this sample we configure a Dead Letter Channel on the route that should redeliver at most 3 times and use a little delay before retrying.
First we configure the reference to myDeadLetterErrorHandler using the errorHandlerRef attribute on the route tag.

<camelContext id="camel" xmlns="http://activemq.apache.org/camel/schema/spring">
      <template id="myTemplate"/>
<!-- set the errorHandlerRef to our DeadLetterChannel, this applies for this route only -->
      <route errorHandlerRef="myDeadLetterErrorHandler">
          <from uri="direct:in"/>
          <process ref="myFailureProcessor"/>
          <to uri="mock:result"/>
      </route>
  </camelContext>

Then we configure myDeadLetterErrorHandler that is our Dead Letter Channel. This configuration is standard Spring using the bean element.
And finally we have another spring bean for the redelivery policy where we can configure the options for how many times to redeliver, delays etc.

<!-- here we configure our DeadLetterChannel -->
<bean id="myDeadLetterErrorHandler" class="org.apache.camel.builder.DeadLetterChannelBuilder">
    <!-- exchanges is routed to mock:dead in cased redelivery failed -->
       <property name="defaultDeadLetterEndpointUri" value="mock:dead"/>
	<!-- reference the redelivery policy to use -->
       <property name="redeliveryPolicy" ref="myRedeliveryPolicyConfig"/>
   </bean>

   <!-- here we set the redelivery settings -->
<bean id="myRedeliveryPolicyConfig" class="org.apache.camel.processor.RedeliveryPolicy">
    <!-- try redelivery at most 3 times, after that the exchange is dead and its routed to the mock:dead endpoint -->
       <property name="maximumRedeliveries" value="3"/>
	<!-- delay 250ms before redelivery -->
       <property name="delay" value="250"/>
   </bean>

Redelivery Policy

You can also configure the RedeliveryPolicy as this example shows

RouteBuilder builder = new RouteBuilder() {
    public void configure() {
        errorHandler(deadLetterChannel("seda:errors").maximumRedeliveries(2).useExponentialBackOff());
        from("seda:a").to("seda:b");
    }
};

And configuration of the redelivery policy in Spring DSL is as:

<onException>
    <exception>java.io.IOException</exception>
    <redeliveryPolicy 
        delay="1000"
        maximumRedeliveries="3"
        maximumRedeliveryDelay="10000"
        backOffMultiplier="1.0"
        useExponentialBackOff="true"
        retryAttemptedLogLevel="TRACE"
        retriesExhaustedLogLevel="FATAL"               
    />
    <setHeader headerName="messageInfo">
      <constant>Damm somekind of IO exception</constant>
    </setHeader>
    <to uri="mock:error"/>
</onException>

Available as of Camel 1.5.1 or later
You can reference a RedeliveryPolicy so you can reuse existing configurations and use standard spring bean style configuration that supports property placeholders.

<bean id="myRedeliveryPolicy" class="org.apache.camel.processor.RedeliveryPolicy">
        <property name="maximumRedeliveries" value="${myprop.max}"/>
    </bean>

     <onException>
         <!-- you can define multiple exceptions just adding more exception elements as show below -->
         <exception>com.mycompany.MyFirstException</exception>
         <exception>com.mycompany.MySecondException</exception>

         <!-- here we reference our redelivy policy defined above -->
         <redeliveryPolicy ref="myRedeliveryPolicy"/>
     </onException>

Exception Policy Strategy

As of Camel 1.4 you can configure the ExceptionPolicyStrategy as this example shows. Notice that we use Exception Clause to handle known exceptions being thrown.

public void configure() throws Exception {
    // configure the error handler to use my policy instead of the default from Camel
    errorHandler(deadLetterChannel().exceptionPolicyStrategy(new MyPolicy()));

    onException(MyPolicyException.class)
        .maximumRedeliveries(1)
        .setHeader(MESSAGE_INFO, constant("Damm my policy exception"))
        .to(ERROR_QUEUE);

    onException(CamelException.class)
        .maximumRedeliveries(3)
        .setHeader(MESSAGE_INFO, constant("Damm a Camel exception"))
        .to(ERROR_QUEUE);

Using our own strategy MyPolicy we can change the default behavior of Camel with our own code to resolve which ExceptionType from above should be handling the given thrown exception.

public static class MyPolicy implements ExceptionPolicyStrategy {

    public ExceptionType getExceptionPolicy(Map<Class, ExceptionType> exceptionPolicices,
                                            Exchange exchange,
                                            Throwable exception) {
        // This is just an example that always forces the exception type configured
        // with MyPolicyException to win.
        return exceptionPolicices.get(MyPolicyException.class);
    }
}

Using the transactional error handler

The transactional error handler is introduced in Camel 1.4 and is based on spring transaction. This requires the usage of the camel-spring component.
See Transactional Client that has many samples for how to use and transactional behavior and configuration with this error handler.

See also

The Dead Letter Channel for further details.
The Transactional Client for transactional behavior
The Exception Clause as it supports handling thrown exceptions

Reply via email to