如何通过引用在Apache Camel中定义要抛出的异常

18

我需要在XML定义的Camel路由中抛出异常。从Camel 2.3开始,可以使用throwException语句,如下所示:

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

然而,我不知道如何定义要抛出的forced异常类。由于相同的异常可能会多次抛出并带有不同的异常消息 - 如果throwException有其他形式的定义,以便在现场定义异常类和异常消息将是很好的。

2个回答

19

ref只是对一个组件的引用,这样你就可以进行操作:

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

<camelContext ...>
  ...
</camelContext>

2
您还可以查看camel-spring源代码中的src/test/java目录,以找到如何在XML中使用<throwException>的示例。 - Claus Ibsen

15

从2.16.0版本开始,有一种更优雅的方法来实现它,可以选择添加异常消息:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接