start / stop a java application (newbie)

D

David

Hi everybody,

I have a small application running a perpetual loop to listen events
from a database. I'd like to add a START / STOP control...

For example :
java MyApp -START should launch the process (infinite loop)
java myApp -STOP should stop the process

Thanks a lot in advance for an example... (Don't forget I'm a newbie)

I already coded a "addShutdownHook", but this is not really what I
want. Furthermore, I'm not able to properly interrupt my loop because
I never reach the /*never come here*/ section.... Maybe somebody
could have a look? I guess the problem is the ShutdownHook runs in a
separated thread...

class MyListener {

public static int i_interrupted = 0;

public static void main(String[] args) throws SQLException,
IOException {

Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
System.out.println("My listener received a kill");
i_interrupted = 1;
}
});

do {

try {
System.out.println("Sleeping 5 seconds");
Thread.sleep(5000);
... do a job here ...
} catch(InterruptedException e) {
System.out.println("Thread interrupted");
i_interrupted = 1;
}
} while (i_interrupted == 0);

/*never come here*/

}
 
J

Jason Teagle

For example :
java MyApp -START should launch the process (infinite loop)
java myApp -STOP should stop the process

The problem with this approach is that for the STOP, you're causing another
execution of the program - which isn't going to do what you want, it's just
going to create headaches {:v)

Why not give your program a UI? Create a frame for it, and have the start /
stop buttons on there. The buttons would set and clear a flag (boolean),
which the loop can monitor.
 
T

Tony Morris

Jason Teagle said:
The problem with this approach is that for the STOP, you're causing another
execution of the program - which isn't going to do what you want, it's just
going to create headaches {:v)

Why not give your program a UI? Create a frame for it, and have the start /
stop buttons on there. The buttons would set and clear a flag (boolean),
which the loop can monitor.

This is the typical approach to such a problem.
However, be aware of the issues created by the use of multiple threads
accessing shared data.

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
 
C

Chris

David said:
Hi everybody,

I have a small application running a perpetual loop to listen events
from a database. I'd like to add a START / STOP control...

For example :
java MyApp -START should launch the process (infinite loop)
java myApp -STOP should stop the process

The difficulty you have is that the second line runs a separate JVM, a
completely separate process, which means that there are only a limited
number of ways it can communicate with the first process. It can't, for
example, just get a reference to an object in the first JVM and execute a
method.

Inter-process communication can be done through 1) opening up a socket and
sending a command, 2) writing something to disk which the first process
could detect, or 3) doing something exotic like RMI (which also runs over a
socket).
 
J

Jason Teagle

Why not give your program a UI? Create a frame for it, and have the
start
/

This is the typical approach to such a problem.
However, be aware of the issues created by the use of multiple threads
accessing shared data.

Agreed. In this specific case the user showed code that appeared to execute
all in the same thread - the UI thread (for the part monitoring the
database, that is).
 
D

David

First of all, thanks everybody for your suggestions and remarks...

I don't develop a UI interface because the application should behave
like a daemon, I mean 24H/24, without any "terminal" or "window".
Somebody else suggests to use a "multicastsocket listener"
and give me an example I have to test :

private class MulticastListener extends Thread {
private MulticastSocket sock = new MulticastSocket("3000");
private boolean loop;

/** Listener for the message
*
*/
public MulticastListener() {
this.setPriority(Thread.MAX_PRIORITY);
InetAdress group = InetAddress.getByName("224.254.252.251");
sok.joinGroup(group);
}

/** run the listener
*/
public void run() {
DatagramPacket recv;
ByteArrayInputStream bin;
ObjectInputStream in;
loop = true;
while(loop) {
try {
byte[] buf = new byte[byte_size];
recv = new DatagramPacket(buf,buf.length);
sok.receive(recv);
bin= new ByteArrayInputStream(recv.getData());
in= new ObjectInputStream(bin);
Object obj = in.readObject();
//Ici recupère les informations
//et effecte tes actions

}catch(java.io.InterruptedIOException e){
loop = false;
}catch(java.net.SocketException e){
loop = false;
}catch(NullPointerException e){
loop = false;
}catch(Exception e) {
e.printStackTrace();
loop = false;
}
}
}

And the method to send message should be :

public void broadcastMessage(Object obj) {
try {

ByteArrayOutputStream bout= new ByteArrayOutputStream();
ObjectOutputStream out= new ObjectOutputStream(bout);
out.writeObject(obj);
out.close();
byte[] msg = bout.toByteArray();
DatagramPacket hi = new
DatagramPacket(msg,msg.length,InetAddress.getByName("224.254.252.251"),3000);
sok.send(hi);
}catch(Exception e) {
e.printStackTrace();
}
return;
}

I hope this will works... and I'm still open to any remarks or
examples to make a daemon from it...
 
J

Jon A. Cruz

David said:
First of all, thanks everybody for your suggestions and remarks...

I don't develop a UI interface because the application should behave
like a daemon, I mean 24H/24, without any "terminal" or "window".
Somebody else suggests to use a "multicastsocket listener"
and give me an example I have to test :

Hmmmm...

Sounds to me like a perfect place to use Rendezvous/Zeroconf

Here's a free implementation in Java.

http://jmdns.sourceforge.net/
 
N

Nick Knight

If you don't want to create a socket listener to listen for a "STOP"
command, you could have the original application occasionally poll for
the existance of a file on the file system and shutdown. Then, during
startup, delete this file....when stop is requested create a small
file to initiate shutdown.

Regards,
Nick
 
Joined
Dec 31, 2009
Messages
1
Reaction score
0
How to stop a java program

To stop a java program is difficult.

Find concrete examples at evolajva.com

Vol 4: Convictions, pag 406: how to stop a program
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top