org.apache.felix.dm.annotation.api
Annotation Type LifecycleController


@Retention(value=CLASS)
@Target(value=FIELD)
public @interface LifecycleController

Injects a Runnable object in a Service for starting/stopping it programatically. By default, a Service is implicitly started when the service's bundle is started and when all required dependencies are satisfied. However, it is sometimes required to programatically take control of when the service is started or stopped. In this case, the injected Runnable can be invoked in order to start/register (or stop/unregister) a Service at any time. When this annotation is used, then the Service on which this annotation is applied is not activated by default, and you have to call the injected Runnable yourself.

Usage Examples

 /**
   * This Service will be registered programatically into the OSGi registry, using the LifecycleController annotation.
   */
 @Service
 class X implements Z {
     @LifecycleController
     Runnable starter
     
     @LifecycleController(start=false)
     Runnable stopper
   
     @Init
     void init() {
         // At this point, all required dependencies are there, but we'll activate our service in 2 seconds ...
         Thread t = new Thread() {
            public void run() {
              sleep(2000);
              // start our "Z" service (our "start" method will be called, juste before service registration
              starter.run();
              
              sleep(2000);
              // now, stop/unregister the "Z" service (we'll then be called in our stop() method
              stopper.run();
            }
          };
          t.start();
     }
     
     @Start
     public void start() {
         // This method will be called after we invoke our starter Runnable, and our service will be
         // published after our method returns, as in normal case.
     }

     @Stop
     public void stop() {
         // This method will be called after we invoke our "stop" Runnable, and our service will be
         // unregistered after our method returns, as in normal case. Notice that the service won't
         // be destroyed here, and the "starter" runnable can be re-invoked later.
     }
 }
 


Optional Element Summary
 boolean start
          Specifies the action to be performed when the Injected runnable is invoked.
 

start

public abstract boolean start
Specifies the action to be performed when the Injected runnable is invoked. By default, the Runnable will fire a Service Component activation, when invoked. If you specify this attribute to false, then the Service Component will be stopped, when the runnable is invoked.

Default:
true


Copyright © 2013 The Apache Software Foundation. All Rights Reserved.