Class DefaultShutdownCallbackRegistry

  • All Implemented Interfaces:
    java.lang.Runnable, LifeCycle, LifeCycle2, ShutdownCallbackRegistry

    public class DefaultShutdownCallbackRegistry
    extends java.lang.Object
    implements ShutdownCallbackRegistry, LifeCycle2, java.lang.Runnable
    ShutdownRegistrationStrategy that simply uses Runtime.addShutdownHook(Thread). If no strategy is specified, this one is used for shutdown hook registration.
    Since:
    2.1
    • Field Detail

      • LOGGER

        protected static final Logger LOGGER
        Status logger.
      • state

        private final java.util.concurrent.atomic.AtomicReference<LifeCycle.State> state
      • threadFactory

        private final java.util.concurrent.ThreadFactory threadFactory
      • hooks

        private final java.util.Collection<Cancellable> hooks
      • shutdownHookRef

        private java.lang.ref.Reference<java.lang.Thread> shutdownHookRef
    • Constructor Detail

      • DefaultShutdownCallbackRegistry

        public DefaultShutdownCallbackRegistry()
        Constructs a DefaultShutdownRegistrationStrategy.
      • DefaultShutdownCallbackRegistry

        protected DefaultShutdownCallbackRegistry​(java.util.concurrent.ThreadFactory threadFactory)
        Constructs a DefaultShutdownRegistrationStrategy using the given ThreadFactory.
        Parameters:
        threadFactory - the ThreadFactory to use to create a Runtime shutdown hook thread
    • Method Detail

      • run

        public void run()
        Executes the registered shutdown callbacks.
        Specified by:
        run in interface java.lang.Runnable
      • addShutdownCallback

        public Cancellable addShutdownCallback​(java.lang.Runnable callback)
        Description copied from interface: ShutdownCallbackRegistry
        Adds a Runnable shutdown callback to this class.
        Specified by:
        addShutdownCallback in interface ShutdownCallbackRegistry
        Parameters:
        callback - the shutdown callback to be executed upon shutdown.
        Returns:
        a Cancellable wrapper of the provided callback or null if the shutdown hook is disabled and cannot be added.
      • start

        public void start()
        Registers the shutdown thread only if this is initialized.
        Specified by:
        start in interface LifeCycle
      • addShutdownHook

        private void addShutdownHook​(java.lang.Thread thread)
      • stop

        public void stop()
        Specified by:
        stop in interface LifeCycle
      • stop

        public boolean stop​(long timeout,
                            java.util.concurrent.TimeUnit timeUnit)
        Cancels the shutdown thread only if this is started.
        Specified by:
        stop in interface LifeCycle2
        Parameters:
        timeout - the maximum time to wait
        timeUnit - the time unit of the timeout argument
        Returns:
        true if the receiver was stopped cleanly and normally, false otherwise.
      • removeShutdownHook

        private void removeShutdownHook()
      • isStarted

        public boolean isStarted()
        Indicates if this can accept shutdown hooks.
        Specified by:
        isStarted in interface LifeCycle
        Returns:
        true if this can accept shutdown hooks