Apache的骆驼onException的 [英] Apache Camel onException

查看:816
本文介绍了Apache的骆驼onException的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要赶在所有航线例外。

I want to catch all Exception from routes.

我添加此OnExeption:

I add this OnExeption :

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

然后,我创建类MyFunctionFailureHandler。

Then, i create the class 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);
   }

}

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

Unfortunately, it doesn't work and i don't know why.

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

if there is an Exeption , the program must stop.

我怎么能知道为什么code不起作用!

How can i know why this code doesn't work !!

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

我希望它能帮助

这篇关于Apache的骆驼onException的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆