Error Handler has been edited by Claus Ibsen (Sep 08, 2008).

(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.

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

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.
See Transactional Client for examples.

Default Error Handler

The default error handler is the Dead Letter Channel which is automatically configured for you. You can then configure the specific dead letter endpoint to use either for an entire rule base or a specific rule as shown above. For example

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

Overriding default behavior

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");
    }
};

As of Camel 1.4 you can configure the ExceptionPolicyStrategy as this example shows

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

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

    exception(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

Reply via email to