-1

I am working on a JavaFX application that is to be run in background in most of time. So i need to execute code if a user close the application. But most of the time application will run in background/system tray so user may be forgot to close the application. So I need to hit the URL if user shutdown or restart the computer. After research I found Shutdown hook in java that can be used to excute task before JVM shutdown, so I implemented the Shutdown hook. When I tested it, it is working if i am closing the application, but not working if I shutdown the system.

Child class

public class ShutDownTask extends Thread {
    CheckInPageController controller = new CheckInPageController();
    @Override
    public void run() {
        System.out.println("ShutDown Called");
        System.out.println("Performing shutdown");
        //Hitting a URL here
            controller.checkoutRequest();

    }
}

Main class

    public static void main(String[] args) throws InterruptedException, IOException {
        launch(args);
                ShutDownTask task = new ShutDownTask();
        // add shutdown hook
        Runtime.getRuntime().addShutdownHook(task);
        // exit application
        System.exit(0);
    }

Please give me solution of it or tell me why is this happening Thanks in advance

  • Ankush so this hook beheviour is OS specific. did you get any java error when you shutdown the system? – Suchandra T Apr 03 '23 at 05:48
  • No, I did not get any error during shutdown. But I am thinking about internet is available after initiate shutdown? Because I tested hook by setting a preference and it is working fine if i shutdown PC. – Ankush Chaudhary Apr 03 '23 at 05:54
  • 1
    Sure even this is a concern. Ankush what I can suggest is writing a heartbeat kind of program. which on a timely basis writes something to a local file ok. anyways content of the file can anyway you can sync with your code to any other server. this solves many issue. – Suchandra T Apr 03 '23 at 05:57
  • 1
    I am not sure what code needs to be executed. If it is possible to 'clean up' after the fact, I would suggest writing the programstate to a file and delete that when handled and shutdown. If the shutdown was ungracefull (i.e. System shutdown without closing the program), you can check that during startup (check if programstate file is present). You can handle the 'cleanup' there and than, or simply restore that file-written programstate and continue. I hope this gives a few handles on how to solve the issue. Otherwise please share the nature of task you want to execute on termination. – n247s Apr 03 '23 at 06:17
  • Hello n247s, I am executing code to hit POST API in JVM hook by using java.net.HttpURLConnection. I am not sending any JSON data, the server auto creates data to save, I just have to hit the URL. I am sending a HTTP request on the server in shutdown hook. – Ankush Chaudhary Apr 03 '23 at 06:31
  • Read the [documentation](https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/lang/Runtime.html#addShutdownHook(java.lang.Thread)): *"Once the shutdown sequence has begun it is impossible to register a new shutdown hook"*. In the scenario you describe, `launch()` will not exit until the shutdown sequence starts, so your shutdown hook cannot be registered. – James_D Apr 03 '23 at 16:18
  • Related: https://stackoverflow.com/questions/42598097/using-javafx-application-stop-method-over-shutdownhook – James_D Apr 03 '23 at 16:22

1 Answers1

0

As noted in the comments, using a shutdown hook can be a handy way to perform non-essential tasks, but -- by its very nature -- cannot be relied upon.

This is not limited to Java -- C/C++ has a finalize function which is run when an object is garbage collected. This can be helpful in optimizing some tasks, but there is no guarantee it will ever be called.

One need not look far for ways an application might not shutdown properly: power failure, an uncaught exception or out-of-memory error, even an OS crash will happen sooner rather than later.

The heartbeat suggestion is excellent: while the shutdown routine is not guaranteed, your application will startup again eventually (provided you or someone else is still wants to use it!) Check the previous state that was saved in the heartbeat and take any necessary action during startup.

JonathanDavidArndt
  • 2,518
  • 13
  • 37
  • 49