commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Mishkin, Ernest" <ernest_mish...@mcgraw-hill.com>
Subject RE: ExecuteWatchdog does not really terminate the "hung" process?
Date Mon, 22 Jun 2009 14:11:16 GMT
Looks like my attachements got chopped somewhere along the way, so I'll
just paste the two files here since they're really short:


ExecTest.java
//////////////////////////////////
import java.io.File;

import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
import org.apache.commons.exec.ExecuteWatchdog;
import org.apache.commons.exec.PumpStreamHandler;
import org.apache.commons.io.output.ByteArrayOutputStream;

public class ExecTest {

	public static void main(String[] args) throws Exception {
		File scriptFile = new File("sleep.bat");
		CommandLine cmdLine = new CommandLine(scriptFile);
		cmdLine.addArgument("10"); // sleep 10 secs
		System.out.println("Cmd line: " + cmdLine);
		DefaultExecutor executor = new DefaultExecutor();
		ExecuteWatchdog watchdog = new ExecuteWatchdog(2 *
1000); // allow process no more than 2 secs
		executor.setWatchdog(watchdog);
		ByteArrayOutputStream baos = new
ByteArrayOutputStream();
		executor.setStreamHandler(new PumpStreamHandler(baos));

		long startTime = System.currentTimeMillis();
		try {
			executor.execute(cmdLine);
		} catch (ExecuteException e) {
			System.out.println(e);
		}
		System.out.println("Process completed in " +
(System.currentTimeMillis() - startTime) +" millis; below is its
output");
		System.out.println(baos);
		if (watchdog.killedProcess()) {
			System.out.println("Process timed out and was
killed.");
		}
	}

}
//////////////////////////////////


sleep.bat

ping -n %1 -w 1000 127.0.0.1
 
--------------------------------------------------------

The information contained in this message is intended only for the recipient, and may be a
confidential attorney-client communication or may otherwise be privileged and confidential
and protected from disclosure. If the reader of this message is not the intended recipient,
or an employee or agent responsible for delivering this message to the intended recipient,
please be aware that any dissemination or copying of this communication is strictly prohibited.
If you have received this communication in error, please immediately notify us by replying
to the message and deleting it from your computer. The McGraw-Hill Companies, Inc. reserves
the right, subject to applicable local law, to monitor and review the content of any electronic
message or information sent to or from McGraw-Hill employee e-mail addresses without informing
the sender or recipient of the message.
--------------------------------------------------------

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message