Search code examples
javaosgiapache-karaf

use @Activate annotation in OSGI in Karaf


I am trying to have a function in my brand new bundle that is triggered once when the Apache Karaf container is started.

The main class in the Bundle looks like this:

@Component(service = BrandNewBundle.class)
public class BrandNewBundle {

    ....
    @Activate
    protected void activate(ComponentContext myComponentContext) {
        LOG.info("Brand new bundle started.");
    }
}

In the karaf console I see the bundle status as Active. But in the logs, I don't see the message that I have tried to log.

Some information:

  • It's a Maven build
  • osgi.cmpn and slf4j-api are my dependencies in pom.xml, and nothing else so far.
  • There is no bnd.bnd file in this

I have read the Apache Felix Service Component Runtime (SCR) page. Unlike what it mentioned there, my OSGI-INF/....xml is Autogenerated. I don't know how to debug further.

To sum up, the question is, how to actually trigger my activate function?


Solution

  • The Component annotation has to have an immediate=true as well. I added that, and the activate() function was triggered on the karaf start.

    @Component(
        immediate=tue,
        service = BrandNewBundle.class
    )