Reputation: 21
I have a "main" java application that will start another java applications each one on its own JVM. The "main" application will, eventually, suspend, resume, or kill the other applications. Restriction: if the "main" application dies, the other applications must still run.
How to do that?
I've used JDI (java debugger interface) but if the "main" application ends, the other applications are stopped also.
Upvotes: 2
Views: 1501
Reputation: 70909
I'm guessing you're talking about a multi-threaded application. The obvious solution would be to subclass the threads and embed the ability to set flags in the thread.
public void run() {
while (keepRunning && notSuspended) {
doNextItem(...);
}
while (notSuspended == false) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// do nothing but continue
}
}
}
public void suspend() {
notSuspended = false;
}
public void resume() {
notSuspended = true;
}
public void kill() {
keepRunning = false;
}
Other solutions that capture the thread from within the internals of the JVM are possible (like using the JDI interface); however, it is not really feasible to design code to maintain a consistent state when the threads are being interfered with externally. A design that internalizes the resting of the thread in a consistent state is much preferred if you want to actually verify that the program state is correctly consistent after a thread was paused.
Upvotes: -1
Reputation: 4812
You can try using Runtime to issue commands to launch your other applications(just like going to the CLI and issuing a java something.jar command.
Then also use the same thing to kill the other applications you launched but you probably need to get hold of their process id(pid) maybe using this
Upvotes: -1
Reputation: 10273
The only way I see is, as you say, to use JDI. But instead of using "local debug", you can try to use "remote debug" (debugging the child JVMs through a TCP port). This should avoid the children JVM stopping even if the main JVM dies.
Upvotes: 2
Reputation: 81694
Could you just write the other applications to open a ServerSocket
and accept commands over it? It would be easy enough to set up a simple command processor that understands "suspend", "resume", and "kill". The child could choose a random port to listen on, and print the port number when it starts, so the parent could easily get that information, then contact the child via a TCP connection.
Upvotes: 1