R - result typepublic class Timeout<R> extends PolicyListeners<Timeout<R>,R> implements Policy<R>
TimeoutExceededException if a timeout is exceeded. Execution interruption is
optionally supported. Asynchronous executions are cancelled by calling cancel on their underlying future. Executions can internally cooperate with cancellation by checking ExecutionContext.isCancelled().
This policy uses a separate thread on the configured scheduler or the common pool to perform timeouts checks.
The PolicyListeners.onFailure(CheckedConsumer) and PolicyListeners.onSuccess(CheckedConsumer) event handlers can be
used to handle a timeout being exceeded or not.
Note: interruption will have no effect when performing an async execution since the async thread is unkown to Failsafe.
This class is threadsafe.
TimeoutExceededException| Modifier and Type | Method and Description |
|---|---|
boolean |
canCancel()
Deprecated.
Always returns
true |
boolean |
canInterrupt()
Returns whether the policy can interrupt an execution if the timeout is exceeded.
|
Duration |
getTimeout()
Returns the timeout duration.
|
static <R> Timeout<R> |
of(Duration timeout)
|
PolicyExecutor |
toExecutor(AbstractExecution execution)
Returns an
PolicyExecutor capable of performing an execution in the context of a Policy and handling
results according to the Policy. |
Timeout<R> |
withCancel(boolean mayInterruptIfRunning)
Deprecated.
Tasks are cancelled by default when a Timeout is exceeded. Use
withInterrupt(boolean)
interrupt cancelled tasks when a Timeout is exceeded. |
Timeout<R> |
withInterrupt(boolean mayInterruptIfRunning)
When
mayInterruptIfRunning is true, configures the policy to interrupt an execution in addition to
cancelling it when the timeout is exceeded. |
onFailure, onSuccesspublic Duration getTimeout()
public boolean canCancel()
truewithCancel(boolean)public boolean canInterrupt()
withInterrupt(boolean)public Timeout<R> withCancel(boolean mayInterruptIfRunning)
withInterrupt(boolean)
interrupt cancelled tasks when a Timeout is exceeded.withInterrupt(boolean)public Timeout<R> withInterrupt(boolean mayInterruptIfRunning)
mayInterruptIfRunning is true, configures the policy to interrupt an execution in addition to
cancelling it when the timeout is exceeded. For synchronous executions this is done by calling Thread.interrupt() on the execution's thread. For asynchronous executions this is done by calling Future.cancel(true). Executions can internally cooperate with
interruption by checking Thread.isInterrupted() or by handling InterruptedException where
available.
Note: Only configure interrupts if the code being executed is designed to be interrupted.
Note: interruption will have no effect when performing an async execution since the async thread is unkown to
Failsafe.
mayInterruptIfRunning - whether to enable interruption or notpublic static <R> Timeout<R> of(Duration timeout)
timeout - the duration after which an execution is failed with TimeoutExceededException.NullPointerException - If timeout is nullIllegalArgumentException - If timeout is <= 0public PolicyExecutor toExecutor(AbstractExecution execution)
PolicyPolicyExecutor capable of performing an execution in the context of a Policy and handling
results according to the Policy.toExecutor in interface Policy<R>Copyright © 2021. All rights reserved.