Package dev.failsafe
Class ExecutionImpl<R>
java.lang.Object
dev.failsafe.ExecutionImpl<R>
- Type Parameters:
R
- result type
- All Implemented Interfaces:
ExecutionContext<R>
,ExecutionInternal<R>
- Direct Known Subclasses:
AsyncExecutionImpl
,SyncExecutionImpl
Execution and ExecutionInternal implementation.
-
Field Summary
FieldsModifier and TypeFieldDescription(package private) boolean
private final AtomicInteger
private Instant
(package private) CheckedRunnable
(package private) int
(package private) boolean
private final AtomicInteger
private final AtomicReference
<ExecutionInternal<R>> (package private) final List
<PolicyExecutor<R>> private boolean
private final ExecutionResult
<R> (package private) ExecutionResult
<R> private Instant
-
Constructor Summary
ConstructorsConstructorDescriptionExecutionImpl
(ExecutionImpl<R> execution) Create an execution for a new attempt.ExecutionImpl
(ExecutionResult<R> previousResult) Used for testing purposes onlyExecutionImpl
(List<? extends Policy<R>> policies) Creates a new execution for thepolicies
. -
Method Summary
Modifier and TypeMethodDescriptionboolean
cancel()
Called indirectly by users.void
cancel
(PolicyExecutor<R> policyExecutor) Called by policies.int
Gets the number of execution attempts so far, including attempts that are blocked before being executed, such as when aCircuitBreaker
is open.Returns the elapsed time since the last execution attempt began.Returns the elapsed time since initial execution began.int
Gets the number of completed executions so far.<T extends Throwable>
TReturns the last exception that was recorded elsenull
.Returns the last result that was recorded elsenull
.getLastResult
(R defaultValue) Returns the last result that was recorded else thedefaultValue
.Returns the most recent execution to be attempted.getLock()
Returns a lock object that is common across all execution attempts.Returns the recorded result for an execution attempt.Returns the time that the initial execution started.boolean
Returns whether the execution has been cancelled.boolean
isCancelled
(PolicyExecutor<R> policyExecutor) Returns whether the execution is considered cancelled for thepolicyExecutor
.boolean
Returnstrue
when an execution result has not yet been recorded, meaning this is the first execution attempt.boolean
Returns whether the execution has been pre-executed, indicating the attempt has started.boolean
isRetry()
Returnstrue
when an execution result has already been recorded, meaning the execution is being retried.void
onCancel
(CheckedRunnable cancelCallback) Sets thecancelCallback
to be called if the execution is cancelled, such as by the resultingCall
orCompletableFuture
, or aTimeout
.(package private) ExecutionResult
<R> postExecute
(ExecutionResult<R> result) Externally called.void
Called when execution of the user's supplier is about to begin.void
record
(ExecutionResult<R> result) Records theresult
if the execution has beenpre-executed
and a result has not already been recorded.void
Records an execution attempt which may correspond with an execution result.toString()
-
Field Details
-
policyExecutors
-
startTime
-
attempts
-
executions
-
latest
-
previousResult
-
result
-
attemptStartTime
-
cancelledIndex
volatile int cancelledIndex -
cancelCallback
-
preExecuted
private volatile boolean preExecuted -
attemptRecorded
volatile boolean attemptRecorded -
completed
volatile boolean completed
-
-
Constructor Details
-
ExecutionImpl
Creates a new execution for thepolicies
. -
ExecutionImpl
ExecutionImpl(ExecutionImpl<R> execution) Create an execution for a new attempt. -
ExecutionImpl
ExecutionImpl(ExecutionResult<R> previousResult) Used for testing purposes only
-
-
Method Details
-
getResult
Description copied from interface:ExecutionInternal
Returns the recorded result for an execution attempt.- Specified by:
getResult
in interfaceExecutionInternal<R>
-
onCancel
Description copied from interface:ExecutionContext
Sets thecancelCallback
to be called if the execution is cancelled, such as by the resultingCall
orCompletableFuture
, or aTimeout
. Any exception thrown by thecancelCallback
is ignored.- Specified by:
onCancel
in interfaceExecutionContext<R>
-
preExecute
public void preExecute()Description copied from interface:ExecutionInternal
Called when execution of the user's supplier is about to begin.- Specified by:
preExecute
in interfaceExecutionInternal<R>
-
isPreExecuted
public boolean isPreExecuted()Description copied from interface:ExecutionInternal
Returns whether the execution has been pre-executed, indicating the attempt has started.- Specified by:
isPreExecuted
in interfaceExecutionInternal<R>
-
recordAttempt
public void recordAttempt()Description copied from interface:ExecutionInternal
Records an execution attempt which may correspond with an execution result. Async executions will have results recorded separately.- Specified by:
recordAttempt
in interfaceExecutionInternal<R>
-
record
Description copied from interface:ExecutionInternal
Records theresult
if the execution has beenpre-executed
and a result has not already been recorded.- Specified by:
record
in interfaceExecutionInternal<R>
-
postExecute
Externally called. Records an execution and performs post-execution handling for theresult
against all configured policy executors. Returns whether the result is complete for all policies.- Throws:
IllegalStateException
- if the execution is already complete
-
cancel
public boolean cancel()Called indirectly by users.- Specified by:
cancel
in interfaceExecutionInternal<R>
- Returns:
- whether cancellation was successful or not. Returns
false
if the execution was already cancelled or completed.
-
cancel
Called by policies.- Specified by:
cancel
in interfaceExecutionInternal<R>
-
isCancelled
public boolean isCancelled()Description copied from interface:ExecutionContext
Returns whether the execution has been cancelled. In this case the implementor should attempt to stop execution.- Specified by:
isCancelled
in interfaceExecutionContext<R>
-
isCancelled
Description copied from interface:ExecutionInternal
Returns whether the execution is considered cancelled for thepolicyExecutor
.- Specified by:
isCancelled
in interfaceExecutionInternal<R>
-
getLock
Description copied from interface:ExecutionInternal
Returns a lock object that is common across all execution attempts. Useful for guarding against races when mutating an execution.- Specified by:
getLock
in interfaceExecutionInternal<R>
-
getLatest
Description copied from interface:ExecutionInternal
Returns the most recent execution to be attempted.- Specified by:
getLatest
in interfaceExecutionInternal<R>
-
getElapsedTime
Description copied from interface:ExecutionContext
Returns the elapsed time since initial execution began.- Specified by:
getElapsedTime
in interfaceExecutionContext<R>
-
getElapsedAttemptTime
Description copied from interface:ExecutionContext
Returns the elapsed time since the last execution attempt began.- Specified by:
getElapsedAttemptTime
in interfaceExecutionContext<R>
-
getAttemptCount
public int getAttemptCount()Description copied from interface:ExecutionContext
Gets the number of execution attempts so far, including attempts that are blocked before being executed, such as when aCircuitBreaker
is open. Will return0
when the first attempt is in progress or has yet to begin.- Specified by:
getAttemptCount
in interfaceExecutionContext<R>
-
getExecutionCount
public int getExecutionCount()Description copied from interface:ExecutionContext
Gets the number of completed executions so far. Executions that are blocked, such as when aCircuitBreaker
is open, are not counted. Will return0
when the first attempt is in progress or has yet to begin.- Specified by:
getExecutionCount
in interfaceExecutionContext<R>
-
getLastException
Description copied from interface:ExecutionContext
Returns the last exception that was recorded elsenull
.- Specified by:
getLastException
in interfaceExecutionContext<R>
-
getLastResult
Description copied from interface:ExecutionContext
Returns the last result that was recorded elsenull
.- Specified by:
getLastResult
in interfaceExecutionContext<R>
-
getLastResult
Description copied from interface:ExecutionContext
Returns the last result that was recorded else thedefaultValue
.- Specified by:
getLastResult
in interfaceExecutionContext<R>
-
getStartTime
Description copied from interface:ExecutionContext
Returns the time that the initial execution started.- Specified by:
getStartTime
in interfaceExecutionContext<R>
-
isFirstAttempt
public boolean isFirstAttempt()Description copied from interface:ExecutionContext
Returnstrue
when an execution result has not yet been recorded, meaning this is the first execution attempt.- Specified by:
isFirstAttempt
in interfaceExecutionContext<R>
-
isRetry
public boolean isRetry()Description copied from interface:ExecutionContext
Returnstrue
when an execution result has already been recorded, meaning the execution is being retried.- Specified by:
isRetry
in interfaceExecutionContext<R>
-
toString
-