Annotation Type LifecycleController


@Retention(CLASS) @Target(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 programmatically into the OSGi registry, using the LifecycleController annotation.
   */
 @Component
 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 before our method is invoked, as in normal case. Notice that the service won't
         // be destroyed here, and the "starter" runnable can be re-invoked later.
     }
 }
 
Author:
Felix Project Team
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    boolean
    Specifies the action to be performed when the injected runnable is invoked.
  • Element Details

    • start

      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.
      Returns:
      true if the component must be started when you invoke the injected runnable, or false if the component must stopped when invoking the runnable.
      Default:
      true