how to handle exception or fault in multiple routes

十年热恋 提交于 2019-12-22 18:16:50

问题


I have some problems when handle the exception between multiple routes.

As a java developer's perspective, I want to extract some common logic to a common route so that other routes can call the common route directly without containing the common logic everywhere.(like the route-version function call) But when it comes to the error handling, I found it's a little tricky.

For instance:

//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")

The problem is when some exception thrown from the "mock:commonlogic" endpoint, the exception won't be caught by doTry...doCatch blocks defined both in route1 and route2. It seems like the exception just can be handled in the common route scope. But what I want is the common route just 'throws out' the exception and the 'caller' routes handle it all by themselves. Is there any way to do that?

Thanks


回答1:


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")



回答2:


You might want to use the exception clause. http://camel.apache.org/exception-clause.html

Like this (in the route builder's configure method)

// 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")...;

It should do the trick for you.

The onException will be global for the current route builder.



来源:https://stackoverflow.com/questions/10731774/how-to-handle-exception-or-fault-in-multiple-routes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!