Hi,
I have a problem concerning commons-exec when using the timeouts with a
watchdog. The test-cases are running fine (Windows Vista, JDK 1.5.x)
I tried to simplify the usage of commons-exec because in our use-case we
often need a very simple case.
So I have a class ExecUtils which tries to hide the complexity of many
(necessary) details. It mainly has a static method ExecUtils.exec(String
cmd, int timeout) which should run an command in a shell and terminate
the command if necessary.
But - in my simple test-case the timout does not occur. What am I missing?
I am glad for your help.
Cheers
Tino
----------------------
--- TEST
----------------------
public void testTimeoutCommand() {
System.out.println("*** testTimeoutCommand.begin");
// 100 Pings senden, aber nach 5 Sekunden abbrechen.
String cmd = "ping localhost -n 10000 -w 1000";
ExecUtils.Result r = ExecUtils.exec(cmd, 5000);
assertTrue("Es muss ein Timeout auftreten!", r.hasTimeout());
System.err.println("result: " + r);
System.out.println("*** testTimeoutCommand.end");
}
----------------------
--- Code of ExecUtils
----------------------
public class ExecUtils {
public static class Result {
private String out;
private String err;
private int exitValue;
private boolean timeout;
private Exception exception;
public String getStdOut() {
return out;
}
public String getStdErr() {
return err;
}
public int getExitValue() {
return exitValue;
}
public boolean hasTimeout() {
return timeout;
}
public Exception getException() {
return exception;
}
}
public static Result exec(String cmdLine) {
return exec(cmdLine, Integer.MAX_VALUE);
}
public static Result exec(String cmdLine, int timeout) {
DefaultExecutor executor = new DefaultExecutor();
ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
executor.setWatchdog(watchdog);
ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
PumpStreamHandler streamHandler = new
PumpStreamHandler(baosOut, baosErr);
//PumpStreamHandler streamHandler = new PumpStreamHandler();
executor.setStreamHandler(streamHandler);
// this is simplified
CommandLine commandLine = "cmd.exe /c " + cmdLine;
Result res = new Result();
try {
res.exitValue = executor.execute(commandLine);
} catch (Exception x) {
res.exception = x;
} finally {
res.out = new String(baosOut.toByteArray());
res.err = new String(baosErr.toByteArray());
res.timeout = watchdog.killedProcess();
}
return res;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]