Class ShutdownHookManager

java.lang.Object
org.apache.hadoop.util.ShutdownHookManager

@Public @Evolving public final class ShutdownHookManager extends Object
The ShutdownHookManager enables running shutdownHook in a deterministic order, higher priority first.

The JVM runs ShutdownHooks in a non-deterministic order or in parallel. This class registers a single JVM shutdownHook and run all the shutdownHooks registered to it (to this class) in order based on their priority. Unless a hook was registered with a shutdown explicitly set through addShutdownHook(Runnable, int, long, TimeUnit), the shutdown time allocated to it is set by the configuration option CommonConfigurationKeysPublic.SERVICE_SHUTDOWN_TIMEOUT in core-site.xml, with a default value of CommonConfigurationKeysPublic.SERVICE_SHUTDOWN_TIMEOUT_DEFAULT seconds.

  • Field Details

    • TIMEOUT_MINIMUM

      public static final long TIMEOUT_MINIMUM
      Minimum shutdown timeout: 1L second(s).
      See Also:
    • TIME_UNIT_DEFAULT

      public static final TimeUnit TIME_UNIT_DEFAULT
      The default time unit used: seconds.
  • Method Details

    • get

      @Public public static ShutdownHookManager get()
      Return ShutdownHookManager singleton.
      Returns:
      ShutdownHookManager singleton.
    • addShutdownHook

      @Public @Stable public void addShutdownHook(Runnable shutdownHook, int priority)
      Adds a shutdownHook with a priority, the higher the priority the earlier will run. ShutdownHooks with same priority run in a non-deterministic order.
      Parameters:
      shutdownHook - shutdownHook Runnable
      priority - priority of the shutdownHook.
    • addShutdownHook

      @Public @Stable public void addShutdownHook(Runnable shutdownHook, int priority, long timeout, TimeUnit unit)
      Adds a shutdownHook with a priority and timeout the higher the priority the earlier will run. ShutdownHooks with same priority run in a non-deterministic order. The shutdown hook will be terminated if it has not been finished in the specified period of time.
      Parameters:
      shutdownHook - shutdownHook Runnable
      priority - priority of the shutdownHook
      timeout - timeout of the shutdownHook
      unit - unit of the timeout TimeUnit
    • removeShutdownHook

      @Public @Stable public boolean removeShutdownHook(Runnable shutdownHook)
      Removes a shutdownHook.
      Parameters:
      shutdownHook - shutdownHook to remove.
      Returns:
      TRUE if the shutdownHook was registered and removed, FALSE otherwise.
    • hasShutdownHook

      @Public @Stable public boolean hasShutdownHook(Runnable shutdownHook)
      Indicates if a shutdownHook is registered or not.
      Parameters:
      shutdownHook - shutdownHook to check if registered.
      Returns:
      TRUE/FALSE depending if the shutdownHook is is registered.
    • isShutdownInProgress

      @Public @Stable public boolean isShutdownInProgress()
      Indicates if shutdown is in progress or not.
      Returns:
      TRUE if the shutdown is in progress, otherwise FALSE.
    • clearShutdownHooks

      @Public @Stable public void clearShutdownHooks()
      clear all registered shutdownHooks.