简体   繁体   中英

Java Shutdown function not executing

Within the main() of my application I have the following code to back up data so it doesn't get lost in the event of a system shut down.

    //add hook to trigger Production Shutdown sequence
    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
        public void run() {
            Production.shutdown();
        }
    }));

However, whether I press the Stop button in my IDE or rely on input via the log (code shown below) it never seems to save data to the database or write any logs to the console.

    ctx.deploy(server);

    server.start();

    //start the production process
    Production.init();

    System.in.read();
    server.stop();

How come this shutdown function is not being executed?

You need to use the Exit button, not Stop , see my answer here for more details.

Note that this feature is currently available only in Run mode, not in Debug.

System.exit(0)

add this line in your code. Debug from here onwards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM