Apache Camel:<onexception>块在升级到camel 2.16.1之后不起作用

时间:2016-01-27 00:28:50

标签: apache-camel

在我升级到camel 2.14到camel 2.16.1之前,我有下面的onException块。在更新之前,我会将我的错误捕获并打印在日志中 - &#34;错误发布到MR&#34;。在我升级到camel 2.16.1之后,我仍然在日志中得到我的错误,但是现在,总是,我的处理错误之后是另一个时间戳,看起来是我认为我已经处理的错误的骆驼默认错误处理程序。它看起来像这样:

"2016-01-26 15:07:09,571 [Camel (mrPostContext) thread #56 - JmsConsumer[mrPost]] ERROR org.apache.camel.processor.DefaultErrorHandler - Failed delivery for (MessageId"....

我不知道自己做错了什么,我尝试使用java.lang.Throwable而不是java.lang.Exception,但到目前为止没有运气。 Haven在文档中找不到任何有用的东西。由于我对骆驼不太好,我会非常感谢你的帮助。

    <route id="mrPost">
        <from uri="activemq:mrPost?concurrentConsumers=8" />
        <onException>
            <exception>java.lang.Exception</exception>
            <redeliveryPolicy maximumRedeliveries="2" redeliveryDelay="1000"/>
            <handled>
                    <constant>true</constant>
            </handled>

            <to uri="activemq:mr-post-fail" />

            <log loggingLevel="ERROR" message="Error posting to MR body:${out.body} exception message: ${exception.message} body:${exception.responseBody}" />  
        </onException> 

        <setHeader headerName="CamelHttpMethod">
           <constant>PUT</constant>
        </setHeader>
        <recipientList>
            <simple>{{mr.source}}/${headers.id}</simple>
        </recipientList>

    </route>

4 个答案:

答案 0 :(得分:1)

如果有人正在寻找它,这是一个更清洁的解决方案:

<errorHandler id="loggingErrorHandler" type="LoggingErrorHandler" logName="LoggingErrorHandler" level="OFF"/>
<camelContext id="mrPostContext" trace="false" errorHandlerRef="loggingErrorHandler" >

答案 1 :(得分:0)

我认为camel失败的地方是将消息放入错误队列 mr-post-fail 并且在执行此操作时抛出异常,因此您的异常就是传播到DefaultExceptionHandler。由于日志记录位于put队列调用之后,因此无法按预期工作。

如果不是这样的话,你可以给出正在打印的错误的整行。

  "2016-01-26 15:07:09,571 [Camel (mrPostContext) thread #56 - JmsConsumer[mrPost]] ERROR org.apache.camel.processor.DefaultErrorHandler - Failed delivery for (MessageId"..

答案 2 :(得分:0)

我通过这两个调整得到了defaultErrorHandler:

 - set  messageHistory="false"  on CONTEXT
 - set  logStackTrace="false" for onException block, like this:
 <redeliveryPolicy maximumRedeliveries="2" redeliveryDelay="1000" logStackTrace="false" />

默认错误处理程序的时间戳仍然显示,但更容易看到它,它看起来像这样:

2016-02-01 11:26:27,792 [Camel (mrPostContext) thread #262 - JmsConsumer[mrPost]] ERROR org.apache.camel.processor.DefaultErrorHandler - Failed delivery for (MessageId: ID-qn7nwscms01-com-44939-1454025367155-1569-4 on ExchangeId: ID-qn7nwscms01-com-44939-1454025367155-1569-5). Exhausted after delivery attempt: 3 caught: org.apache.camel.http.common.HttpOperationFailedException: HTTP operation failed invoking http://mr.cloud.dig.com/api/contents/news/30939510 with statusCode: 409. Processed by failure processor: FatalFallbackErrorHandler[Pipeline[[Channel[SetHeader(CamelExceptionCaught, Simple: Headers: ${headers}  Message: ${exception.message}  Body: ${exception.responseBody})], Channel[sendTo(Endpoint[activemq://mr-post-fail])], Channel[choice{when Filter[if: Simple: ${exception.message} not contains "statusCode: 409" do: Pipeline[[Channel[SetHeader(Content-Type, text/plain)], Channel[SetHeader(to, Simple: lw@ac.com)], Channel[SetHeader(subject, Simple: Error posting to MR)], Channel[SetBody(Simple: ${header.CamelExceptionCaught})]]]]}], Channel[Log(mrPost)[Error posting to MR ID: ${headers.contentId}, type:${headers.cmsContentType}, modified:${headers.lastModifiedDate}, getTime:${headers.lastModifiedGetTime}]], Channel[Log(merlinPost)[Error posting to MR body:${out.body} exception message: ${exception.message} body:${exception.responseBody}]]]]]

我希望有办法完全关闭它。 我也尝试使用defaultErrorHandler设置:

<errorHandler id="defaultErrorHandler" type="DefaultErrorHandler">
    <redeliveryPolicy  logStackTrace="false"/>
</errorHandler>

但它没有任何影响。

答案 3 :(得分:0)

您可以使用此页面中的示例:http://camel.apache.org/exception-clause.html

<!-- setup our error handler as the deal letter channel -->
<bean id="errorHandler" class="org.apache.camel.builder.DeadLetterChannelBuilder">
    <property name="deadLetterUri" value="activemq:mr-post-fail"/>
</bean>

<!-- this is the camel context where we define the routes -->
<!-- define our error handler as a global error handler -->
<camelContext errorHandlerRef="errorHandler" xmlns="http://camel.apache.org/schema/spring">

  <route>
    <!-- the route -->
    <from uri="activemq:mrPost?concurrentConsumers=8" />
    <setHeader headerName="CamelHttpMethod">
           <constant>PUT</constant>
    </setHeader>
    <recipientList>
        <simple>{{mr.source}}/${headers.id}</simple>
    </recipientList>
  </route>

</camelContext>

或者,在rote中设置,错误句柄仅适用于特定路径

<route errorHandlerRef="errorHandler">
...
</route>

从camelContext中删除