You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Reji Mathews <co...@gmail.com> on 2021/03/01 02:25:11 UTC

Adding Global scoped exception handler in DefaultCamelContext

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

Re: Adding Global scoped exception handler in DefaultCamelContext

Posted by Reji Mathews <co...@gmail.com>.
Thanks guys. These are good pointers. Will try these out.

On Mon, Mar 1, 2021 at 3:14 AM Romain Manni-Bucau <rm...@gmail.com>
wrote:

> 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 <ra...@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 <co...@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
> > >
> >
>

Re: Adding Global scoped exception handler in DefaultCamelContext

Posted by Romain Manni-Bucau <rm...@gmail.com>.
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 <ra...@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 <co...@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
> >
>

Re: Adding Global scoped exception handler in DefaultCamelContext

Posted by ski n <ra...@gmail.com>.
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 <co...@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
>