...
The current implementations Camel provides out of the box are:
Non-transacted:
- DefaultErrorHandler is the default error handler in Camel. This error handler does not support a dead 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 exceptionsNoErrorHandler
for no error handling
Transacted:
- TransactionErrorHandler is the default error handler in Camel for transacted routes. 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
Short Summary of the
...
Provided Error Handlers
DefaultErrorHandler
The DefaultErrorHandler is the default error handler in Camel. Unlike Dead Letter Channel it does not have any dead letter queue, and do not handle exceptions by default.
...
The Dead Letter Channel will redeliver at most 6 times using 1 second delay, and if the exchange failed it will be logged at at ERROR
level.
You can configure the default dead letter endpoint to use:
Wiki Markup |
---|
{snippet:id=e3|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/builder/ErrorHandlerTest.java} |
Code Block | ||
---|---|---|
| ||
<bean id="deadLetterErrorHandler" class="org.apache.camel.builder.DeadLetterChannelBuilder"> <property name="deadLetterUri" value="log:dead"/> </bean> <camelContext errorHandlerRef="deadLetterErrorHandler" xmlns="http://camel.apache.org/schema/spring"> <!-- ... --> </camelContext> |
or also from Camel 2.3.0 onwards:
Code Block | ||
---|---|---|
| ||
<camel:errorHandler id="deadLetterErrorHandler" type="DeadLetterChannel" deadLetterUri="log:dead"> <camel:camelContext errorHandlerRef="deadLetterErrorHandler"> ... </camel:camelContext> |
...
The LoggingErrorHandler
The logging error handler will log (by default at at ERROR
level) whenever an uncaught exception is thrown. The logging category, logger and level may all be defined in the builder.
Code Block | ||
---|---|---|
| ||
errorHandler(loggingErrorHandler("mylogger.name").level(LoggingLevel.INFO)); |
or in Spring DSL:
Code Block | ||
---|---|---|
| ||
<bean id="loggingErrorHandler" class="org.apache.camel.builder.LoggingErrorHandler"> <property name="logName" value="mylogger.name"/> <property name="level" value="DEBUG"/> </bean> <camelContext errorHandlerRef="loggingErrorHandler" xmlns="http://camel.apache.org/schema/spring"> ... </camelContext> |
or also from Camel 2.3.0 onwards:
Code Block | ||
---|---|---|
| ||
<camel:errorHandler id="loggingErrorHandler" type="LoggingErrorHandler" logName="mylogger.name" level="DEBUG"/> <camel:camelContext errorHandlerRef="loggingErrorHandler"> ... </camel:camelContext> |
This would create an error handler which logs exceptions using the category category mylogger.name
and uses the level level INFO
for all log messages created.
Code Block | ||
---|---|---|
| ||
from("seda:a") .errorHandler(loggingErrorHandler("mylogger.name").level(LoggingLevel.DEBUG)) .to("seda:b"); |
Loggers may also be defined for specific routes.
...
The NoErrorHandler
The no error handler is to be used for disabling error handling.
Code Block | ||
---|---|---|
| ||
errorHandler(noErrorHandler()); |
or in Spring DSL:
Code Block | ||
---|---|---|
| ||
<bean id="noErrorHandler" class="org.apache.camel.builder.NoErrorHandlerBuilder"/> <camelContext errorHandlerRef="noErrorHandler" xmlns="http://camel.apache.org/schema/spring"> ... </camelContext> |
or also from Camel 2.3.0 onwards:
Code Block | ||
---|---|---|
| ||
<camel:errorHandler id="noErrorHandler" type="NoErrorHandler"/> <camel:camelContext errorHandlerRef="noErrorHandler"> ... </camel:camelContext> |
...
Tip | ||||
---|---|---|---|---|
| ||||
If you have marked a route as transacted using the |
Features
...
Support by
...
Various Error Handlers
Here is a breakdown of which features is supported by the Error Handler(s):
...
The following example shows how you can register a global error handler (in this case using the logging handler)
Wiki Markup |
---|
{snippet:id=e1|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/builder/ErrorHandlerTest.java} |
seda:a
Wiki Markup |
---|
{snippet:id=e2|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/builder/ErrorHandlerTest.java} |
Spring
...
Based Configuration
Info | ||
---|---|---|
| ||
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 is configured with the errorHandlerRef
attribute.
Tip | ||
---|---|---|
| ||
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 to myDeadLetterErrorHandler
using the errorHandlerRef
attribute on the route
tag.
Wiki Markup |
---|
{snippet:id=e1|lang=xml|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/DeadLetterChannelRedeliveryConfigTest-context.xml} |
myDeadLetterErrorHandler
that is our Dead Letter Channel. This configuration is standard Spring using the bean element.And finally Finally, we have another spring bean for the redelivery policy where we can configure the options for how many times to redeliver, delays etc.
Wiki Markup |
---|
{snippet:id=e2|lang=xml|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/DeadLetterChannelRedeliveryConfigTest-context.xml} |
Wiki Markup |
---|
{snippet:id=example|lang=xml|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/handler/ErrorHandlerDefinitionParser.xml} |
Using
...
the TransactionalErrorHandler
The TransactionalErrorHandler
The transactional error handler 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.
...