我正试图在java (windows和linux)中开发一个监督狗,它将以两种方式运行:
1)被动监测。
在流程完成他的工作后,看门狗需要检查流程完成后的返回值。(出口(1),出口(0).)
2)主动监测。
该过程需要“触摸”属于他的每个间隔(X)的文件。看门狗将检查每一个间隔(y),如果进程“触摸”他的文件,检查文件印章。如果进程没有接触到该文件,监视狗将尝试向进程发送一个信号以用于触摸该文件。主动监视的目的是杀死具有死锁的进程。
看门狗将启动所有进程。
1)如何发送进程信号的jvm?一个信号是“提醒”进程触摸文件。另一个信号是杀死这个过程。
2)如何在线程上实现这一思想?
3) Java中有我可以使用的API吗?
谢谢
发布于 2013-12-17 14:07:46
也许这段片段能帮上忙
public class ProcessTest {
public static void main(String[] args) {
new ProcessTest().start();
}
private void start() {
startWatchDog();
startProcess();
}
private boolean abortCondition = false;
private int watchDogTSleepTime = 3000; //3 sek
private void startWatchDog() {
Runnable r = new Runnable() {
@Override
public void run() {
while(!abortCondition){
try {
Thread.sleep(watchDogTSleepTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
//check the file touch
boolean ok = checkFileTouch();
try {
//send signals to the process
outStream.write("signal".getBytes() );
} catch (IOException e) {
e.printStackTrace();
}
//if you want, you might try to kill the process
process.destroy();
}
}
};
Thread watchDog = new Thread(r);
watchDog.start();
//watchDog.setDaemon(true); //maybe
}
private boolean checkFileTouch(){
//...
return false;
}
private InputStream inStream;
private OutputStream outStream;
private Process process;
private void startProcess() {
String[] cmd = new String[]{"foo.exe", "para1", "param2"};
try {
//create and start the process
process = Runtime.getRuntime().exec(cmd);
inStream = process.getInputStream();
outStream = process.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
}
}https://stackoverflow.com/questions/20634144
复制相似问题