title: Delaying service registration in OSGi. How to do it properly
body: <p>When working with declarative services in OSGi, I encountered a special usecase:</p>
  
  <p>Sometimes you want a ds component to register a service as soon a long time operation has completed. Usually this is not a problem since services will be registered after the activate method has finished. However, sometimes it is necessary to open up a thread inside the activate method (in order to avoid the thread from blocking) and register the service once the thread has finished its work, e.g.</p>
  
  <pre><code>@Component
  public class MyClass implements MyService {
  
     private ExecutorService executor = Executors.newCachedThreadPool();
  
  @Activate
  public void start(final BundleContext context) {
    executor.execute(new Runnable() {
  
    @Override
    public void run() {
  
      ...   // doing something for long
  
       context.registerService(MyService.class, MyClass.this, null);
    }
    });
   }
  }
  </code></pre>
  
  <p>We came up with an util class doing exactly that (and covering issues like the component being stopped while the thread is still running etc.). Is there a better way to do such a thing? If not, wouldn't it be convenient to add such an util class to the framework?</p>
  
  <p>Best regards,
  Mike </p>