public class ExecuteWatchdog extends java.lang.Object implements TimeoutObserver
ExecuteWatchdog watchdog = new ExecuteWatchdog(30000); Executer exec = new Executer(myloghandler, watchdog); exec.setCommandLine(mycmdline); int exitvalue = exec.execute(); if (Execute.isFailure(exitvalue) && watchdog.killedProcess()) { // it was killed on purpose by the watchdog }When starting an asynchronous process than 'ExecuteWatchdog' is the keeper of the process handle. In some cases it is useful not to define a timeout (and pass 'INFINITE_TIMEOUT') and to kill the process explicitly using 'destroyProcess()'.
Please note that ExecuteWatchdog is processed asynchronously, e.g. it might be still attached to a process even after the DefaultExecutor.execute has returned.
Modifier and Type | Field and Description |
---|---|
private java.lang.Exception |
caught
Exception that might be thrown during the process execution.
|
private boolean |
hasWatchdog
Is a user-supplied timeout in use
|
static long |
INFINITE_TIMEOUT
The marker for an infinite timeout
|
private boolean |
killedProcess
Say whether or not the process was killed due to running overtime.
|
private java.lang.Process |
process
The process to execute and watch for duration.
|
private boolean |
processStarted
Indicates that the process is verified as started
|
private boolean |
watch
Say whether or not the watchdog is currently monitoring a process.
|
private Watchdog |
watchdog
Will tell us whether timeout has occurred.
|
Constructor and Description |
---|
ExecuteWatchdog(long timeout)
Creates a new watchdog with a given timeout.
|
Modifier and Type | Method and Description |
---|---|
void |
checkException()
This method will rethrow the exception that was possibly caught during
the run of the process.
|
protected void |
cleanUp()
reset the monitor flag and the process.
|
void |
destroyProcess()
Destroys the running process manually.
|
private void |
ensureStarted()
Ensures that the process is started, so we do not race with asynch execution.
|
boolean |
isWatching()
Indicates whether or not the watchdog is still monitoring the process.
|
boolean |
killedProcess()
Indicates whether the last process run was killed.
|
(package private) void |
setProcessNotStarted() |
void |
start(java.lang.Process processToMonitor)
Watches the given process and terminates it, if it runs for too long.
|
void |
stop()
Stops the watcher.
|
void |
timeoutOccured(Watchdog w)
Called after watchdog has finished.
|
public static final long INFINITE_TIMEOUT
private java.lang.Process process
private final boolean hasWatchdog
private boolean watch
private java.lang.Exception caught
private boolean killedProcess
private final Watchdog watchdog
private volatile boolean processStarted
public ExecuteWatchdog(long timeout)
timeout
- the timeout for the process in milliseconds. It must be
greater than 0 or 'INFINITE_TIMEOUT'public void start(java.lang.Process processToMonitor)
processToMonitor
- the process to monitor. It cannot be null
java.lang.IllegalStateException
- if a process is still being monitored.public void stop()
public void destroyProcess()
public void timeoutOccured(Watchdog w)
timeoutOccured
in interface TimeoutObserver
w
- the watchdog that timed out.public void checkException() throws java.lang.Exception
java.lang.Exception
- a wrapped exception over the one that was silently swallowed
and stored during the process run.public boolean isWatching()
true
if the process is still running, otherwise
false
.public boolean killedProcess()
true
if the process was killed
false
.protected void cleanUp()
void setProcessNotStarted()
private void ensureStarted()