Hi Reji, I used something similar, here is the code of the registration/unregistration of routes:
public Runnable registerRoutes(final ExtendedCamelContext camelContext, final RouteBuilder builder) { // error handling builder.onException(Exception.class).maximumRedeliveries(0).process(this:: onError).handled(true); var before = camelContext.getRoutes(); try { camelContext.addRoutes(builder); var routes = camelContext.getRoutes().stream().filter(it - > !before. contains(it)).collect(toList()); // unregister callback return () - > routes.forEach(route - > { try { final var id = route.getRouteId(); camelContext.getRouteController().stopRoute(id); if (!camelContext.removeRoute(id)) { log.warn("Can't stop route '{}'", id); } } catch (final Exception e) { throw new IllegalStateException(e); } }); } catch (final RuntimeException re) { throw re; } catch (final Exception e) { throw new IllegalStateException(e); } } Hope it helps. Romain Manni-Bucau @rmannibucau <https://twitter.com/rmannibucau> | Blog <https://rmannibucau.metawerx.net/> | Old Blog <http://rmannibucau.wordpress.com> | Github <https://github.com/rmannibucau> | LinkedIn <https://www.linkedin.com/in/rmannibucau> | Book <https://www.packtpub.com/application-development/java-ee-8-high-performance> Le lun. 1 mars 2021 à 08:44, ski n <raymondmees...@gmail.com> a écrit : > Hi Reji, > > You may check the Error Handler: > https://camel.apache.org/manual/latest/error-handler.html > > Regards, > > Raymond > > Op ma 1 mrt. 2021 om 03:23 schreef Reji Mathews <contactr...@gmail.com>: > > > I am trying to build a camel context from scratch using very basic API's > > (Not with the RouteBuilder). > > > > Started off with an instance of DefaultCamelContext class in plain > vanilla > > java and populated some route definitions into the context as follows > > > > > > > myCamelContext.addRouteDefinitions(routeDefinitionService.generateRouteDefinitions()) > > > > myCamelContext.start() > > > > > > My custom method here generates a List of RoleDefinitions and adds it to > > my myCamelContext. It works great and I can see my context starts with > all > > those routes. > > > > I need to add an onException handler as global scoped in this context. > If > > anyone has a sample code snippet on how this can be done, would be great. > > > > Cheers > > Reji > > >