java - Apache Camel onException

标签 java apache esb onexception

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

我添加这个 OnExeption :

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

然后,我创建了 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);
   }

}

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

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

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

谢谢。

最佳答案

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

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

希望对你有帮助

关于java - Apache Camel onException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14198043/

相关文章:

java - 运行基本 Spring Boot 应用程序时出现问题

Java多维数组

apache - Hadoop生态系统部署步骤

mule - mule 中的入站终点和出站终点究竟是什么?

java - Apache Camel - 失败的 FTP 组件

.net - NServiceBus 与 SQL Server 消息传输

java - 如何将 Web 应用程序中的集成小程序部署到 Tomcat 中

java - 使用java rmi的优缺点

c - apache 对 update/var/www 文件夹的写入权限

php - 如何在 XAMPP 上创建虚拟主机