userimage

Triggering a task on apache camel starup and run once

Recently I worked on a Java project using Camel & Spring. We would like to trigger an initialize method on a singleton bean after Spring finished doing its thing and Camel has finished building all routes.

We cant call the method at class creation time as it has dynamic linkings to other classes that it picks up from the @Component spring annotation and we dont know when/if these classes have been loaded yet to actually run the init method as part of a constructor.

How can I go about invoking a method or methods to run only once right after Camel startup is complete?

userimage

You can add the logic in a method of your bean class and annotate it with @PostConstruct - spring will invoke this method once this bean is fully initialized and all its dependencies are set.

e.g

@Component
class MyBeanClass{
    @PostConstruct
    void init() {

      //business need to perform on startup
    }
}

If the logic needs to be invoked once the whole spring application context is fully initialized you can do that by implementing the LifeCycle interface.
    

Answer is