Apache Camel onException

2024-02-07

我想捕获路由中的所有异常。

我添加这个 OnException :

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

然后,我创建 MyFunction FailureHandler 类。

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);
   }

}

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

如果出现异常,程序必须停止。

我怎么知道为什么这段代码不起作用!!

Thanks.


我在我的路线上使用了这个:

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

并在我的错误处理器

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

我希望它有帮助

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Apache Camel onException 的相关文章

随机推荐