Error Handler

http://camel.apache.org/error-handler.html

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.

Exception Clause
Using Error Handler combined with Exception Clause is a very powerful combination. We encourage end-users to use this combination in your error handling strategies. See samples and Exception Clause.
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:

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 exceptions
  • NoErrorHandler for no error handling
Transacted

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.

Dead Letter Channel

The Dead Letter Channel 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() {
        // using dead letter channel with a seda queue for errors
        errorHandler(deadLetterChannel("seda:errors"));

        // here is our route
        from("seda:a").to("seda:b");
    }
};

or in Spring DSL

<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

<camel:errorHandler id="deadLetterErrorHandler" type="DeadLetterChannel" deadLetterUri="log:dead">

<camel:camelContext errorHandlerRef="deadLetterErrorHandler">
  ...
</camel:camelContext>
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));

or in Spring DSL

<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

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

or in Spring DSL

<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

<camel:errorHandler id="noErrorHandler" type="NoErrorHandler"/>

<camel:camelContext errorHandlerRef="noErrorHandler">
  ...
</camel:camelContext>
TransactionErrorHandler

The TransactionErrorHandler is the default error handler in Camel for transacted routes.

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.

Features support by various Error Handlers

Here is a breakdown of which features is supported by the Error Handler(s):

See Exception Clause documentation for documentation of some of the features above.

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)

RouteBuilder builder = new RouteBuilder() {
    public void configure() {
        // use logging error handler
        errorHandler(loggingErrorHandler("com.mycompany.foo"));

        // here is our regular route
        from("seda:a").to("seda:b");
    }
};

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

RouteBuilder builder = new RouteBuilder() {
    public void configure() {
        // this route is using a nested logging error handler
        from("seda:a")
            // here we configure the logging error handler
            .errorHandler(loggingErrorHandler("com.mycompany.foo"))
            // and we continue with the routing here
            .to("seda:b");

        // this route will use the default error handler (DeadLetterChannel)
        from("seda:b").to("seda:c");
    }
};

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 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 xmlns="http://camel.apache.org/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="deadLetterUri" 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="redeliveryDelay" value="250"/>
   </bean>

From Camel 2.3.0, camel provides a customer bean configuration for the Error Handler, you can find the examples here.

<errorHandler id="loggingErrorHandler" type="LoggingErrorHandler" logName="foo" level="INFO" xmlns="http://camel.apache.org/schema/spring"/>

<!-- If don't specify type attribute, the type value will be set to DefaultErrorHandler -->
<errorHandler id="errorHandler" xmlns="http://camel.apache.org/schema/spring"/>

<!-- You can define the redeliveryPolicy inside of the errorHandler -->
<camel:errorHandler id="defaultErrorHandler" type="DefaultErrorHandler">
    <camel:redeliveryPolicy maximumRedeliveries="2" redeliveryDelay="0" logStackTrace="false"/>
</camel:errorHandler>

<camel:errorHandler id="deadLetterErrorHandler" type="DeadLetterChannel" deadLetterUri="log:dead">
    <camel:redeliveryPolicy maximumRedeliveries="2" redeliveryDelay="1000" logHandled="true" asyncDelayedRedelivery="true"/>
</camel:errorHandler>

<bean id="myErrorProcessor" class="org.apache.camel.spring.handler.MyErrorProcessor"/>

<!-- TX error handler can be configured using a template -->
<camel:errorHandler id="transactionErrorHandler" type="TransactionErrorHandler"
                    transactionTemplateRef="PROPAGATION_REQUIRED" onRedeliveryRef="myErrorProcessor"/>

<!-- or using a transaction manager -->
<camel:errorHandler id="txEH" type="TransactionErrorHandler" transactionManagerRef="txManager"/>

<!-- You can also define the errorHandler inside the camelContext -->
<camelContext errorHandlerRef="noErrorHandler" xmlns="http://camel.apache.org/schema/spring">
    <errorHandler id="noErrorHandler" type="NoErrorHandler"/>
</camelContext>

Using the transactional error handler

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.

See also

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值