error-handling - Camel ErrorHandler useOriginalMessage属性

标签 error-handling apache-camel

我已经使用Camel(当前的版本2.12.2)已有一年多了,而我的一个Web应用程序(数百条路由)遇到了一种有趣的情况。为了克服此问题,我想使用特殊的ErrorHandler,如果在特定的扩展路由中抛出异常,则返回原始主体。我整理了以下示例上下文来说明我想发生的事情:

 <?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/spring"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:util="http://www.springframework.org/schema/util"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
   http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
   http://www.springframework.org/schema/util  http://www.springframework.org/schema/util/spring-util.xsd">

   <!-- use for enriched routes so they pass the original to the global onException if exception is encountered -->
   <camel:errorHandler id='enrichmentErrorHandler' type="DefaultErrorHandler" useOriginalMessage="true"/>

   <camel:endpoint camelContextId="TestErrorHandlerContext" id="TestServletEndpoint" uri="servlet:///order"/>

   <bean id="testEx" class="java.io.IOException"/>

   <camelContext id="TestErrorHandlerContext" xmlns="http://camel.apache.org/schema/spring" xmlns:oas="http://www.verizonwireless.com/oas" trace="true">

      <onException>
         <exception>java.io.IOException</exception>
         <handled><constant>true</constant></handled>
         <log message="logging"/>
      </onException>

      <route id="startRoute">
          <from uri="TestServletEndpoint"/>

         <choice>
           <when>
             <simple>${body} == 'hello'</simple>

            <setBody><constant>world</constant></setBody>
            <!-- use new body -->
            <throwException ref="testEx"/>
           </when>
           <otherwise>
            <enrich uri="direct:enrichRoute"/>
           </otherwise>
         </choice>
       </route>

       <route id="enrichRoute" errorHandlerRef="enrichmentErrorHandler">
          <from uri="direct:enrichRoute"/>
           <setBody><constant>somethingElse</constant></setBody>
           <!-- use original body -->
           <throwException ref="testEx"/>
       </route>

    </camelContext>  
</beans>

当我发送“你好”时,我会返回“世界”->预期

当我发送“某物”时,我返回“sometingElse”->意外的

我的思想过程告诉我,richRoute应该使用中的原始正文,因为它引用了richmentErrorHandler。我的期望不正确怎么办?

我知道我可以使用路由级别的onException块来返回原始主体,但是如果我可以使错误处理程序引用正常工作,那将更加干净,因为我必须更改大约40个左右的路由定义。

最佳答案

我得出的结论是,路由级onException子句或doTry/doCatch是处理扩展程序后的异常的唯一方法。

关于error-handling - Camel ErrorHandler useOriginalMessage属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23346914/

相关文章:

r - sendmailR : how to handle occasional “Unknown SMTP code” errors

delphi - Delphi中GetLastError和FormatMessage的正确用法是什么?

c# - 如何在 C# (.NET 2.0) 中获取给定 TableAdapter 和 SqlDataAdapter 的 Sql 命令对象

apache-camel - Apache Camel : Accessing CamelLoopIndex

java - 如何在主线程中保持 apache Camel 上下文处于 Activity 状态

java - 野蝇上的 Camel CXF

java - Camel : pollEnrich and access to the Exchange

php - 自定义页面错误 500 错误

sql - 情况下的SQL RAISERROR

java - Apache Camel Multipart Netty Rest DSL 不适用于camel-jackson 依赖项