Timeouts allow you to fail an execution with
TimeoutExceededException if it takes too long to complete:
Timeout<Object> timeout = Timeout.of(Duration.ofSeconds(10));
You can also interrupt an execution if it times out:
If a cancellation is triggered by a
Timeout, the execution is completed with
TimeoutExceededException. See the execution cancellation page for more on cancellation and interruption.
Timeout is composed outside a
RetryPolicy, a timeout occurrence will cancel any inner retries:
Timeout is composed inside a
RetryPolicy, a timeout occurrence will not automically cancel any outer retries:
When an async executions times out, Failsafe still waits until the execution completes, either through interruption or naturally, before recording a
TimeoutExceededException. This avoids the risk of retrying while the original execution is still running, which ultimately could lead to numerous abandoned executions running in numerous threads.
timeout.onFailure(e -> log.error("Connection attempt timed out", e.getFailure()));
Or when an execution completes and the timeout is not exceeded:
timeout.onSuccess(e -> log.info("Execution completed on time"));
Since the async integration methods involve external threads, which Failsafe has no knowledge of, these executions cannot be directly cancelled or interrupted by a Timeout. But, these executions can still cooperate with a Timeout cancellation.