Class DefaultShutdownCallbackRegistry

java.lang.Object
org.apache.logging.log4j.core.util.DefaultShutdownCallbackRegistry
All Implemented Interfaces:
Runnable, LifeCycle, LifeCycle2, ShutdownCallbackRegistry

public class DefaultShutdownCallbackRegistry extends Object implements ShutdownCallbackRegistry, LifeCycle2, 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 Details

    • LOGGER

      protected static final Logger LOGGER
      Status logger.
  • Constructor Details

    • DefaultShutdownCallbackRegistry

      public DefaultShutdownCallbackRegistry()
      Constructs a DefaultShutdownRegistrationStrategy.
    • DefaultShutdownCallbackRegistry

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

    • run

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

      public Cancellable addShutdownCallback(Runnable callback)
      Description copied from interface: ShutdownCallbackRegistry
      Adds a Runnable shutdown callback to this class. Note: The returned Cancellable must be retained on heap by caller to avoid premature garbage-collection of the registered callback (and to ensure the callback runs on shutdown).
      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.
    • initialize

      public void initialize()
      Specified by:
      initialize in interface LifeCycle
    • start

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

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

      public boolean stop(long timeout, 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.
    • getState

      public LifeCycle.State getState()
      Description copied from interface: LifeCycle
      Gets the life-cycle state.
      Specified by:
      getState in interface LifeCycle
      Returns:
      the life-cycle state
    • 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
    • isStopped

      public boolean isStopped()
      Specified by:
      isStopped in interface LifeCycle