camel-cdi icon indicating copy to clipboard operation
camel-cdi copied to clipboard

Support RouteBuilder with custom scopes

Open gnodet opened this issue 8 years ago • 2 comments

The current Camel-CDI extension instantiates all RouteBuilder when the application is started. This does not allow defining routes inside a custom scope. This would be interesting in OSGi to allow routes to be created / destroyed along with the OSGi service dependencies. One possible way would be to somehow override the @PostConstruct / @PreDestroy lifecycle of those RouteBuilders, so that the routes would be automatically created / destroyed when the builder is instantiated / destroyed.

So instead of having

@Component @Immediate
class Application {

    @Inject
    CamelContext context;

    @Inject @Service
    UnreliableService unreliableService;

    RouteBuilder builder;

    @PostConstruct
    public void start() throws Exception {
        builder = new MetricsRoute();
        context.addRoutes(builder);
        for (RouteDefinition rd : builder.getRouteCollection().getRoutes()) {
            context.startRoute(rd.getId());
        }
    }

    @PreDestroy
    public void stop() throws Exception {
        context.removeRouteDefinitions(builder.getRouteCollection().getRoutes());
    }

    @Vetoed
    class MetricsRoute extends RouteBuilder {

        @Override
        public void configure() {
            // TODO: create routes
        }
    }

}

We would simply have:

class Application {

    @Component @Immediate
    static class MetricsRoute extends RouteBuilder {

        @Inject @Service
        UnreliableService unreliableService;

        @Override
        public void configure() {
            // TODO: create routes
        }
    }

}

gnodet avatar May 24 '16 10:05 gnodet

It should be possible to move the RouteBuilder bean references binding logic to the corresponding Camel contexts into a custom InjectionTarget, so that it's still performed for custom scoped beans and skip the eager instantiation of these custom scoped RouteBuilder beans that's done in the Camel CDI extension so that it can be delegated to whoever is responsible for the custom scope.

Am I understanding your use case correctly?

astefanutti avatar May 24 '16 12:05 astefanutti

Yes.

gnodet avatar May 24 '16 12:05 gnodet