Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't find service because OSGi bundle isn't activated

I'm having a problem discovering services that are provided by some OSGi bundles that are not being activated. Let me describe the situation:

  • Bundle A defines interface X
  • Bundles B, C, and D provide services that implement interface X
    • These bundles' services are registered via Spring DM, so they are only created when the bundle is activated and Spring DM initialized the application context defined in the bundle
  • Bundle A is activated and at some point asks the service registry for services for interface X. It doesn't find any, because bundles B, C, and D haven't been moved into the ACTIVE state (they are only RESOLVED).

I cannot seem to get bundles B, C, or D to start, and therefore register their services. Forcing them to start by adding them to the config.ini is not an option, because there can be any number of bundles that are installed in the application (via an Eclipse p2-like update mechanism) that implement interface X.

The application is an Eclipse 3.5-based RCP app, using Spring 2.5.6 and Spring DM 1.2.1.

How do I force these bundles to be activated?

like image 907
Paul Schifferer Avatar asked Jan 05 '10 21:01

Paul Schifferer


People also ask

How do I get OSGi services?

There's three ways of getting services in OSGi, in reverse order of ease of use: Acquire the BundleContext (such as via the BundleActivator or through a handler like FrameworkUtil ) and then use getService() directly. Use a ServiceTracker to keep a cache of the services available for quick return.

How do I run OSGi bundle?

Procedure. Install the plug-in bundle into the Eclipse Equinox OSGi framework with the OSGi console. Start the Eclipse Equinox framework with the console enabled. Install the plug-in bundle in the Equinox console.

What is an OSGi bundle?

In OSGi, a single component is called a bundle. Logically, a bundle is a piece of functionality that has an independent lifecycle – which means it can be started, stopped and removed independently. Technically, a bundle is just a jar file with a MANIFEST. MF file containing some OSGi-specific headers.


1 Answers

What you really have is a dependency hierarchy problem, your proposed hacky solution is really just a band-aid over the underlying issue.

What you should really consider is the architecture of your system, as effectively what you have is a circular dependency (re: discussion in comments your original post). You have (like it or not) A requires services from (and in some sense depends on) B and C. Meanwhile, B and C directly depend on A, and as such, cannot start until A comes up.

In the best case, you can write code in B and C to listen for the existence of A, but this at best masks (as I mentioned) the underlying issue. What you should really consider is splitting A into two bundles, let's call them A1 and A2.

A1 should provide the interface which B and C require (depend on). A2 should have listeners for the services B and C depend on. At startup, if B and C are required services, A1 must be run, but A2 may start any time later, and everything should work.

like image 107
Mark Elliot Avatar answered Sep 24 '22 14:09

Mark Elliot