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

问题描述

在我的项目中,我想在每个新创建的路由上分别应用OnCompletion和OnException处理器.

In my project I want to apply separate OnCompletion and OnException processor on each newly created route.

假设我必须创建3条路线.对于每个路由,我正在准备一个单独的RouteBuilder类,并进行如下配置-

Suppose I have to create 3 routes. For each route I am preparing a separate RouteBuilder class and doing configuration like below -

onException(Throwable.class).handled(true).process(new ExceptionHandlingProcessor(RouteId)).maximumRedeliveries(2)
        .redeliveryDelay(1000).retriesExhaustedLogLevel(LoggingLevel.ERROR)
        .retryAttemptedLogLevel(LoggingLevel.WARN);

 onCompletion().process(new OnCompletionProcessor(RouteId)) ;

        from("sftp:Configuration").routeId("test")
        .choice()
            .when(body().isEqualTo(null))
                .process(new AgeCalculatingProcessor("test"))
            .otherwise()
                .to("file:configuration").log("Downloaded file ${file:name} complete.")
                ;

我的问题是.... OnException和OnCompletion是否在同一Route Builder类上创建的路由上工作(因为我在每个RouteBuilder类中仅创建一个路由),否则这些将应用于上下文级别并可以在所有路线上使用吗?

My question is ....are the OnException and OnCompletion working on the route that is being created on the same Route Builder class (as I am creating only one route in each RouteBuilder class) or these will be applied to context level and will work on all the routes?

实际上,我想在路由级别上应用Onexception和OnCompletion,但是如果我在每个端点上都应用OnException,则出现异常(例如-尝试将OnException移到路由的顶部),如下所示-

Actually I want to apply Onexception and OnCompletion on Route level, but I am getting exception (like - try moving OnException to the top of route), if I apply the OnException on each endPoint, like below -

from(sftp:conf).OnException(Throwable.class).restExceptionconf.to(file:conf).OnException(Throwable.class).restExceptionConf

from(sftp:conf).OnException(Throwable.class).restExceptionconf.to(file:conf).OnException(Throwable.class).restExceptionConf

推荐答案

RouteBuilder级别onException :如果您这样定义onException处理程序

RouteBuilder level onException: If you define onException handler like this

onException(...).log("This is RouteBuilder level exception handling.");

configure() {
    from(...).to(...);
}

它将处理同一RouteBuilder中所有路由的异常.

it will handler exceptions from all routes within the same RouteBuilder.

路由级别onException :如果您这样定义onException处理程序

Route level onException: If you define onException handler like this

configure() {
    from(...)
    .onException(...).log("This is Route level exception handling.");
    .to(...);
}

它将成为路由级别的onException处理程序,并且仅用于该单个路由上的异常.

it will become a route level onException handler and it will be used only for exceptions on that single route.

路由级别的onException定义将覆盖RouteBuilder级别的定义(例如,如果两个都都定义了onException(MyException.class),那么如果在该路由上引发MyException,则仅会调用直接在路由中定义的一个).

Route level onException definitions will override RouteBuilder level definitions (e.g. if both define onException(MyException.class) then only the one defined directly in the route will be called if MyException is raised on that route).

onCompletion 的行为与onException相同.

onCompletion will behave the same way as onException.

关于"尝试将OnException移到路线顶部"的异常:您应该只在路线的开头定义onException,如下所示:

About the "try moving OnException to the top of route" exception you are getting: you are supposed to only define onException at the beginning of the route like this:

from(sftp:conf).OnException(Throwable.class).restExceptionconf
   .to(file:conf);

进一步了解onException 此处以及有关onCompletion 此处.

Further reading about onException here and about onCompletion here.

这篇关于Apache-Camel:OnCompletion和OnException查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-27 18:37