Questions tagged [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.

public void addShutdownHook(Thread hook)

Registers a new virtual-machine shutdown hook.

The Java virtual machine shuts down in response to two kinds of events:

The program exits normally, when the last non-daemon thread exits or when the exit (equivalently, System.exit) method is invoked, or The virtual machine is terminated in response to a user interrupt, such as typing ^C, or a system-wide event, such as user logoff or system shutdown. 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. When all the hooks have finished it will then run all uninvoked finalizers if finalization-on-exit has been enabled. Finally, the virtual machine will halt. Note that daemon threads will continue to run during the shutdown sequence, as will non-daemon threads if shutdown was initiated by invoking the exit method.

Once the shutdown sequence has begun it can be stopped only by invoking the halt method, which forcibly terminates the virtual machine.

Once the shutdown sequence has begun it is impossible to register a new shutdown hook or de-register a previously-registered hook. Attempting either of these operations will cause an IllegalStateException to be thrown.

Shutdown hooks run at a delicate time in the life cycle of a virtual machine and should therefore be coded defensively. They should, in particular, be written to be thread-safe and to avoid deadlocks insofar as possible. They should also not rely blindly upon services that may have registered their own shutdown hooks and therefore may themselves in the process of shutting down. Attempts to use other thread-based services such as the AWT event-dispatch thread, for example, may lead to deadlocks.

Shutdown hooks should also finish their work quickly. When a program invokes exit the expectation is that the virtual machine will promptly shut down and exit. When the virtual machine is terminated due to user logoff or system shutdown the underlying operating system may only allow a fixed amount of time in which to shut down and exit. It is therefore inadvisable to attempt any user interaction or to perform a long-running computation in a shutdown hook.

Uncaught exceptions are handled in shutdown hooks just as in any other thread, by invoking the uncaughtException method of the thread's ThreadGroup object. The default implementation of this method prints the exception's stack trace to System.err and terminates the thread; it does not cause the virtual machine to exit or halt.

In rare circumstances the virtual machine may abort, that is, stop running without shutting down cleanly. This occurs when the virtual machine is terminated externally, for example with the SIGKILL signal on Unix or the TerminateProcess call on Microsoft Windows. The virtual machine may also abort if a native method goes awry by, for example, corrupting internal data structures or attempting to access nonexistent memory. If the virtual machine aborts then no guarantee can be made about whether or not any shutdown hooks will be run.

220 questions
2
votes
2 answers

In a Java application, is it a good idea to use a shutdown hook to auto-update the app?

The idea is to check for updates while the application is running. The update consists of one main jar and a couple of third party jars. If updates are available: Download them Put them in a temp directory Add a shutdown hook that replaces all the…
Chadi
  • 737
  • 1
  • 6
  • 21
2
votes
1 answer

Closing I/O streams during a shutdown hook

I am writing a server-client application in Java, and I want the server to send a message to the client in the event that it crashes or shuts down unexpectedly. Runtime.getRuntime().addShutdownHook(new Thread(){ public void run(){ …
jedyobidan
  • 876
  • 1
  • 8
  • 14
2
votes
1 answer

Shutdown hook how to distinguish normal and abrupt(by user) shutdown

In my application i have added a shutdown hook for some clean up and finishing last transaction. As all docs stated this hook is also called on "normal" shutdown - if all is finished. But i want to give the user some feedback: "ctrl-c detected -…
dermoritz
  • 12,519
  • 25
  • 97
  • 185
2
votes
0 answers

How do you detect the Windows XPe shutdown reason in Delphi?

I'm correctly intercepting the Windows shutdown message WMQueryEndSession in Delphi 2009 with the following procedure: procedure TMyForm.WMQueryEndSession(var msg: TMessage); This article suggests that I can distinguish between a regular shutdown…
Duncan
  • 858
  • 1
  • 11
  • 29
1
vote
6 answers

Setup ShutdownHook and exit application

I have the following code: public static void main(String[] args) { // login event String event = "login"; System.out.printf("Handling event: %s %s\n",event,getCurrentLogin()); sendMessage(event, getCurrentLogin()); // logout…
1
vote
2 answers

ShutDownHook in multi-threaded application

I have an application which in its main method spawns a hundred threads (let's say we simulate a hundred accounts). I am experimenting with it and I would like it to just print terminating when intterupted with Control-C. I read you can do that with…
user998388
  • 81
  • 2
  • 5
1
vote
2 answers

Prevent shutdown after shutdown hook

So, I should handle Ctrl-C. Ok, I can do that by registering a shutdown hook. And it works well until I want to prevent the shutdown. When Ctrl-C pressed I want to ask the user if he really wants to close the app, and if he isn't, then I want my app…
M--
  • 375
  • 1
  • 2
  • 11
1
vote
0 answers

How not to close Hikari pool right after Spring application gets close signal?

I have Spring boot application, that goes to database using HikariCP. The thing is, when application gets stop (SIGTERM or other) signal, it must go to database and change some data, after that it closes ("gracefull shutdown"). This logic is written…
programmer
  • 75
  • 8
1
vote
2 answers

Should EntityManagerFactory be closed at application shutdown?

I have a Java application that has a GUI made with Swing and that uses two databases interchangeably. One of the two databases is mongoDB and the other one is MySQL. Which database to use is chosen with a command line option. For the MySQL database…
ClaudiaR
  • 3,108
  • 2
  • 13
  • 27
1
vote
0 answers

ShutdownHook is not being called uppon pressing Ctrl+C if programm was started via gradle

I have a program which implements a Shutdown Hook in order to ensure everything is closed before the program terminates. If I launch the program from terminal the Shutdown Hook is executed after I press Ctrl+C. However if I launch the program with…
Beru
  • 657
  • 1
  • 6
  • 21
1
vote
0 answers

Illegal access error when deleting Google Pub Sub subscription upon JVM shutdown

I'm trying to delete a Google Pub Sub subscription in a JVM shutdown hook, but I'm encountering an illegal access error with the Google Pub Sub subscription admin client when the shutdown hook runs. I've tried using both sys.addShutdownHook as well…
1
vote
1 answer

Vertx stop method not executed on service stop

When I stop the service the stop is not getting called but if try the same in test case where I deploy and undeploy verticle with deployment ID the stop method is executing. EDIT:- I am creating jar file (not a shadow Jar). Below is build.gradle…
Ravat Tailor
  • 1,193
  • 3
  • 20
  • 44
1
vote
2 answers

Application continues to run after System.exit(0) is called - Java

I'm trying to clean up resources in my application before it shuts down, following on from my previous question (Detecting When A Java Application Closes) I have implemented the following code which performs the cleanup operation…
Jamie Keeling
  • 9,806
  • 17
  • 65
  • 102
1
vote
0 answers

How to change the response of rest contolloer during runtime

I need to achieve something similar to Executor.shutdown() vs Executor.shutdownNow() in the context of rest service (reactive in this case). I tried health status outOfService and down while carrying out graceful shutdown, but application still send…
sss
  • 598
  • 6
  • 24
1
vote
1 answer

Spring boot - Issue in invoking Shutdownhook

When i try to shutdown the spring boot application using ctrl+c or using process id using below script.. Shutdown hook is not invoked. Require some solution to invoke the shutdown hook both in windows and linux. Shutdown script: SET /P…
Jessie
  • 963
  • 5
  • 16
  • 26