0

I have code (runtime between 1 and 2 seconds) which I want to run everytime the computer shuts itself down (logging off a user out of a another system on another server).

I have already tried to add it to the ShutdownHook but the execution of the overgiven Thread is to slow - the thread gets killed before all commands could be executed.

Also tried to add it to a finally statement, that didn't help anything too.

What is IYO common practise for that case?

THX a lot.

jntme
  • 602
  • 1
  • 5
  • 18
  • Similar question here: http://stackoverflow.com/questions/9277630/windows-shutdown-hook-on-java-application-run-from-a-bat-script – orange Nov 03 '15 at 16:13
  • http://stackoverflow.com/questions/6816858/detecting-that-jvm-is-shutting-down – Keval Nov 03 '15 at 16:14

1 Answers1

1

You will not get this to work robustly. ShutdownHook will only be called if the JVM is closed down gracefully. Ungraceful closes include the OS killing the process, or even a power failure.

An alternative in this instance is to maintain a "heartbeat" signal between your computer and the server which, if severed, logs the user off the server.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483