Apache-Camel : OnCompletion and OnException query

一笑奈何 提交于 2019-12-11 13:22:20

问题


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

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

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?

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


回答1:


RouteBuilder level onException: If you define onException handler like this

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

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

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

Route level onException: If you define onException handler like this

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

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

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 will behave the same way as 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);

Further reading about onException here and about onCompletion here.



来源:https://stackoverflow.com/questions/32561436/apache-camel-oncompletion-and-onexception-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!