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
1 answer

ShutdownBlockReasonDestroy when real power button down

Prevent Shutdown on Windows almost using windows API ShutdownBlockReasonCreate when application received "shutdown signal" ,windows message is 17 (need set AutoEndTasks registry key to 0) simple test with Notepad,if shutdown from start menu…
0
votes
1 answer

Hooking up into OS shutdown event in .NET WPF-Application on Win7 x64?

This seems to be mission impossible? According to the comments on this page: http://msdn.microsoft.com/en-us/library/system.windows.application.exit.aspx , others also have trouble archieving it. Which event is the best one to use? Session.Ending?…
Sascha
  • 2,193
  • 3
  • 24
  • 38
0
votes
1 answer

ShutdownHooks on Tomcat for Threads

I am currently starting some threads in Servlet class like this: public class GTCInitServlet extends HttpServlet implements Runnable { public GTCInitServlet() { super(); } public void init(ServletConfig config) throws…
hell_storm2004
  • 1,401
  • 2
  • 33
  • 66
0
votes
0 answers

Avoid memory leak ThreadLocal SimpleDateFormat

I'm working with jdk7 on weblogic 11g, as I would to use ThreadLocal variable to overcome issues with SimpleDateFormat and thread safety, I also thought about potential memory leak, in case I don't remove the current value. In order to do that, I'm…
Raffaele
  • 461
  • 1
  • 7
  • 20
0
votes
2 answers

Issue with Java Shutdown hook

My test code is like following : public static void main(String[] args) throws IOException, InterruptedException { // TODO Auto-generated method stub while(true) { Runtime.getRuntime().addShutdownHook(new Thread(new…
0
votes
1 answer

In Java, how can I limit the shut down hook only support SIGTERM?

I found that the runtime shutdown hook in Java can handle SIGTERM, SIGHUP, SIGINT and normal exit(0). However, I only want the hook to handle the SIGTERM signal. How can I limit it? Simple example from geeksforgeeks: public class ShutDownHook { …
Tianbo Zhang
  • 27
  • 1
  • 4
0
votes
0 answers

Is there a hook for Spark cluster termination?

I see there is a hook for when the driver JVM is shut down : Runtime.addShutdownHook(...) Is there a hook for when the cluster is terminated? I want to copy some files from the driver when the cluster is terminated so I don't lose them. Thanks!
Gadam
  • 2,674
  • 8
  • 37
  • 56
0
votes
1 answer

Best way to run exit logic on accidental pod shutdown

I am working on a spring boot application and we deploy through kubernetes. My requirement is to run some logic in case the pod crashes or pod gets removed or pod is intentionally shut down. Currently I am using @PreDestroy to run my logics on…
smruti ranjan
  • 741
  • 2
  • 9
  • 23
0
votes
1 answer

Understanding a java log-file

As a beginner technical support, I received a logfile from our client written in Java. The client cannot preview a document. I need some help in order to understand the system messages in the log file in order to explain the issue to the client with…
user7591558
0
votes
2 answers

Spring - execute query before application shutdown (tests)

I'm running SpringBoot 2.1 with Sprind Data JPA/Hibernate as persistence layer. I run into problem to succesfully run query, in my tests, before application shutdown. Details: During application context startup I'm executing a query via JPA (let's…
0
votes
1 answer

How is VM_Exit delivered to VM Thread when sending SIGTERM and who does the delivering?

I'm trying to understand how JVM hotspot handles termination signals (SIGTERM for example). What I could found is that the SIGTERM signal disposition is set at this point to UserHandler which looks as (comments ommitted): static void UserHandler(int…
Some Name
  • 8,555
  • 5
  • 27
  • 77
0
votes
0 answers

log both normal and unhandled exception-shutdown

I want to write a json file recording the execution duration and unhandled exception if any once. Java provides Runtime.addShutdownHook and Thread.setDefaultUncaughtExceptionHandler However, Runtime.addShutdownHook is called regardless of whether…
user21479
  • 1,179
  • 2
  • 13
  • 21
0
votes
1 answer

Spring Boot / JavaFX : adding a shutdown hook (ctrl-c) that still has access to JPA

This question has been asked a few times but I have not found a post that describes my situation exactly. I have a JavaFX/Spring boot based application that is required to perform some cleanup tasks before shutting down. I can intercept the event…
Martin
  • 1,977
  • 5
  • 30
  • 67
0
votes
0 answers

How to exit with a non-zero exit code from within shutdown hook?

I have a javaagent which collects certain info while the app is running. It also registers a shutdown hook which at the time of the app exit compares the collected data with some golden data and is expected to exit with a non zero exit status if the…
0
votes
0 answers

Java application terminating with error code when using shutdown hook

I have a ServerSocket application that terminates when the user type Ctrl + C on the terminal. The application termination cleanup looks fine, but when I tried to see the returned code of the process it gives me an error code 143 for SIGTERM and 130…
Michael Pacheco
  • 948
  • 1
  • 17
  • 25