Each function call has a timeout and terminates the execution. After each function call (even after a timeout or an exception) a TaskResult is created with one of the following ResultCode:
-
ResultCode.OKThe execution is completed without exception and the result can be obtained withTaskResult#getResult -
ResultCode.TIMEOUTThe function call was NOT completed, the task was canceled -
ResultCode.ERRORAn unexpected exception has occurred. The exception can be determined withTaskResult#getErrorReason.
This library supports Runnable, Callable, Consumer and Function. When a timeout occurs, the worker thread is interrupted. Be careful to handle the interruption properly, otherwise a thread of an ExecutorService (⇒ your function) could end up in an infinite loop (⇒ examples). One way to handle an interruption is as follows:
if (Thread.interrupted()) {
throw new InterruptedException();
}-
Import the
Libraryin your project (see instructions below) -
Create & store a
WatchdogFactory. Each factory stores twoExecutorServices -
Use
Watchable.builder(…)to create a watchable andWatchableOptions.builder(…)to create the options
optional: add aResultProcessorto the watchable builder (this callback not monitored) -
Create an asynchronous function call with
WatchdogFactory#submitFunctionCall, a synchronized function call usingWatchdogFactory#waitForCompletionor create aRepeatableTaskwithWatchdogFactory#createRepeated -
In case of a RepeatableTask call the task with
RepeatableTask#submitFunctionCallandRepeatableTask#waitForCompletion
repositories {
maven {
url = uri("https://maven.pkg.github.com/JanPollmann/Watchdog")
}
}
dependencies {
implementation 'de.pollmann.watchdog:watchdog:<version>'
}Follow the GitHub documentation of the desired package: https://github.com/JanPollmann/Watchdog/packages
|
Caution
|
|
Just implement loop. Remarks:
-
A timeout of 0 ms will be handled as
no timeout -
both tasks have a
ResultConsumerregistered, but that’s an optional feature
terminate endless loop https://github.com/JanPollmann/Watchdog/blob/HEAD/Library/src/test/java/de/pollmann/watchdog/SabotageTest.java
You can find more examples in the UnitTests:
https://github.com/JanPollmann/Watchdog/blob/HEAD/Library/src/test/java/de/pollmann/watchdog/