Search code examples
javaosgiapache-karafosgi-bundlekaraf

Karaf : Recommended way to uninstall/install API's bundle, and how to avoid ghost bundle resolution


I'm using Karaf (2.4.* & 4.0.*) for the past two years, in different missions, and each time I faced caching problem with API bundles when it comes to uninstall/install them.

Let's imagine the following scenario :

  1. I install (1) service-api/1.0.0, exporting com.service.api
  2. I install (2) service-core/1.0.0, which imports com.service.api
  3. I uninstall service-api/1.0.0.
  4. I install (3) service-api/1.1.0, which adds new interfaces.

The different behaviour occurs when I use bundle:install -s to install bundles, uninstall to uninstall them :

  1. The -core is not always stopped. In fact, he use a cached version of service-api. I can even see the bundle id (1) when I do a imports -i (2). Doing a "resolve (2)" doesn't solve the problem, only the fact to "stop (2)" then start it again.

  2. When I install service-api/1.1.0, the bug in 3 still occurs and my service-core uses again the old -api. The big problem is if I install a new service-soap, it will use the bundle (3), while the (2) uses the (1), and thus it creates clash between classloader.

I can't find any resource on that matter on the internet, and I've tried Equinox & Felix and I do not see any solution for that problem. Is bundle:install the good way to install bundle ? Does using deploy is a better solution ?

Bonus : In OSGI spec, a bundle should not be uninstalled until all the reference are released, but in Karaf, the bundle is uninstalling itself directly.. Could it be the cause of the problem described above ?


Solution

  • What you see is conforming to the OSGi specs. When you uninstall an API then all bundles using it will still use it until they are resolved again. This behavior was introduced to avoid cascading changes in the runtime.

    To make a bundle pick up or loose an API you need to resolve the bundle. The easiest way to do this is to use the resolve command. Which resolves a given bundle or all bundles.

    Another way to auto resolve bundles is to always install them using Apache Karaf features. When you install / uninstall features Karaf will always make sure to re resolve all bundles that could be affected.