2

在处理多条路由之间的异常时,我遇到了一些问题。

作为一个java开发者的角度,我想把一些公共逻辑提取到一个公共路由中,这样其他路由就可以直接调用公共路由,而不用处处包含公共逻辑。(比如route-version函数调用)但是当涉及到错误处理,我发现它有点棘手。

例如:

//main logic 1
from("direct:route1")
  .doTry()
     .to("direct:common")
  .doCatch(Exception.class)
     .log("Error in route1")
  .end()

//main logic 2
from("direct:route2")
  .doTry()
     .to("direct:common")
  .doCatch(Exception.class)
     .log("Error in route2")
  .end()

//common logic
from("direct:common")
   .to("mock:commonlogic")

问题是当从“mock:commonlogic”端点抛出一些异常时,doTry 不会捕获异常...doCatch 块在 route1 和 route2 中都定义。似乎可以在公共路由范围内处理异常。但我想要的是通用路由只是“抛出”异常,而“调用者”路由自己处理它。有没有办法做到这一点?

谢谢

4

2 回答 2

4

You need to disable error handling in the common route.Then any exceptions thrown from the common route, is not handled by any error handler, and propagated back to the caller route, which has the try .. catch block.

from("direct:common")
   .errorHandler(noErrorHandler())
   .to("mock:commonlogic")
于 2012-05-25T04:17:05.293 回答
0

您可能想要使用异常子句。 http://camel.apache.org/exception-clause.html

像这样(在路由构建器的配置方法中)

// A common error handler for all exceptions. You could also write onException statements for explicit exception types to handle different errors different ways.
onException(Exception.class).to("log:something"); 

from("direct:route1")...;

from("direct:route2")...;

它应该为您解决问题。

onException 对于当前的路由构建器来说是全局的。

于 2012-05-24T10:13:07.200 回答