R
- result typepublic class Timeout<R> extends PolicyListeners<Timeout<R>,R> implements Policy<R>
TimeoutExceededException
if it exceeds a timeout. 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:
PolicyListeners.onFailure(CheckedConsumer)
and PolicyListeners.onSuccess(CheckedConsumer)
event handlers
can be used to handle a timeout being exceeded or not.Cancellation and interruption
are not supported when performing an async execution
and will have no effect since the async thread is
unkown to Failsafe.This class is threadsafe.
TimeoutExceededException
Modifier and Type | Method and Description |
---|---|
boolean |
canCancel()
Returns whether the policy can cancel an execution if the timeout is exceeded.
|
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)
Configures the policy to cancel an execution if it times out.
|
onFailure, onSuccess
public Duration getTimeout()
public boolean canCancel()
withCancel(boolean)
public boolean canInterrupt()
withCancel(boolean)
public Timeout<R> withCancel(boolean mayInterruptIfRunning)
ExecutionContext.isCancelled()
, allowing execution to be gracefully stopped.
Asynchronous executions are cancelled by calling cancel
on
their underlying future.
Notes:
TimeoutExceededException
.
async execution
and will have no effect since the async thread is unkown to Failsafe.
mayInterruptIfRunning
- Whether the policy should interrupt an execution in addition to cancelling it if the
timeout is exceeded. When set to true
the execution will be interrupted. 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)
. Only set mayInterruptIfRunning
to true
if the code being executed is designed to be interrupted.public 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)
Policy
PolicyExecutor
capable of performing an execution in the context of a Policy and handling
results according to the Policy.toExecutor
in interface Policy<R>
Copyright © 2020. All rights reserved.