@Retention(value=CLASS)
@Target(value=FIELD)
public @interface LifecycleController
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.
/** * 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. } }
Modifier and Type | Optional Element and Description |
---|---|
boolean |
start
Specifies the action to be performed when the injected runnable is invoked.
|
public abstract boolean start