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
4 answers

Issue running shutdown hook in java

I am trying to get a shutdown hook to run in java. The purpose of this hook is to write into a log file that the program was terminated. The java application is compiled into a jar file and executed using a batch file, which means that the output of…
Drifter64
  • 1,103
  • 3
  • 11
  • 30
0
votes
2 answers

Handle tomcat stopping

We use exchange server for getting email and open connection for 30 minutes and then automatically reopen it for next 30 minutes. But when we want to deploy new war-file and stops tomcat service, connection doesn't close while connection period…
Vitaliy Borisok
  • 822
  • 3
  • 11
  • 21
0
votes
1 answer

Queries regarding Shutdown hook

If i have gotten it correctly each java process is associated with a separate instance of JVM and each instance of JVM is provided with a Heap memory by the OS which is also recollected by the OS on JVM termination. So on termination even if there…
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
0
votes
1 answer

java shutdown hook "system cannot find message text"

I have the following shutdown hook in the main method of my Server class: Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { if (open) { open = false; //log out all players …
bumbleshoot
  • 1,082
  • 2
  • 17
  • 32
0
votes
1 answer

Prevent EclipseLink to close connection before shutdown hook executes

It looks like EclipseLink is closing the connection before the shutdown hook executes. (I am not 100% sure but from my experiments it looks that way.) I need to do something inside the shutdown hook, so I need to prevent EclipseLink to close the…
user2796118
0
votes
1 answer

how can we use addShutdownHook in project?

i saw some code use ShutdownHook like this Runtime.getRuntime().addShutdownHook(new Thread(){ ConfigurableApplicationContext.stop(); //close spring context; threadpool.shutdownnow(); //close theadpool }); is there anything useful…
martin
  • 1
  • 1
0
votes
1 answer

Neo4j - database shutdown

I am using Neo4j embedded in my Scala project. I have been including ShutdownHookThread { shutdown(ds) } the above piece of code in each and every function before the beginning of transaction. Do I need to include it in every function. What…
yAsH
  • 3,367
  • 8
  • 36
  • 67
0
votes
1 answer

Java ShutDownHook not being fired on Windows

Ive got the following ShutdownHook to detect when the application is exited: Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { public void run() { // Do what you want when the application is stopping …
Alosyius
  • 8,771
  • 26
  • 76
  • 120
0
votes
1 answer

Can a servlet filter's destory method be called by a shutdown hook or is there a better solution?

I have a Java servlet filter that should clean up some resources whenever the filter is not longer used. This is achieved by implementing the Filter.destory method. Now I wonder if this method is also called when the entire servlet container is…
Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192
0
votes
2 answers

Shutdown hook is not killing executor

I have the following code: public class Driver { private ExecutorService executor = Executors.newCachedThreadPool(); public static void main(String[] args) { Driver d = new Driver(); d.run(); } private void run() { …
user1768830
0
votes
2 answers

Can a headless JAR add a shutdown hook to the main thread?

I would like to create a reusable JAR that I would share across many of my projects. In this JAR, when a certain method is invoked, several threads need to be created. I'd like to be able to add a shutdown hook (Runtime.addShutdownHook(...)) that…
user1768830
0
votes
1 answer

Alternative to nohup when input user is requested

I've got a java application which is started with a nohup command. Now the stop command is just a kill -15 of that application. This application is a file parsing and write to db application and it has a shutdown hooker which intercepts the kill…
BangTheBank
  • 809
  • 3
  • 11
  • 26
0
votes
1 answer

iOS 5 - shutdown hook

I would like to perform certain cleanup tasks when the app shuts down. I use an observer like: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillResignActiveNotif:) name:UIApplicationWillResignActiveNotification…
Resh32
  • 6,500
  • 3
  • 32
  • 40
0
votes
1 answer

Any module to start, shutdown Java process (like Jetty start.jar)?

Most servers like Jetty or Weblogic provide some sort of mechanism to start or shutdown the server via a call (sometimes over jmx). Can always to keep stuff running from main: public static void main(String args[]) { try { Setup Spring…
Matthew Campbell
  • 1,864
  • 3
  • 24
  • 51
0
votes
1 answer

Is it possible to collect information about a running .jar file when it exits?

is it possible to gather information on all the existing variables within a running .jar file when it exits? I'm running an application on the linux command line of various servers and I try to debug an issue when the jar just stops working on 10%…
Dennis Winter
  • 2,027
  • 4
  • 32
  • 45
1 2 3
14
15