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 = ExecuteWatchdog.builder().setTimeout(Duration.ofSeconds(30)).get();
 Executor executor = DefaultExecutor.builder().setExecuteStreamHandler(new PumpStreamHandler()).get();
 executor.setWatchdog(watchdog);
 int exitValue = executor.execute(myCommandLine);
 if (executor.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_DURATION) 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(CommandLine) or a variation has returned.
 
- See Also:
- 
Nested Class SummaryNested ClassesModifier and TypeClassDescriptionstatic final classBuilds ExecuteWatchdog instances.
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final longThe marker for an infinite timeout.static final DurationThe marker for an infinite timeout.
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionstatic ExecuteWatchdog.Builderbuilder()Creates a new builder.voidThis 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.voidNotification that starting the process failed.booleanIndicates whether or not the watchdog is still monitoring the process.booleanIndicates whether the last process run was killed.voidWatches the given process and terminates it, if it runs for too long.voidstop()Stops the watcher.voidCalled after watchdog has finished.
- 
Field Details- 
INFINITE_TIMEOUTThe marker for an infinite timeout.- See Also:
 
- 
INFINITE_TIMEOUT_DURATIONThe marker for an infinite timeout.
 
- 
- 
Constructor Details- 
ExecuteWatchdogDeprecated.Creates a new watchdog with a given timeout.- Parameters:
- timeoutMillis- the timeout for the process in milliseconds. It must be greater than 0 or- INFINITE_TIMEOUT.
 
 
- 
- 
Method Details- 
builderCreates a new builder.- Returns:
- a new builder.
- Since:
- 1.4.0
 
- 
checkExceptionThis 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.
 
- 
cleanUpreset the monitor flag and the process.
- 
destroyProcessDestroys the running process manually.
- 
failedToStartNotification that starting the process failed.- Parameters:
- e- the offending exception.
 
- 
isWatchingIndicates whether or not the watchdog is still monitoring the process.- Returns:
- trueif the process is still running, otherwise- false.
 
- 
killedProcessIndicates whether the last process run was killed.- Returns:
- trueif the process was killed- false.
 
- 
startWatches 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 be- null.
- Throws:
- IllegalStateException- if a process is still being monitored.
 
- 
stopStops the watcher. It will notify all threads possibly waiting on this object.
- 
timeoutOccuredCalled after watchdog has finished.- Specified by:
- timeoutOccuredin interface- TimeoutObserver
- Parameters:
- w- the watchdog that timed out.
 
 
- 
ExecuteWatchdog.Builder.get().