Camel ErrorHandler useOriginalMessage属性

时间:2014-04-28 16:58:14

标签: error-handling apache-camel

我已经使用Camel(目前版本为2.12.2)已经有一年多的时间了,并且我的网络应用程序(几百条路线)遇到了一个有趣的情况。为了解决这个问题,我想使用一个特殊的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>

当我发送&#39; hello&#39;时,我会回到世界&#39; - &GT;预期

当我发送“&#39;”时,我会回过头来看看&#39; sometingElse&#39; - &GT;意想不到

我的思维过程告诉我,enricRoute应该使用原始主体,因为它引用了enrichmentErrorHandler。我的期望是不正确的?

我知道我可以使用路由级别的onException块来返回原始主体,但如果我能让错误处理程序引用工作,它会更清晰,因为我必须更改大约40个左右的路径定义

1 个答案:

答案 0 :(得分:0)

我得出的结论是路由级别的onException子句或doTry / doCatch是处理richr背后的异常的唯一方法。

相关问题