Class Lifecycle
java.lang.Object
org.apache.druid.java.util.common.lifecycle.Lifecycle
A manager of object Lifecycles.
This object has methods for registering objects that should be started and stopped. The Lifecycle allows for
four stages:
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.-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<T extends Closeable>
TaddCloseableInstance(T o) Adds a Closeable instance to the lifecycle atLifecycle.Stage.NORMALstage, doesn't try to call any "start" method on it, useaddStartCloseInstance(Object)instead if you need the latter behaviour.voidaddHandler(Lifecycle.Handler handler) Adds a handler to the Lifecycle at the Stage.NORMAL stage.voidaddHandler(Lifecycle.Handler handler, Lifecycle.Stage stage) Adds a handler to the Lifecycle.<T> TaddManagedInstance(T o) Adds a "managed" instance (annotated withLifecycleStartandLifecycleStop) to the Lifecycle at Stage.NORMAL.<T> TaddManagedInstance(T o, Lifecycle.Stage stage) Adds a "managed" instance (annotated withLifecycleStartandLifecycleStop) to the Lifecycle.voidaddMaybeStartHandler(Lifecycle.Handler handler) Adds a handler to the Lifecycle at the Stage.NORMAL stage and starts it if the lifecycle has already been started.voidaddMaybeStartHandler(Lifecycle.Handler handler, Lifecycle.Stage stage) Adds a handler to the Lifecycle and starts it if the lifecycle has already been started.<T> TAdds a "managed" instance (annotated withLifecycleStartandLifecycleStop) to the Lifecycle at Stage.NORMAL and starts it if the lifecycle has already been started.<T> TaddMaybeStartManagedInstance(T o, Lifecycle.Stage stage) Adds a "managed" instance (annotated withLifecycleStartandLifecycleStop) to the Lifecycle and starts it if the lifecycle has already been started.<T> TAdds 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> TaddMaybeStartStartCloseInstance(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> TAdds an instance with a start() and/or close() method to the Lifecycle at Stage.NORMAL.<T> TaddStartCloseInstance(T o, Lifecycle.Stage stage) Adds an instance with a start() and/or close() method to the Lifecycle.voidvoidjoin()voidstart()voidstop()
-
Constructor Details
-
Lifecycle
public Lifecycle() -
Lifecycle
-
-
Method Details
-
addManagedInstance
public <T> T addManagedInstance(T o) Adds a "managed" instance (annotated withLifecycleStartandLifecycleStop) to the Lifecycle at Stage.NORMAL. If the lifecycle has already been started, it throws anISE- Parameters:
o- The object to add to the lifecycle- Throws:
ISE-addHandler(Handler, Stage)
-
addManagedInstance
Adds a "managed" instance (annotated withLifecycleStartandLifecycleStop) to the Lifecycle. If the lifecycle has already been started, it throws anISE- Parameters:
o- The object to add to the lifecyclestage- The stage to add the lifecycle at- Throws:
ISE-addHandler(Handler, Stage)
-
addStartCloseInstance
public <T> T addStartCloseInstance(T o) Adds an instance with a start() and/or close() method to the Lifecycle at Stage.NORMAL. If the lifecycle has already been started, it throws anISE- Parameters:
o- The object to add to the lifecycle- Throws:
ISE-addHandler(Handler, Stage)
-
addStartCloseInstance
Adds an instance with a start() and/or close() method to the Lifecycle. If the lifecycle has already been started, it throws anISE- Parameters:
o- The object to add to the lifecyclestage- The stage to add the lifecycle at- Throws:
ISE-addHandler(Handler, Stage)
-
addHandler
Adds a handler to the Lifecycle at the Stage.NORMAL stage. If the lifecycle has already been started, it throws anISE- Parameters:
handler- The hander to add to the lifecycle- Throws:
ISE-addHandler(Handler, Stage)
-
addHandler
Adds a handler to the Lifecycle. If the lifecycle has already been started, it throws anISE- Parameters:
handler- The hander to add to the lifecyclestage- The stage to add the lifecycle at- Throws:
ISE- indicates that the lifecycle has already been started and thus cannot be added to
-
addMaybeStartManagedInstance
Adds a "managed" instance (annotated withLifecycleStartandLifecycleStop) to the Lifecycle at Stage.NORMAL and starts it if the lifecycle has already been started.- Parameters:
o- The object to add to the lifecycle- Throws:
Exception-addMaybeStartHandler(Handler, Stage)
-
addMaybeStartManagedInstance
Adds a "managed" instance (annotated withLifecycleStartandLifecycleStop) to the Lifecycle and starts it if the lifecycle has already been started.- Parameters:
o- The object to add to the lifecyclestage- The stage to add the lifecycle at- Throws:
Exception-addMaybeStartHandler(Handler, Stage)
-
addMaybeStartStartCloseInstance
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.- Parameters:
o- The object to add to the lifecycle- Throws:
Exception-addMaybeStartHandler(Handler, Stage)
-
addMaybeStartStartCloseInstance
Adds an instance with a start() and/or close() method to the Lifecycle and starts it if the lifecycle has already been started.- Parameters:
o- The object to add to the lifecyclestage- The stage to add the lifecycle at- Throws:
Exception-addMaybeStartHandler(Handler, Stage)
-
addCloseableInstance
Adds a Closeable instance to the lifecycle atLifecycle.Stage.NORMALstage, doesn't try to call any "start" method on it, useaddStartCloseInstance(Object)instead if you need the latter behaviour. -
addMaybeStartHandler
Adds a handler to the Lifecycle at the Stage.NORMAL stage and starts it if the lifecycle has already been started.- Parameters:
handler- The hander to add to the lifecycle- Throws:
Exception-addMaybeStartHandler(Handler, Stage)
-
addMaybeStartHandler
Adds a handler to the Lifecycle and starts it if the lifecycle has already been started.- Parameters:
handler- The hander to add to the lifecyclestage- The stage to add the lifecycle at- Throws:
Exception- an exception thrown from handler.start(). If an exception is thrown, the handler is *not* added
-
start
- Throws:
Exception
-
stop
public void stop() -
ensureShutdownHook
public void ensureShutdownHook() -
join
- Throws:
InterruptedException
-