Class JvmShutdownSafeguard

  • All Implemented Interfaces:
    Runnable

    public class JvmShutdownSafeguard
    extends Thread
    A utility that guards against blocking shutdown hooks that block JVM shutdown.

    When the JVM shuts down cleanly (SIGTERM or System.exit(int)) it runs all installed shutdown hooks. It is possible that any of the shutdown hooks blocks, which causes the JVM to get stuck and not exit at all.

    This utility installs a shutdown hook that forcibly terminates the JVM if it is still alive a certain time after clean shutdown was initiated. Even if some shutdown hooks block, the JVM will terminate within a certain time.

    • Method Detail

      • run

        public void run()
        Specified by:
        run in interface Runnable
        Overrides:
        run in class Thread
      • installAsShutdownHook

        public static void installAsShutdownHook​(org.slf4j.Logger logger)
        Installs the safeguard shutdown hook. The maximum time that the JVM is allowed to spend on shutdown before being killed is five seconds.
        Parameters:
        logger - The logger to log errors to.
      • installAsShutdownHook

        public static void installAsShutdownHook​(org.slf4j.Logger logger,
                                                 long delayMillis)
        Installs the safeguard shutdown hook. The maximum time that the JVM is allowed to spend on shutdown before being killed is the given number of milliseconds.
        Parameters:
        logger - The logger to log errors to.
        delayMillis - The delay (in milliseconds) to wait after clean shutdown was stared, before forcibly terminating the JVM.