Java

JVM Shutdown Hook

Java JVM provides you a hook to register a thread with the shutdown initiation sequence. Once a thread is registered, on every shutdown that thread is run.

JVM’s shutdown sequence has two phases.

  1. All registered shutdown hooks are started in some unspecified order and allowed to run concurrently until they finish.
  2. All un-invoked finalizers are run if finalization-on-exit has been enabled.

After the above two phases are complete, java virtual machine halts.

Its not a complicated exercise, but a less known feature of java. You might require this to release critical resources in the event of unexpected JVM shutdown. You use finally in a try block to release resources and that is completely different.

Just a single line of code will register with the hook of JVM shutdown. You use, Runtime class to register the thread.

System.getRuntime().addShutdownHook(<thread instance>);
  • If you register multiple threads with shutdown hook, all those threads will be run in parallel on shutdown. If you wish to run them in a sequence, then you need to have only one thread registered and have your custom logic controlled within that.
  • If you are shutting down the VM using Runtime.getRuntime().halt, this will not invoke the shutdown hook. It will abruptly shutdown all running process and close the show.

Example Code for JVM Shutdown HookIn the above program, I manualy call the System.exit() and make the JVM to shutdown. Once the System.exit is invoked the phase I of shutdown sequence is initiated and that starts then registered thread ‘jvmShutdownHook’ and then halts the JVM.

Runtime.runFinalizersOnExit() method is almost similar to addShutdownHook() but its depreciated in 1.2

In case if you are wondering on how to detect the web context shutdown in application server scenarios, you can use ServletContextListner

When shutdown hook will NOT be initiated

public class JVMShutdownHookTest {
  public static void main(String[] args) {
    JVMShutdownHook jvmShutdownHook = new JVMShutdownHook();
    Runtime.getRuntime().addShutdownHook(jvmShutdownHook);
    System.out.println("JVM Shutdown Hook Registered.");
    System.out.println("Pre exit.");
    System.exit(0);
    System.out.println("Post exit.");
  }

  private static class JVMShutdownHook extends Thread {
    public void run() {
      System.out.println("JVM Shutdown Hook: Thread initiated.");
    }
  }
}

Output:
JVM Shutdown Hook Registered.
Pre exit.
JVM Shutdown Hook: Thread initiated.

There is no guarantee that always the shutdown hooks will run. There might be a devastating crash of the whole system and JVM might crash due to it. In similar circumstances there is no guarantee that shut down hooks will invoke the thread. Similarly if native OS kills the JVM process then this shutdown hook sequence will not be initiated. Therefore in all cases when the JVM is shutdown abnormally the hook will not be initiated.

Note:

  • You cannot register a shutdown hook after a shutdown is intiated. That is, if you try to register a thread to shutdown hook from within another thread that is already registered with the hook you will get IllegalStateException.
  • You can use Runtime.getRuntime().halt(status); to halt JVM abruptly after the shutdown sequence is initiated.

From http://javapapers.com

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s