Reputation: 487
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
Upvotes: 1
Views: 2375
Reputation: 745
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.
Upvotes: 2