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.
Using try ... catch ... finally
Related to error handling is the Try Catch Finally as DSL you can use directly in your route. Its basically a mimic of the regular try catch finally in the Java language but with more power.
The current implementations Camel provides out of the box are:
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
The Dead Letter Channel will redeliver at most 6 times using 1 second delay, and if the exchange failed it will be logged at
You can configure the default dead letter endpoint to use:or in Spring DSL:
or also from Camel 2.3.0:
Supported By The Following Error Handler
See Exception Clause documentation for documentation of some of the features above.
The error handler is scoped as either
- per route
The following example shows how you can register a global error handler (in this case using the logging handler)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
Spring Based Configuration
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.
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
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
myDeadLetterErrorHandlerthat is our Dead Letter Channel. This configuration is standard Spring using the bean element.
Finally, we have another spring bean for the redelivery policy where we can configure the options for how many times to redeliver, delays etc. From Camel 2.3.0, camel provides a customer bean configuration for the Error Handler, you can find the examples here.
TransactionalErrorHandler 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.
- Error handling in Camel for introduction and background material on error handling in Camel
- Dead Letter Channel for the dead letter error handler
- DefaultErrorHandler for the default error handler in Camel
- TransactionErrorHandler for the default error handler for transacted routes
- Transactional Client for transactional behavior
- Exception Clause as it supports handling thrown exceptions
- Try Catch Finally for try ... catch ... finally as DSL you can use in the routing