I need to modify JVM return code according to my application result.
But it is risky to explicitly call System.exit(code) coz the application is complicated and it is hard to identify the end of running threads.
So I come up with using shutdown hook to modify the return code before JVM exit.
But there is a problem that how can I get the original return code of JVM coz it may be an error code not 0.
A shutdown hook is simply an initialized but unstarted thread. When the virtual machine begins its shutdown sequence it will start all registered shutdown hooks in some unspecified order and let them run concurrently.
getRuntime(). removeShutdownHook(Thread) method. Its boolean return value indicates if the respective thread was a registered hook previously. Thus, one can test if the hook' run() method performs correctly as one part of a test.
A shutdown hook runs for an orderly shutdown: when the last normal thread terminates, someone calls System. exit or by other platform specific means (such as typing Ctrl-C).
The registerShutdownHook() is a method of Spring AbstractApplicationContext class. 2. The registerShutdownHook() method registers a shutdown hook named SpringContextShutdownHook with the JVM runtime. 3. On calling registerShutdownHook() , the Spring context is closed on JVM shutdown, if not already closed.
A special construct that facilitates the developers to add some code that has to be run when the Java Virtual Machine (JVM) is shutting down is known as the Java shutdown hook. The Java shutdown hook comes in very handy in the cases where one needs to perform some special cleanup work when the JVM is shutting down.
If you need to remove a previously registered shutdown hook, the Runtime class provides the removeShutdownHook (Thread) method as well. System.out.println ("Shutdown Hook is running !"); System.out.println ("Application Terminating ...");
Adding Hooks In order to add a shutdown hook, we can use the Runtime.getRuntime ().addShutdownHook () method: Thread printingHook = new Thread (() -> System.out.println ("In the middle of a shutdown")); Runtime.getRuntime ().addShutdownHook (printingHook); Here we simply print something to the standard output before JVM shuts down itself.
For registering the instance of the derived class as the shutdown hook, one has to invoke the method Runtime.getRuntime ().addShutdownHook (Thread), whereas for removing the already registered shutdown hook, one has to invoke the removeShutdownHook (Thread) method.
You should not call exit method in shutdown hook, System.exit(status)
internally calls Runtime.getRuntime().exit(status);
which will cause your application to block indefinitely.
As per the JavaDoc
If this method is invoked after the virtual machine has begun its shutdown sequence then if shutdown hooks are being run this method will block indefinitely.
You don't have access to status
, as it could change even after all shutdown hooks are called.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With