Apache Camel - 未捕获异常
Apache Camel - Exception is not catched
我阅读了相应的文档,但我无法弄清楚为什么我的异常没有被捕获。
这是我的路由配置:
<route id="foo">
<from uri="vm://.../>
<doTry>
<to uri="jetty:http://127.0.0.1:123/foo?restletMethod=PUT"/>
<to uri="ejb:java:global/..?method=method1(${body}, ${headers})"/>
<to uri="ejb:java:global/..?method=method2(${body}, ${headers})"/>
<doCatch>
<exception>java.lang.Exception</exception>
<transform>
<simple> ${exception.message} </simple>
</transform>
<to uri="smtp://... />
</doCatch>
</doTry>
</route>
现在当无法访问 JettyClient 时抛出 org.apache.camel.CamelExchangeException
,
路线终止,我收到一封电子邮件。这是期望的行为。
但是当 method1
抛出异常时,它没有被捕获,因此路由继续并且
我没有收到电子邮件。
如何让camel在第二种情况下也能识别并处理异常?
解决方法:确保异常没有在 try-catch 块中抛出-.-
这在文档中有说明:
http://camel.apache.org/try-catch-finally.html
Camel error handling is disabled
When using doTry .. doCatch .. doFinally then the regular Camel Error
Handler does not apply. That means any onException or the likes does
not trigger. The reason is that doTry .. doCatch .. doFinally is in
fact its own error handler and that it aims to mimic and work like how
try/catch/finally works in Java.
你最好这样写:
.doCatch(Exception.class)
// and catch all other exceptions
// they are handled by default (ie handled = true)
.to("direct:error")
在 direct:error 中,您可以指定在出现问题时要做什么。
我阅读了相应的文档,但我无法弄清楚为什么我的异常没有被捕获。
这是我的路由配置:
<route id="foo">
<from uri="vm://.../>
<doTry>
<to uri="jetty:http://127.0.0.1:123/foo?restletMethod=PUT"/>
<to uri="ejb:java:global/..?method=method1(${body}, ${headers})"/>
<to uri="ejb:java:global/..?method=method2(${body}, ${headers})"/>
<doCatch>
<exception>java.lang.Exception</exception>
<transform>
<simple> ${exception.message} </simple>
</transform>
<to uri="smtp://... />
</doCatch>
</doTry>
</route>
现在当无法访问 JettyClient 时抛出 org.apache.camel.CamelExchangeException
,
路线终止,我收到一封电子邮件。这是期望的行为。
但是当 method1
抛出异常时,它没有被捕获,因此路由继续并且
我没有收到电子邮件。
如何让camel在第二种情况下也能识别并处理异常?
解决方法:确保异常没有在 try-catch 块中抛出-.-
这在文档中有说明: http://camel.apache.org/try-catch-finally.html
Camel error handling is disabled
When using doTry .. doCatch .. doFinally then the regular Camel Error Handler does not apply. That means any onException or the likes does not trigger. The reason is that doTry .. doCatch .. doFinally is in fact its own error handler and that it aims to mimic and work like how try/catch/finally works in Java.
你最好这样写:
.doCatch(Exception.class)
// and catch all other exceptions
// they are handled by default (ie handled = true)
.to("direct:error")
在 direct:error 中,您可以指定在出现问题时要做什么。