1

Is there a way to delay a windows shutdown with Java? I always have the problem, that the system is killing the JVM before my ShutdownHook ran through completely.

Annotation: I have to perform action on a remote server when the system shuts down. This taked a while and I dont have the possibility to change the servers behaviour.

jntme
  • 602
  • 1
  • 5
  • 18
  • 4
    Well, the documentation of `addShutdownHook` explicitly says that shutdown hooks should be quick exactly for this reason. – RealSkeptic Nov 23 '15 at 11:47
  • And your application should be able to recover reasonably well even if it crashes, when shutdown hooks are definitely not called. – biziclop Nov 23 '15 at 11:49
  • @RealSkeptic As I said - the JVM kills the application BEFORE the ShutdownHook ran through. – jntme Nov 23 '15 at 12:55
  • @biziclop Nope - I have some action I have to run ONLY when the computer shuts down. – jntme Nov 23 '15 at 12:56
  • @bananatreedad There is a high level of absurdity in that requirement. You really should make sure that things are consistent throughout, rather than trying to reach a consistent state at shutdown (which may involve pulling the plug on the machine, resulting in nothing being run). – biziclop Nov 23 '15 at 12:58
  • Perhaps you misunderstood: the documentation says that it's the programmer's responsibility to write the shutdown hook to be quick enough so that the short time the operating system gives it before shutting the JVM forcibly will suffice for whatever is required. – RealSkeptic Nov 23 '15 at 13:04
  • added annotation to question to clearify my problem. this can't be a very complicated use case - or am I that wrong? – jntme Nov 23 '15 at 13:10
  • @RealSkeptic Sorry - understood now what you meant. But as I said - for my use case I need another solution as it seems. – jntme Nov 23 '15 at 13:16
  • 2
    @bananatreedad It isn't complicated, it's simply impossible. Think about it: what happens if the computer is just powered off? Or if the network connection goes down? Or if the remote server is down? Relying on sending a message just before shutdown is not a very good idea. The way around this is to send a message at regular intevals whenever your application is running. That way the remote side can assume your application has ceased running when it stops receiving the messages. – biziclop Nov 23 '15 at 13:32

0 Answers0