1

In previous projects i often used Guice also in conjunction with camel. My approach was to extend Camel's Main class and inject my preconfigured context there. I needed to control start of the context. Before start of context i did some preperation (e.g. start hawtio and other setup stuff).

The same i did with RouteBuilder. One central RouteBuilder set up stuff like onException, added RoutePolicies and configured autostart on other routes and of course added all the other routes.

In meantime i learned to love CDI and camel's CDI support in 2.17 (and fuse 6.3) seems to be complete.

  • So what would be a good approach with camel-cdi to control the start of camel context (deployed as osgi bundle on fuse)?

  • How to disable or control autodiscovery of RouteBuilder (and or other stuff)?

dermoritz
  • 12,519
  • 25
  • 97
  • 185
  • I'm confused. Why do you want to not discover your route builders? The point behind camel-cdi is that its doing the look up for you. If you need to start other components like hawt.io those should be their own components, probably managed in the CDI life cycle. It may help to give an example. – John Ament Oct 08 '16 at 11:48
  • because i want to do some context scoped setup before all routes start. E.g. RoutePolicies and autostart behavior. Normally i hav one main route that does not routing but setup and including other routes. – dermoritz Oct 10 '16 at 07:00

1 Answers1

2

So what would be a good approach with camel-cdi to control the start of camel context (deployed as osgi bundle on fuse)?

Camel CDI always starts the auto-configured Camel contexts. That being said, it is possible to customise these so that routes are not started, by declaring a PostConstruct lifecycle event for example:

@ApplicationScoped
class CustomCamelContext extends DefaultCamelContext {

    @PostConstruct
    void customize() {
        setAutoStartup(false);
    }
}

In that example, the routes added to that Camel context won't be started along with the context.

This respects the Camel principle to start contexts with all the validation that's done at that stage. Yet with the ability to not start the routing.

How to disable or control autodiscovery of RouteBuilder (and or other stuff)?

The RoutesBuilder beans qualified with @ContextName are automatically added to the corresponding CamelContext beans by Camel CDI. If no such CamelContext bean exists, it gets automatically created. On the other hand, RoutesBuilder beans qualified with user-defined qualifiers do not trigger the automatic creation of any CamelContext beans. That can be used for Camel routes that may be required to be added later during the application execution. For example with:

@DoNotDiscover
class MyRouteBuilder extends RouteBuilder {
    // ...
}

If no Camel context bean qualified with @DoNotDiscover is explicitly declared, the MyRouteBuilder bean won't be auto-discovered. Still it can be used later during the application execution, e.g.:

@Inject
@DoNotDiscover
Instance<RouteBuilder> routes;

@Inject
CamelContext context;

for (RouteBuilder route : routes)
    route.addRoutes(route);
Antonin Stefanutti
  • 1,061
  • 6
  • 6
  • I guess your last piece of code is inside the only auto-discovered route builder?! All in all you you pushed me in right direction to get all control i need - thanks. – dermoritz Oct 13 '16 at 07:54
  • 1
    This can actually be in any beans deployed in your application, though if you want to have that logic executed upon bootstrap (as CDI beans are not eagerly instantiated), you may want to put that piece of code along with `@Observes @Initialized(ApplicationScoped.class)` or more specifically `@Observes CamelContextStartedEvent`. – Antonin Stefanutti Oct 13 '16 at 08:35
  • A discussion has just started on how to improve disabling auto-discovery and startup at https://issues.apache.org/jira/browse/CAMEL-10391 – Antonin Stefanutti Oct 19 '16 at 13:56