Apache骆驼onException

我想从路线中捕获所有exception。

我添加这个OnExeption:

onException(Exception.class).process(new MyFunctionFailureHandler()).stop(); 

然后,我创build类MyFunctionFailureHandler。

 public class MyFunctionFailureHandler implements Processor { @Override public void process(Exchange exchange) throws Exception { Throwable caused; caused = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Throwable.class); exchange.getContext().createProducerTemplate().send("mock:myerror", exchange); } } 

不幸的是,它不工作,我不知道为什么。

如果有一个执行,程序必须停止。

我怎么知道为什么这个代码不工作!

thxs。

我用我的路线:

 public class MyCamelRoute extends RouteBuilder { @Override public void configure() throws Exception { from("jms:start") .process(testExcpProcessor) // -- Handle Exceptions .onException(Exception.class) .process(errorProcessor) .handled(true) .to("jms:start"); } } 

并在我的errorProcessor

 public class ErrorProcessor implements Processor { @Override public void process(Exchange exchange) throws Exception { Exception cause = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class); if(cause != null){ log.error("Error has occurred: ", cause); // Sending Error message to client exchange.getOut().setBody("Error"); }else // Sending response message to client exchange.getOut().setBody("Good"); } } 

我希望它有帮助

请记住,如果您有多个RouteBuilder类中的路由,则在此路由中使用onExcpetion将仅影响此RouteBuilder下的所有路由

审查这个答案

此外,如果在路由中发生异常并在此路由中处理,它将不会传播到原始调用者路由,您需要使用noErrorHandler()(即from(direct:start).errorHandler(noErrorHandler())传播异常处理回到调用者