You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 36 Next »

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.

For introduction and background material see Error handling in Camel.

Camel 2.0 has a new default error handler

In Camel 2.0 onwards there default error handler is changed from Dead Letter Channel to DefaultErrorHandler. This error handler does not support a dead letter queue and will return exceptions back to the caller. This is what you expects when working with regular Java that exceptions will be thrown back to the caller. This error handler supports a limited set of features.

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.

The current implementations Camel provides out of the box are:

Non transacted

  • DefaultErrorHandler is the default error handler in Camel 2.0 onwards. This error handler does not support a deal letter queue, it will propagate exceptions back to the caller, as if there where no error handler at all. It has a limited set of features.
  • Dead Letter Channel which supports attempting to redeliver the message exchange a number of times before sending it to a dead letter endpoint
  • LoggingErrorHandler for just catching and logging exceptions
  • NoErrorHandler for no error handling

Transacted

  • TransactionErrorHandler is the default error handler in Camel 2.0 onwards for transacted routes. This error handler does not support a deal letter queue, it will propagate exceptions back to the caller, as if there where no error handler at all. It has a limited set of features.
  • Transaction for transactional error handling (Spring based). See the Transactional Client EIP pattern.

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

Transaction in Camel 1.x

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.

Short Summary of the provided Error Handlers

DefaultErrorHandler new in Camel 2.0

The DefaultErrorHandler is the new default error handler in Camel 2.0. It only supports a limited set of features such as catching exception and using the Exception Clause. It does not support redelivery, delays or the likes. It will propagate exceptions back to the original caller. It does not have any dead letter queue.

Dead Letter Channel

The Dead Letter Channel is the default error handler in Camel 1.x, 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:

Error formatting macro: snippet: java.lang.NullPointerException

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());

TransactionErrorHandler new in Camel 2.0

The TransactionErrorHandler is the new default error handler in Camel 2.0 for transacted routes. It only supports a limited set of features such as catching exception and using the Exception Clause. It does not support redelivery, delays or the likes. It will propagate exceptions back to the original caller. It does not have any dead letter queue.

If you have marked a route as transacted using the transacted DSL then Camel will automatic use a TransactionErrorHandler. It will try to lookup the global/per route configured error handler and use it if its a TransactionErrorHandlerBuilder instance. If not Camel will automatic create a temporary TransactionErrorHandler that overrules the default error handler. This is convention over configuration.

Scopes

The error handler is scoped as either

  • global
  • per route

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

Error formatting macro: snippet: java.lang.NullPointerException

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

Error formatting macro: snippet: java.lang.NullPointerException

Spring based configuration

Available as of Camel 1.4

Java DSL vs. Spring DSL

The error handler is configured a bit differently in Java DSL and Spring DSL. Spring DSL relies more on standard Spring bean configuration whereas Java DSL uses fluent builders.

In Camel 1.4 the error handler can be configured as a spring bean and scoped in:

  • global (the camelContext tag)
  • per route (the route tag)
  • or per policy (the policy/transacted 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.

Error formatting macro: snippet: java.lang.NullPointerException

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.

Error formatting macro: snippet: java.lang.NullPointerException

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

  • No labels