下面是有问题的代码块。代码几乎总是有效的,但有时它永远挂起。应用程序是一个EJB计时器bean。
实际上,它只挂了一次,我不能复制它。它已经工作了近2年,在生产中没有任何问题。但是,在测试应用程序的更新版本时,计时器在运行了几天之后就冻结了,并且从未从上次运行中释放过数据库锁。日志清楚地表明它冻结在下面代码块的某个地方。它运行的命令是“chmod”。
public void shellExec(String cmd, File workDir) {
String s = null;
try {
Process p = Runtime.getRuntime().exec(cmd, null, workDir);
int i = p.waitFor();
if (i == 0){
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
// read the output from the command
while ((s = stdInput.readLine()) != null) {
logger.debug(s);
}
}
else {
BufferedReader stdErr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
// read the output from the command
while ((s = stdErr.readLine()) != null) {
logger.debug(s);
}
}
} catch (Exception e) {
logger.debug(e);
}
}
我不愿修改这段代码,因为它已经过测试,并且已经正常工作了近两年。我也不能重现这个问题,所以我不知道重写的版本是否更好。但很明显
能够
我不知道可能性有多大。
从谷歌搜索这个问题来看,代码块似乎是执行shell命令的标准。这个代码有什么已知的问题吗?有没有人知道一个好方法来确保它会抛出一个异常而不是挂起,因为我不能重现这个问题?
谢谢。