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
0
votes
2 answers

Java detect when application is closing

I learned Java for about 1 year some years ago. Now I wanted to go back to it, but my skills got a bit rusty. So I'm using Eclipse with the WindowBuilder to make an easy Form Application to get started again. I wanted to get a popup window when I…
0
votes
2 answers

Spring Boot - How to register a shutdown hook for a non-GUI application

I am developing an application which is basically a service that will be run with the command line. I do have an option in the config file for it to display a GUI. If the user chooses to have it display the window then I can call my shutdown()…
Martin
  • 1,977
  • 5
  • 30
  • 67
0
votes
1 answer

Java Poller Thread cleanup before JVM shutdown

I have a Java application which has some infinitely running threads polling the back-end Database. When the JVM is shutdown (via weblogic UI) I need to perform some cleanup operations for each of the threads, like connecting to DB and setting status…
0
votes
1 answer

Shutdownhook - Log not printed in console/File @Predestroy method

Hav spring boot application, While invoking Shutdown hook @Predestroy method is called and application shutdown happens. But Logs are not printed in both console and file, sys out line after the log is printed. It seems, Looger console shutdown…
Jessie
  • 963
  • 5
  • 16
  • 26
0
votes
0 answers

Java: Threading in Shutdown Hook

I have a manager class that allows sub-modules to register a shutdown-hook using Runnable. public class ApplicationManager() { private final List shutdownHooks; private ApplicationManager() { // Other stuff …
Jai
  • 8,165
  • 2
  • 21
  • 52
0
votes
2 answers

Restart Akka Actor System after terminated

We have an Akka http app with approx. 100+ API and 15+ Actors. After Http.bindAndHandle(routes, host, port) I have terminated ActorSystem. Http().bindAndHandle(corsHandler(routes), "0.0.0.0", 9090/*, connectionContext =…
Nilesh
  • 2,054
  • 3
  • 23
  • 43
0
votes
1 answer

Allow rabbitmq to process current running message before shutdown

My application is spring boot micro service listening to a Rabbit MQ queue. The queue receives messages from different sources. The requirement is that when the application server is going down (this could happen because of many reasons, may be…
GGKamal
  • 21
  • 6
0
votes
1 answer

Running a Windows service, and cobertura

I'm trying to run a windows service with cobertura. The only problem is cobertura reports results when the shutdown hook is executed. I am unable to directly modify the code for these results, so I was wondering if it is possible to run a java…
Steve
  • 1,145
  • 1
  • 11
  • 25
0
votes
0 answers

Java ShutdownHook with server

I'm writing a basic server in java. I have a Server class that waits for requests: class Server{ public void listen(){ ServerSocket listenSocket = new ServerSocket(port); while(true){ Socket clientSocket =…
0
votes
1 answer

Low logging level preventing shutdown hook to run properly

I am using the MariaDb4j library for my integration tests and it registers a shutdown hook, this way: protected void cleanupOnExit() { String threadName = "Shutdown Hook Deletion Thread for Temporary DB " + dataDir.toString(); final DB…
Bentaye
  • 9,403
  • 5
  • 32
  • 45
0
votes
2 answers

Find out when shutdown -s is being executed

I have a main computer that knows when there is a power outage. That main computer will then tell all workstations to shutdown over the network using the built in "shutdown /m \computername /s" command. That main computer will also tell individual…
Forivin
  • 14,780
  • 27
  • 106
  • 199
0
votes
1 answer

`exit` event in node.js behaves different than in the manual

https://nodejs.org/api/process.html Listener functions must only perform synchronous operations. The Node.js process will exit immediately after calling the 'exit' event listeners causing any additional work still queued in the event loop to…
Daniel W.
  • 31,164
  • 13
  • 93
  • 151
0
votes
1 answer

Shutdown hook exits automatically when debugging it

I set a breakpoint in the shutdown hook, and debug it in intellij. But it looks like it has some timeout that it would automatically exit after some time.
zjffdu
  • 25,496
  • 45
  • 109
  • 159
0
votes
0 answers

Remove Shutdown Hook of another Java process

I have a Java application that I want to shutdown gracefully in Windows. I am starting this application using a .bat file and I am able to stop it gracefully if I press CTRL+C from keyboard. Sending a SIGINT/SIGTERM signal to an application is easy…
blu3
  • 123
  • 1
  • 12
0
votes
1 answer

Java(FX) Fail-Safe Alert on Faulty Termination / Crash?

I absolutely need my programme to exit cleanly, i.e. if and only if the user does it manually. In any other case, my programme must notify them either by means of displaying a popup or, better yet, playing a sound (on loop). Hence: is there a…
MightyMalcolm
  • 191
  • 1
  • 16