Search code examples
javaosgikarafblueprint

OSGi Blueprint: How can onBundleChanged() method get called from the BundleListener Interface?


I am trying to implement the BundleListener interface in a class to retrieve bundle changes. I am trying to get it work, but my onBundleChanged() method does not seem to get called. Maybe it is because it is not being implemented in an "Activator" class. I have no clue.

I would like to get the latest bundle changes because I want to develop an application where bundles can be installed as plugins at runtime. I thought by using the BundleListener interface, I could sort of authorize the Bundles that can be installed in my Apache Karaf environment.

Excerpt from Blueprint.xml

<bean id="MyBundleListener" class="com.hallo.service.MyBundleListener"/>

My class

public class MyBundleListener implements BundleListener {

@Override
public void bundleChanged(BundleEvent event) {
    System.out.println("bundleChanged() -> Just to test if it works");
}
}

In Apache Karaf I use the

log:tail

To look if the method gets called, but no result so far from it.


Solution

  • Creating the bean is not enough for a BundleListener. You also have to add the BundleListener to the BundleContext.

    You can do this in a init method of the bean. See

    bundleContext.addBundleListener
    

    Also do not forget to remove the listener again in the destroy method.

    https://osgi.org/javadoc/r4v43/core/org/osgi/framework/BundleContext.html