问题描述
在我的项目中,我想在每个新创建的路由上应用单独的 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?
实际上我想在路由级别应用 Oneexception 和 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 level onException: 如果你像这样定义 onException handler
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 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!