Reputation: 5
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 Runnable()
{
public void run()
{
System.out.println("Shutdown Hook is running !");
System.out.println("Application Terminating ...");
}
}));
System.out.println("Running");
}
}
I run the code in eclipse, go to debug tab where I can see it in running state.I do right click -> terminate .And shutdown hook won't execute.
Output printed :
Running
Running
Running
.
.
.
.
.
.
Upvotes: 0
Views: 853
Reputation: 103813
.. and eclipse is unlikely to grow the ability to send the nice 'please terminate' signal (which is called SIGTERM, which causes shutdown hooks to run), because java's Process API only has one method to exit the process, and this is implemented with sending SIGKILL.
If you want to test your shutdown hooks, just.. invoke System.exit(0)
within your own app, and you'll witness your shutdown hooks running.
Upvotes: 0
Reputation: 1471
You can't handle that case. It seems Eclipse is sending a SIGKILL which is not part of a graceful shutdown, and thereby cannot be caught. See https://stackoverflow.com/a/2541618/3560873
Upvotes: 4