Java Forum / General / June 2006
Stopping a Java Program
TheRhythmic@gmail.com - 23 Jun 2006 14:31 GMT Hi everyone,
This' the issue am facing:
A java application is runs forever. I want to provide the users a facility to shut it down whenever they want to. How to do this?
I tried writing another class, say class A, which does this: Runtime r = Runtime.getRuntime(); r.exit(o);
I started the App. Then when I wanted to shut it down, I executed class A's method by using the 'java' command. But this doesn't seem to help in stopping the App. A's method executes successfuly, but I think it starts a new JVM & executes in it & so the exit() command doesn't have effect on the JVM in which the App is running.
One more method I thought of is to execute a method in the same JVM & do an 'exit(0)' to terminate the JVM so that App will shut down. But donno how this'll be possible because 'java' command starts a new JVM.
Please give your suggestions on what can be done.
Thanks.
EdwardH - 23 Jun 2006 14:41 GMT > One more method I thought of is to execute a method in the same JVM & > do an 'exit(0)' to terminate the JVM so that App will shut down. But > donno how this'll be possible because 'java' command starts a new JVM. System.exit(0);
I don't see what the problem should be.
TheRhythmic@gmail.com - 23 Jun 2006 14:45 GMT Hi Edward,
Thanks for your quick reply.
This' the scenario: Program A is running forever. I wanna stop it. How do I do it?
If I write another java program, say program 2, which says 'System.exit(0)', this doesn't work. I think because when I run java program 2 using java command line tool, it executes in a new JVM I think.
Thanks.
> > One more method I thought of is to execute a method in the same JVM & > > do an 'exit(0)' to terminate the JVM so that App will shut down. But [quoted text clipped - 3 lines] > > I don't see what the problem should be. EdwardH - 23 Jun 2006 15:18 GMT > Program A is running forever. I wanna stop it. How do I do it? Oh. You want to kill program A from outside of itself.
So find the java process that's running it and kill it.
"ps aux" and then "kill <pid>"
Moiristo - 23 Jun 2006 15:39 GMT >> Program A is running forever. I wanna stop it. How do I do it? > [quoted text clipped - 3 lines] > > "ps aux" and then "kill <pid>" To do it more nicely, I'd say that you create a public method A.shutDown(), which simply executes 'System.exit(0);' when it is called by App B
Matt Humphrey - 23 Jun 2006 16:06 GMT >>> Program A is running forever. I wanna stop it. How do I do it? >> [quoted text clipped - 7 lines] > A.shutDown(), which simply executes 'System.exit(0);' when > it is called by App B App B cannot invoke a method in a different JVM. App A must provide some communication protocol (socket, RMI, stream read, shared file / mem, signal handler) in order to be controlled externally. If App A provides no such mechanism, you must use an OS-dependent technique to kill the process.
If you can modify the application, a common approach is to add a mini server that accepts a client connetion as an administrative interface. A tiny client app can then be used to control the main application. Some applications use clever command-line arguments to gives the appearance the app is invoking itself, even though what's happening is a different application is communicating with the main app.
Cheers, Matt Humphrey matth@ivizNOSPAM.com http://www.iviz.com/
jsillitoe@gmail.com - 23 Jun 2006 20:55 GMT Matt is exactly right on the money for this. I have written several applications that behave the exact same way. If you need more confirmation, this is exactly how Tomcat is managed also.
> >>> Program A is running forever. I wanna stop it. How do I do it? > >> [quoted text clipped - 22 lines] > Cheers, > Matt Humphrey matth@ivizNOSPAM.com http://www.iviz.com/ M.J. Dance - 23 Jun 2006 16:09 GMT > Hi Edward, > [quoted text clipped - 7 lines] > program 2 using java command line tool, it executes in a new JVM I > think. That's right. Starting java twice results in two java processes. Stopping one doesn't ( necessarily ;) ) stop the other. But you can stop it anyway.
If you have access to program-A's console, you can just press Control+C.
Otherwise...
1. If you run some flavor of Linux (Unix?), you do the following:
1.1. Find out what program-A's pid is:
ps -A | grep java (where -A is a switch, not program-A's name)
or
ps x | grep java
1.2. and then
kill <pid>
If the process doesn't want to die, you turn the radical-gauge nine notches up ;-) and
kill -9 <pid>
See also: killall.
2. If, on the other hand, you use Windows, just use Task Manager (End Process, End Process Tree).
steve - 23 Jun 2006 23:13 GMT >> One more method I thought of is to execute a method in the same JVM & >> do an 'exit(0)' to terminate the JVM so that App will shut down. But [quoted text clipped - 3 lines] > > I don't see what the problem should be. it could be that the program is launching multiple threads, the main program can exit , but sometimes the threads hold onto the instance of the jvm.
I do this with a boot loader, the bootloader hits an exit(0) and dies, but my other threads continue the application.
you need to close the other threads, or anything else you have launched from your app, before the exit will fully close the JVM
steve
Rob - 24 Jun 2006 16:48 GMT > it could be that the program is launching multiple threads, the main program > can exit , but sometimes the threads hold onto the instance of the jvm. According to http://java.sun.com/j2se/1.3/docs/api/java/lang/Thread.html the JVM will terminate when System.exit() is called, regardless of the type of threads still running, or when the only threads running are daemon threads
Simon - 23 Jun 2006 14:45 GMT > A java application is runs forever. I want to provide the users a > facility to shut it down whenever they want to. How to do this? [quoted text clipped - 12 lines] > do an 'exit(0)' to terminate the JVM so that App will shut down. But > donno how this'll be possible because 'java' command starts a new JVM. I assume you don't have access to the programs source code. You can try the following:
Find the main class of the application, say app.Main, put the application and your class A on the classpath and do the following.
* start app.Main.main() in a new thread from A.main() * Wait for the user action that is supposed to terminate the app. * Exit the JVM
like this:
public class A {
public static void main(final String[] argv) { new Thread() { public void run() { app.Main.main(argv); } }.start(); waitForNotificationAboutUserAction(); System.exit(0); } }
Of course, that doesn't help if the app is already running, but then anyway the only way to stop it is to kill it.
Cheers, Simon
John O'Conner - 23 Jun 2006 16:31 GMT > Hi everyone, > > This' the issue am facing: > > A java application is runs forever. I want to provide the users a > facility to shut it down whenever they want to. How to do this? Your app needs a processing loop in which it checks for a SHUTDOWN flag. In addition, the app should have a thread that listens for a shutdown message that it receives via a socket or shared memory (a file maybe). When the external message is received, the thread sets the SHUTDOWN flag. THe processing loop will eventually see the flag, and then it will have the oppty to shutdown gracefully.
-- John O'Conner
TheRhythmic@gmail.com - 23 Jun 2006 20:22 GMT Hi,
Thanks to everyone for your replies. One more method I thought of: Isn't it possible to obtain a reference to A & then use that reference object to stop it?
Thanks.
> > Hi everyone, > > [quoted text clipped - 12 lines] > -- > John O'Conner jsillitoe@gmail.com - 23 Jun 2006 20:58 GMT No, or at least not that I know of. Normally when you start a java program it maintains it's own JVM for execution. This provides a layer of separation between instances of a program running, even if it is run from the same code base.
> Hi, > [quoted text clipped - 20 lines] > > -- > > John O'Conner lordy - 23 Jun 2006 22:12 GMT > Hi, > [quoted text clipped - 3 lines] > > Thanks. I just wrote something (may be a better way to do it, so here goes ...) , Every thread registers itself with the ShutdownManager at a certain shutdown level.. If the thread is blocking on a socket then it registers that socket..
Then the thread goes about its busienss like this..
try ShutdownManager.register(socket,1); while(!ShutdownManager.shuttingDown(socket) && other stuff ) { process input } ShutdownManager.unregister(socket); } catch (Exception e) { ShutdownManager.unregister(socket); }
Then the ShutdownManger. will cycle through the shutdown levels calling either Thread.interrupt(), Socket.close() etc..
Generally the server sockets are at level 1. SessionSockets level 2. Output threads etc. level 3. Core processes (eg internal hsqldb) at highest level.
The ShutdownManager enters a sleep, loop waiting for a shutdownRequest from the rest of the app... This sets a flag and interrupts the sleep loop.
There's probably a nice class out there that does this properly!
Lordy
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|