Search code examples
osgiosgi-ds

Long lasting Declarative Service activate method


I tried to understand from the OSGi specs what it should happen in case an activate method blocks indefinitely but I did not find an answer. On the other hand, it seams that that Felix SCR has ds.lock.timeout.milliseconds and ds.stop.timeout.milliseconds properties to manage activate/deactivate timeouts, right?

Questions:

  • Why OSGi specs do not mention activate/deactivate deadlock management?
  • In case a DS needs more time to run its activate method, is it wise to increase the default SCR ds.lock.timeout.milliseconds value? Or is it better to avoid the activate method altogether and register the service "by hand" using context.registerService in a dedicated thread?

Solution

  • If you have a long initialization you mark the component immediate. In the activate method you start a background initialization. When your service is called, you block until the initialization is done. (Promises work very nice for this technique.)

    @Component(immediate=true)
    public class FooImpl implements Foo {
       Promise<Foo>  promise;
    
       @Activate void activate() { promise = begin(); }
    
       @Override
       public void foo() { promise.get().foo(); }
    }
    

    The advantage of this technique is that it allows many initializations to proceed in parallel.

    The delegation you need is kind of ugly. If performance is not overly important you can easily create a proxy doing the actual work.