views:

108

answers:

1

Hi,

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.

+1  A: 

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);
}
Bozho
Good idea. But i dont understand why this is OS Specific.
Antoine Claval
my suggestion is not OS specific. But on some OS killing the tomcat _might_ kill its subprocesses
Bozho
Ok. For what i'v seen, the process are keeped alive on win vista and ubuntu 8.04. Thanks for your sugestion. I think that will do the trick. ( i will test it right now )
Antoine Claval
Ok it's working. Bozho, can you add a getContent() to your UrlConnection object ? like this : new URL("http://localhost:8080").openConnection().getContent() ;Because i remark that even if tomcat is not running, the openConnection() doest fail each time. When i ask for the content of the page, it's fail each time.
Antoine Claval
@Antoine Claval - nice, I updated the answer
Bozho