public class Lifecycle extends Object
Lifecycle.Stage.INIT, Lifecycle.Stage.NORMAL, Lifecycle.Stage.SERVER, and Lifecycle.Stage.ANNOUNCEMENTS.
Things added at Lifecycle.Stage.INIT will be started first (in the order that they are added to the Lifecycle instance)
and then things added at Lifecycle.Stage.NORMAL, then Lifecycle.Stage.SERVER, and finally, Lifecycle.Stage.ANNOUNCEMENTS
will be started.
The close operation goes in reverse order, starting with the last thing added at Lifecycle.Stage.ANNOUNCEMENTS and
working backwards.
Conceptually, the stages have the following purposes:
- Lifecycle.Stage.INIT: Currently, this stage is used exclusively for log4j initialization, since almost everything
needs logging and it should be the last thing to shutdown. Any sort of bootstrapping object that provides
something that should be initialized before nearly all other Lifecycle objects could also belong here (if it
doesn't need logging during start or stop).
- Lifecycle.Stage.NORMAL: This is the default stage. Most objects will probably make the most sense to be registered
at this level, with the exception of any form of server or service announcements
- Lifecycle.Stage.SERVER: This lifecycle stage is intended for all 'server' objects, for example,
org.apache.druid.server.initialization.jetty.JettyServerModule, but any sort of 'server' that expects most (or
some specific) Lifecycle objects to be initialized by the time it starts, and still available at the time it stops
can logically live in this stage.
- Lifecycle.Stage.ANNOUNCEMENTS: Any object which announces to a cluster this servers location belongs in this stage.
By being last, we can be sure that all servers are initialized before we advertise the endpoint locations, and
also can be sure that we un-announce these advertisements prior to the Stage.SERVER objects stop.
There are two sets of methods to add things to the Lifecycle. One set that will just add instances and enforce that
start() has not been called yet. The other set will add instances and, if the lifecycle is already started, start
them.| Modifier and Type | Class and Description |
|---|---|
static interface |
Lifecycle.Handler |
static class |
Lifecycle.Stage |
| Modifier and Type | Method and Description |
|---|---|
<T extends Closeable> |
addCloseableInstance(T o)
Adds a Closeable instance to the lifecycle at
Lifecycle.Stage.NORMAL stage, doesn't try to call any "start" method on
it, use addStartCloseInstance(Object) instead if you need the latter behaviour. |
void |
addHandler(Lifecycle.Handler handler)
Adds a handler to the Lifecycle at the Stage.NORMAL stage.
|
void |
addHandler(Lifecycle.Handler handler,
Lifecycle.Stage stage)
Adds a handler to the Lifecycle.
|
<T> T |
addManagedInstance(T o)
Adds a "managed" instance (annotated with
LifecycleStart and LifecycleStop) to the Lifecycle at
Stage.NORMAL. |
<T> T |
addManagedInstance(T o,
Lifecycle.Stage stage)
Adds a "managed" instance (annotated with
LifecycleStart and LifecycleStop) to the Lifecycle. |
void |
addMaybeStartHandler(Lifecycle.Handler handler)
Adds a handler to the Lifecycle at the Stage.NORMAL stage and starts it if the lifecycle has already been started.
|
void |
addMaybeStartHandler(Lifecycle.Handler handler,
Lifecycle.Stage stage)
Adds a handler to the Lifecycle and starts it if the lifecycle has already been started.
|
<T> T |
addMaybeStartManagedInstance(T o)
Adds a "managed" instance (annotated with
LifecycleStart and LifecycleStop) to the Lifecycle at
Stage.NORMAL and starts it if the lifecycle has already been started. |
<T> T |
addMaybeStartManagedInstance(T o,
Lifecycle.Stage stage)
Adds a "managed" instance (annotated with
LifecycleStart and LifecycleStop) to the Lifecycle
and starts it if the lifecycle has already been started. |
<T> T |
addMaybeStartStartCloseInstance(T o)
Adds an instance with a start() and/or close() method to the Lifecycle at Stage.NORMAL and starts it if the
lifecycle has already been started.
|
<T> T |
addMaybeStartStartCloseInstance(T o,
Lifecycle.Stage stage)
Adds an instance with a start() and/or close() method to the Lifecycle and starts it if the lifecycle has
already been started.
|
<T> T |
addStartCloseInstance(T o)
Adds an instance with a start() and/or close() method to the Lifecycle at Stage.NORMAL.
|
<T> T |
addStartCloseInstance(T o,
Lifecycle.Stage stage)
Adds an instance with a start() and/or close() method to the Lifecycle.
|
void |
ensureShutdownHook() |
void |
join() |
void |
start() |
void |
stop() |
public Lifecycle()
public Lifecycle(String name)
public <T> T addManagedInstance(T o)
LifecycleStart and LifecycleStop) to the Lifecycle at
Stage.NORMAL. If the lifecycle has already been started, it throws an ISEo - The object to add to the lifecycleISE - addHandler(Handler, Stage)public <T> T addManagedInstance(T o,
Lifecycle.Stage stage)
LifecycleStart and LifecycleStop) to the Lifecycle.
If the lifecycle has already been started, it throws an ISEo - The object to add to the lifecyclestage - The stage to add the lifecycle atISE - addHandler(Handler, Stage)public <T> T addStartCloseInstance(T o)
ISEo - The object to add to the lifecycleISE - addHandler(Handler, Stage)public <T> T addStartCloseInstance(T o,
Lifecycle.Stage stage)
ISEo - The object to add to the lifecyclestage - The stage to add the lifecycle atISE - addHandler(Handler, Stage)public void addHandler(Lifecycle.Handler handler)
ISEhandler - The hander to add to the lifecycleISE - addHandler(Handler, Stage)public void addHandler(Lifecycle.Handler handler, Lifecycle.Stage stage)
ISEhandler - The hander to add to the lifecyclestage - The stage to add the lifecycle atISE - indicates that the lifecycle has already been started and thus cannot be added topublic <T> T addMaybeStartManagedInstance(T o)
throws Exception
LifecycleStart and LifecycleStop) to the Lifecycle at
Stage.NORMAL and starts it if the lifecycle has already been started.o - The object to add to the lifecycleException - addMaybeStartHandler(Handler, Stage)public <T> T addMaybeStartManagedInstance(T o,
Lifecycle.Stage stage)
throws Exception
LifecycleStart and LifecycleStop) to the Lifecycle
and starts it if the lifecycle has already been started.o - The object to add to the lifecyclestage - The stage to add the lifecycle atException - addMaybeStartHandler(Handler, Stage)public <T> T addMaybeStartStartCloseInstance(T o)
throws Exception
o - The object to add to the lifecycleException - addMaybeStartHandler(Handler, Stage)public <T> T addMaybeStartStartCloseInstance(T o,
Lifecycle.Stage stage)
throws Exception
o - The object to add to the lifecyclestage - The stage to add the lifecycle atException - addMaybeStartHandler(Handler, Stage)public <T extends Closeable> T addCloseableInstance(T o)
Lifecycle.Stage.NORMAL stage, doesn't try to call any "start" method on
it, use addStartCloseInstance(Object) instead if you need the latter behaviour.public void addMaybeStartHandler(Lifecycle.Handler handler) throws Exception
handler - The hander to add to the lifecycleException - addMaybeStartHandler(Handler, Stage)public void addMaybeStartHandler(Lifecycle.Handler handler, Lifecycle.Stage stage) throws Exception
handler - The hander to add to the lifecyclestage - The stage to add the lifecycle atException - an exception thrown from handler.start(). If an exception is thrown, the handler is *not* addedpublic void stop()
public void ensureShutdownHook()
public void join()
throws InterruptedException
InterruptedExceptionCopyright © 2011–2023 The Apache Software Foundation. All rights reserved.