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

How to add shutdown hook in Apache Flink job?

I'm using redis connection pool in my flink job. I need to release all connections when the job get cancelled or finished. How can I add a shutdown hook for my flink job?
xingbin
  • 27,410
  • 9
  • 53
  • 103
3
votes
1 answer

Difficulty to understand when is shutdown hook executed and how to terminate an ExecutorService

I'm trying to run a program based on a scheduleAtFixedRate ExecutorService. I use this service to replace a dummy while loop. The global idea is to have first an ExecutorService (the scheduler) executing a Runnable (the runnable). then, for some…
machinus
  • 153
  • 2
  • 13
3
votes
0 answers

Spring boot - Tomcat proper shutdown procedure

I have a sprig boot app leveraging websockets using tomcat. The app is writing stuff to the database constantly, and now I would like to implement a proper shutdown procedures i.e. I have found a way to tell in my Service Layer to wait until all…
Tito
  • 2,234
  • 6
  • 31
  • 65
3
votes
1 answer

How to finish kafka consumer safety?(Is there meaning to call thread#join inside shutdownHook ? )

I am reading this article And here code to finish the consumer thread: Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { System.out.println("Starting exit..."); consumer.wakeup(); 1 …
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
3
votes
1 answer

Different shutdown-hook behavior on Windows shutdown

In my Eclipse E4 RCP application, which runs with Java 7, I have the problem that the shutdown-hook is not executed when Windows (7) is shutting down or logging off. After some research I found the following Open JDK bug entry, which creates an…
gillesB
  • 1,061
  • 1
  • 14
  • 30
3
votes
2 answers

sys.exitfunc not working in python

I am trying to run following simple code import sys print("Starting Test Python Module"); def testmethod(): print("From test method") sys.exitfunc = testmethod print("Terminating Test Python Module"); and it…
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
3
votes
1 answer

Any way to shut down Java process programmatically but not invoke the JVM shutdown hook?

I use System.exit(), but it would always call the JVM shutdown hook. How can I shut down a Java process programmatically but not invoke the JVM shutdown hook ?
zjffdu
  • 25,496
  • 45
  • 109
  • 159
3
votes
2 answers

Java shutdown hook call on windows shut down

I have a situation in which I want to perform some task when the user signals the OS(in my case only Windows) to shutdown. I have tried using java shutdown hooks. The problem I face is that when I exit the program using System.exit(0);, the…
Pranav
  • 323
  • 1
  • 3
  • 16
3
votes
1 answer

EventBus google guava shutdown hook

currently I am using the guava EventBus approach in my application. The listener tries to do some work and if it fails, the event should be back in the bus and resent. My question is: what if my application is going down (performing a shut down)?…
Blanca Hdez
  • 3,513
  • 19
  • 71
  • 93
3
votes
1 answer

Why does the official JVM documentation contradict its implementation in addShutdownHook?

There's a bit of a clash for whether shutdown hooks, which are of class Thread, run their runnable code on the thread on which the shutdown was called, or run on themselves. addShutdownHook takes a Thread as a parameter. This implies the thread will…
djechlin
  • 59,258
  • 35
  • 162
  • 290
3
votes
5 answers

Is there an inverse equivalent to the Java Runtime ShutdownHook i.e. StartupHook?

I am looking for a general inverse equivalent of the ShutdownHook in Java Runtime i.e. something like a StartupHook where certain custom warmup or setup code can be executed when the JVM first starts up. I am aware of alternatives like the use of…
tsaixingwei
  • 636
  • 9
  • 13
3
votes
1 answer

What are the best practices to manage an ExecutorService in a library?

In one of my libraries, I use a fixed thread pool executor with 5 threads; my threads are not heavyweight, I .get() with timeouts, but as to the ExecutorService, I create it and after that, this is "live and let die". You are supposed to…
fge
  • 119,121
  • 33
  • 254
  • 329
2
votes
1 answer

Do Java shutdown hooks get called on Heap OOM?

addShutdownHook says: 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…
cozos
  • 787
  • 10
  • 19
2
votes
0 answers

Universal connection Pool shuts down automatically before application shutdown hook executes

I have my java application running on jetty which uses JDBC (uses UCP) to make database connection. Problem I am facing is during jetty shutdown, before my application shutdown hook executes, UCP goes on auto shutdown which is not desired since I…
Rajat
  • 103
  • 2
  • 12
2
votes
1 answer

Can shutdown hooks be used for slightly longer task

I have come across the shutdown hooks functionality which is invoked when java application is shutdown. I am using SIGTERM signal to shutdown my java process on a linux Box. And at the time of shutdown, I want to do some disk I/O (persisting some…
MoveFast
  • 3,011
  • 2
  • 27
  • 53