Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get return code in shutdown hook

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.

like image 929
Leslie Wu Avatar asked Feb 09 '17 02:02

Leslie Wu


People also ask

How do you use a shutdown hook?

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.

How do you test a shutdown hook?

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.

Does System exit call shutdown hook?

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).

What is Register shutdown hook in spring?

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.

What is a Java shutdown hook?

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.

How to remove a previously registered shutdown hook?

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 ...");

How do I add a shutdown hook to a thread?

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.

How to remove a shutdown hook from a derived class?

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.


1 Answers

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.

like image 61
ravthiru Avatar answered Oct 22 '22 19:10

ravthiru