Class Lifecycle


  • public class Lifecycle
    extends Object
    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.
    • Constructor Detail

      • Lifecycle

        public Lifecycle()
      • Lifecycle

        public Lifecycle​(String name)
    • Method Detail

      • addManagedInstance

        public <T> T addManagedInstance​(T o)
        Adds a "managed" instance (annotated with LifecycleStart and LifecycleStop) to the Lifecycle at Stage.NORMAL. If the lifecycle has already been started, it throws an ISE
        Parameters:
        o - The object to add to the lifecycle
        Throws:
        ISE - addHandler(Handler, Stage)
      • addManagedInstance

        public <T> T addManagedInstance​(T o,
                                        Lifecycle.Stage stage)
        Adds a "managed" instance (annotated with LifecycleStart and LifecycleStop) to the Lifecycle. If the lifecycle has already been started, it throws an ISE
        Parameters:
        o - The object to add to the lifecycle
        stage - 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 an ISE
        Parameters:
        o - The object to add to the lifecycle
        Throws:
        ISE - addHandler(Handler, Stage)
      • addStartCloseInstance

        public <T> T addStartCloseInstance​(T o,
                                           Lifecycle.Stage stage)
        Adds an instance with a start() and/or close() method to the Lifecycle. If the lifecycle has already been started, it throws an ISE
        Parameters:
        o - The object to add to the lifecycle
        stage - The stage to add the lifecycle at
        Throws:
        ISE - addHandler(Handler, Stage)
      • addHandler

        public void addHandler​(Lifecycle.Handler handler)
        Adds a handler to the Lifecycle at the Stage.NORMAL stage. If the lifecycle has already been started, it throws an ISE
        Parameters:
        handler - The hander to add to the lifecycle
        Throws:
        ISE - addHandler(Handler, Stage)
      • addHandler

        public void addHandler​(Lifecycle.Handler handler,
                               Lifecycle.Stage stage)
        Adds a handler to the Lifecycle. If the lifecycle has already been started, it throws an ISE
        Parameters:
        handler - The hander to add to the lifecycle
        stage - The stage to add the lifecycle at
        Throws:
        ISE - indicates that the lifecycle has already been started and thus cannot be added to
      • addMaybeStartStartCloseInstance

        public <T> T addMaybeStartStartCloseInstance​(T o)
                                              throws Exception
        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

        public <T> T addMaybeStartStartCloseInstance​(T o,
                                                     Lifecycle.Stage stage)
                                              throws Exception
        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 lifecycle
        stage - The stage to add the lifecycle at
        Throws:
        Exception - addMaybeStartHandler(Handler, Stage)
      • addMaybeStartHandler

        public void addMaybeStartHandler​(Lifecycle.Handler handler,
                                         Lifecycle.Stage stage)
                                  throws Exception
        Adds a handler to the Lifecycle and starts it if the lifecycle has already been started.
        Parameters:
        handler - The hander to add to the lifecycle
        stage - 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
      • stop

        public void stop()
      • ensureShutdownHook

        public void ensureShutdownHook()