JimJim2000 JimJim2000 - 1 year ago 89
Java Question

OSGI: do declarative services become available after bundle start

The question is simple, but I can't find answer - Can I state that all declarative services in bundle A become available after bundle A start? For example,

//In this point are declarative services of bundle A 100% available?

P.S. I use apache felix, but I think it must be defined in Specs but not in implementation.


I assume that DS runtime is running, config is present and all mandatory references are present.

Answer Source

The answer to your question is a very simple: NO. There are NO guarantees about availability in OSGi ever based on neither timing nor ordering. The only guarantees are specified in the service events.

It is one of the greatest causes of complexity to make timing/ordering assumptions in your code because they are always violated in the most obscure way.

DS makes it trivial to write code that correctly reacts to the service dependencies as they come and go. Making sure that you get those guarantees associated with services is incredibly complex and you destroy all that value if you start to make assumptions that something should be available after you call a method.

In your example, just rely on a service that you need. If that service is available, then you are sure all initialization is done.

If you stick to service dependencies life in OSGi is fairly easy and very robust.

UPDATED with example after questions

One the non-OSGi side:

 systemBundleContext = ... create framework
    new BundleActivator() {
        public void start(BundleContext c) {
           // start non-OSGi code
        public void stop(BundleContext c) {
           // stop non-OSGi code
     null );

DS Component:

 public class Initiator {
    BundleActivator ba;

    MyService myService;

    @Activate void activate(BundleContext context) throws Exception {

    @Deactivate void deactivate(BundleContext context) throws Exception {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download