简体   繁体   English

Apache-camel-自定义错误处理

[英]Apache-camel - custom error handling

Is possible to implement some switch before error handler of apache-camel? 可以在apache-camel的错误处理程序之前实现一些切换吗?

Something like: If it is MyException.class, then use default error handler otherwise use dead letter channel for handling an error. 类似于:如果是MyException.class,则使用默认错误处理程序,否则使用死信通道处理错误。

I have try to use but seems this cannot be set globaly so easy as it has to be in method configure() of each route. 我已经尝试使用,但是似乎不能如此简单地全局设置它,因为它必须在每个路由的configure()方法中。

Yes you can have a generic error handler. 是的,您可以拥有通用的错误处理程序。

In the configure method I have done like this: 在configure方法中,我这样做如下:

public void configure() throws Exception {
  ExceptionBuilder.setup(this);
  ...
}

The ExceptionBuilder class look like this: ExceptionBuilder类如下所示:

public class ExceptionBuilder {

    public static void setup(RouteBuilder routeBuilder) {
      routeBuilder.onException(Exception.class).useOriginalMessage().handled(true).to("direct:errorHandler");
    }  
}

Finally in the error handler configure it to your requirements. 最后,在错误处理程序中根据您的要求进行配置。 That means, save the body and headers to log file or send them to a jms queue or stop the processing or anything else. 这意味着,将正文和标头保存到日志文件或将它们发送到jms队列或停止处理或其他操作。 That is up to you. 那取决于你。 You simply configure it once and refer to it from all your routeBuilder classes. 您只需配置一次即可,并从所有routeBuilder类中引用它。

Global scope for the errorHandler is only per RouteBuilder instance . errorHandler的全局作用域仅针对每个RouteBuilder实例 You will need to create a base RouteBuilder class that contains the error handling logic in its configure() method and then extend all of your other routes from it (not forgetting to call super.configure()). 您将需要创建一个基本的RouteBuilder类,该类的configure()方法中包含错误处理逻辑,然后从中扩展所有其他路由(不要忘记调用super.configure())。

You can use a combination of errorHandler as a catch-all for exceptions, with specific exceptions handled by onException() 您可以将errorHandler用作异常的全部捕获,特定的异常由onException()处理

errorHandler(deadLetterChannel("mock:generalException"));

onException(NullPointerException.class)
    .handled(true)
    .to("mock:specificException");

Any routes with these handlers will send exchanges that throw a NullPointerException to the endpoint "mock:specificException". 具有这些处理程序的所有路由都将发送交换,该交换将NullPointerException抛出给端点“ mock:specificException”。 Any other exceptions thrown will be handled by the errorHandler, and the exchange will be sent to "mock:generalException". 引发的任何其他异常将由errorHandler处理,并且交换将发送到“ mock:generalException”。

http://camel.apache.org/error-handler.html http://camel.apache.org/error-handler.html

http://camel.apache.org/exception-clause.html http://camel.apache.org/exception-clause.html

Use try-catch in camel route

.doTry()
 .to("bean:<beanName>?method=<method>")
.endDoTry()

.doCatch(MyException.class)
 .to("bean:<beanName>?method=<method1>")
.doCatch(Exception.class)
 .to("bean:<beanName>?method=<method2>")

Solution: I have used DeadLetterChannelBuilder as error handler with failureProcessor and deadLetterHandleNewException as false, that did the check what I needed (rethrowing exception/hide exception). 解决方案:我已经使用DeadLetterChannelBuilder作为错误处理程序,failureProcessor和deadLetterHandleNewException为false,这检查了我需要的内容(抛出异常/隐藏异常)。

Thanks for advice anyway, it led me to the right way. 无论如何,谢谢您的建议,这使我找到了正确的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM