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

Spring Boot Shutdown Hook terminated halfway

I have added ShutDownHook to my Spring Boot Application. When I pass SIGTERM to my application, shutdown hook got triggered but it is terminated in halfway i.e, in middle of execution. Googled it and tried many solutions it is not working. Some…
Raashith
  • 155
  • 3
  • 16
1
vote
1 answer

SimpleMessageListenerContainer shutdowntimeout

I am using spring-rabbit-1.7.3.RELEASE.jar I have defined a SimpleMessageListenerContainer in my xml with shutdownTimeout parameter. bean id="aContainer" class="org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer">
1
vote
0 answers

Shutdown hooks in Spring

I am trying to add a shutdown hook via Runtime.getRuntime().addShutdownHook() in a Spring Gradle application. I tried adding an anonymous thread subclass to the Application.java class from this exact tutorial So that it looks like this: package…
efong5
  • 366
  • 1
  • 6
  • 21
1
vote
1 answer

Spring boot graceful shutdown mid-transaction

I'm working on a spring-boot service that performs sensitive payment processing, and would like to ensure that any shutdown to the app will be done without interrupting these transactions. Curious on how to best approach this in spring-boot. I read…
sa125
  • 28,121
  • 38
  • 111
  • 153
1
vote
3 answers

Java shutdown hook not launching my Process

I'm trying to give to my app self update ability. It download an JAR from my website and save it as myapp.jar.new. After that, I want to launch a command to delete the current version and rename the new one. This is my code (see the notes): public…
Beto Neto
  • 3,962
  • 7
  • 47
  • 81
1
vote
1 answer

Is there a Symfony register_shutdown_function / destruction equivalent for executing code post-content delivery?

I need various things to happen after the user has been sent a response, like how register_shutdown_function used to work. I've had a play with sfShutdownPlugin, it just uses register_shutdown_function, I've also had a look at using a destructor…
Steve
  • 5,771
  • 4
  • 34
  • 49
1
vote
1 answer

LWJGL - OpenGL context gets lost in shutdown hook

I'm currently working with Java and LWJGL 3 and I'm writing some wrappers for stuff like vertex array objects, vertex buffer objects, etc. Now it's a good habit to delete these objects before program exit, so I created a shutdown hook which is…
scewps
  • 91
  • 1
  • 13
1
vote
1 answer

How to register a function to be called before R script is exited

I'd like to do some cleanup work before the R process is exited by accidently. Is there any api like java's shutdown hook in R ? Thanks
zjffdu
  • 25,496
  • 45
  • 109
  • 159
1
vote
0 answers

Pattern for shutting down multi-threaded java spring boot application

We are contemplating what would be the best way to gracefully shutdown our spring boot application. We are using boot's shutdown endpoint and a shutdown hook to get notified on a shutdown. The application is very heavily multi-threaded. It is…
dor.elmaliach
  • 515
  • 5
  • 14
1
vote
1 answer

flush printwriter on application close

So I have a printwriter that will occasionally write to a file ("occasionally" here meaning "periodically after a certain number of specified events"). Now if these events happen to be too few, the log file may end up empty when the application is…
User1291
  • 7,664
  • 8
  • 51
  • 108
1
vote
0 answers

How to tell a specifc bean to be destroyed after all other bean

How to destroy a spring bean after the destruction of all other beans. I want my bean being destroyed as the last one when the context is closed.
user3021914
  • 143
  • 1
  • 3
  • 13
1
vote
1 answer

ShutdownHook code not being executed on System.exit(0)

I have Shutdown hook that i attach to runtime Runtime.getRuntime().addShutdownHook(new ShutDownHook(false)); Here is a shutDownHook Class public class ShutDownHook extends Thread { private final boolean interupt; public…
Tomas Bisciak
  • 2,801
  • 5
  • 33
  • 57
1
vote
2 answers

Java shutdown hook not running

I have a product service in Java. In our code I am creating shut down hook, but when I stop service it is not calling shut down hook consistently. Out of 5 stop calls it has called shutdown hook only once. Runnable shutdownHandler = new Runnable()…
user1108687
  • 209
  • 1
  • 2
  • 13
1
vote
1 answer

Shutdown Hook with windows-scripting-host

This link discusses how a shutdown hook can be implemented using VB. Now my question is, can something like that be done using Windows Script Host/windows scripting host? I would very much appreciate your input; maybe include some code snippets.
Klaus
  • 35
  • 1
  • 4
1
vote
1 answer

What are the differences between Stage.setOnCloseRequest() and Runtime.addShutdownHook()?

When writing a JavaFX application, you can provide a shutdown hook to any javafx.stage.Stage with the setOnCloseRequest(EventHandler) method. If you do that with your main JavaFX Scene, it serves as an application shutdown hook, and can…
Ramiro
  • 698
  • 6
  • 21