【问题标题】:How to define exception to be thrown through ref in Apache Camel如何在 Apache Camel 中定义要通过 ref 抛出的异常
【发布时间】:2011-08-15 22:13:49
【问题描述】:

必须在我用 XML 定义的骆驼路线中抛出异常。发现 throwException 声明可从 Camel 2.3 获得,如下所示:

 <throwException ref="forced"></throwException>

但是,我不知道如何定义要抛出的forced 异常类。由于可以使用不同的异常消息多次抛出相同的异常 - 很高兴知道 throwException 是否有其他形式的定义,因此异常类和异常消息是就地定义的。

【问题讨论】:

    标签: apache-camel


    【解决方案1】:

    ref 只是对 a 的引用,所以你可以这样做

    <bean id="forced" class="java.lang.IllegalArgumentException">
       <constructor-arg index="0" value="This is forced"/>
    </bean>
    
    <camelContext ...>
      ...
    </camelContext>
    

    【讨论】:

    • 你也可以在camel-spring源码的src/test/java目录下查看在XML中的使用示例
    【解决方案2】:

    从 2.16.0 版本开始,有更优雅的方式来做,可选的异常消息:

    <route>
         <throwException exceptionType="java.lang.IllegalArgumentException" message="illegal argument found"/>
    </route>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-28
      • 1970-01-01
      • 2011-01-07
      • 2012-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多