简体   繁体   中英

Ensure the termination of a Application launched by Runtime.exec when tomcat is killed

I launch several external process via a call to

Runtime.getRuntime.exec("java myApp");

I monitor the correct launching and execution of the application with some java.concurent.Future. I can tell if the app is launched, in error, stopped. I can stop it.

But, when I kill the main application (the one who launches the others) the several processes of the children applications keep runnning and I lose control on them.

I looking for a way to ensure that when the mother application is stopped / killed / in error, all of the children process are killed as well.

What I have already done :

The main application runs in tomcat, so i listen for the destruction of the context, and kill all subprocesses. But it's not enough: when tomcat is stopped or killed, it's not working.

What you can do is - make your applications open a connection to the tomcat (on localhost:port) in certain intervals, and if it is not there - System.exit(0) . For example:

try {
    new URL("http://localhost:8080").openConnection().getContent();
} catch (IOException ex) {
    // do app finalizations
    System.exit(0);
}

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