Package org.apache.commons.exec
Class ExecuteWatchdog
java.lang.Object
org.apache.commons.exec.ExecuteWatchdog
- All Implemented Interfaces:
TimeoutObserver
Destroys a process running for too long. For example:
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.
- Version:
- $Id: ExecuteWatchdog.java 1612032 2014-07-20 06:30:44Z ggregory $
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate ExceptionException that might be thrown during the process execution.private final booleanIs a user-supplied timeout in usestatic final longThe marker for an infinite timeoutprivate booleanSay whether or not the process was killed due to running overtime.private ProcessThe process to execute and watch for duration.private booleanIndicates that the process is verified as startedprivate booleanSay whether or not the watchdog is currently monitoring a process.private final WatchdogWill tell us whether timeout has occurred. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidThis method will rethrow the exception that was possibly caught during the run of the process.protected voidcleanUp()reset the monitor flag and the process.voidDestroys the running process manually.private voidEnsures that the process is started, so we do not race with asynch execution.booleanIndicates whether or not the watchdog is still monitoring the process.booleanIndicates whether the last process run was killed.(package private) voidvoidWatches the given process and terminates it, if it runs for too long.voidstop()Stops the watcher.voidCalled after watchdog has finished.
-
Field Details
-
INFINITE_TIMEOUT
public static final long INFINITE_TIMEOUTThe marker for an infinite timeout- See Also:
-
process
The process to execute and watch for duration. -
hasWatchdog
private final boolean hasWatchdogIs a user-supplied timeout in use -
watch
private boolean watchSay whether or not the watchdog is currently monitoring a process. -
caught
Exception that might be thrown during the process execution. -
killedProcess
private boolean killedProcessSay whether or not the process was killed due to running overtime. -
watchdog
Will tell us whether timeout has occurred. -
processStarted
private volatile boolean processStartedIndicates that the process is verified as started
-
-
Constructor Details
-
ExecuteWatchdog
public ExecuteWatchdog(long timeout) Creates a new watchdog with a given timeout.- Parameters:
timeout- the timeout for the process in milliseconds. It must be greater than 0 or 'INFINITE_TIMEOUT'
-
-
Method Details
-
start
Watches the given process and terminates it, if it runs for too long. All information from the previous run are reset.- Parameters:
processToMonitor- the process to monitor. It cannot benull- Throws:
IllegalStateException- if a process is still being monitored.
-
stop
public void stop()Stops the watcher. It will notify all threads possibly waiting on this object. -
destroyProcess
public void destroyProcess()Destroys the running process manually. -
timeoutOccured
Called after watchdog has finished.- Specified by:
timeoutOccuredin interfaceTimeoutObserver- Parameters:
w- the watchdog that timed out.
-
checkException
This method will rethrow the exception that was possibly caught during the run of the process. It will only remains valid once the process has been terminated either by 'error', timeout or manual intervention. Information will be discarded once a new process is ran.- Throws:
Exception- a wrapped exception over the one that was silently swallowed and stored during the process run.
-
isWatching
public boolean isWatching()Indicates whether or not the watchdog is still monitoring the process.- Returns:
trueif the process is still running, otherwisefalse.
-
killedProcess
public boolean killedProcess()Indicates whether the last process run was killed.- Returns:
trueif the process was killedfalse.
-
cleanUp
protected void cleanUp()reset the monitor flag and the process. -
setProcessNotStarted
void setProcessNotStarted() -
ensureStarted
private void ensureStarted()Ensures that the process is started, so we do not race with asynch execution. The caller of this method must be holding the lock on this
-