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
class ExecutionImpl<R> extends java.lang.Object implements ExecutionInternal<R>
Execution and ExecutionInternal implementation.
-
-
Field Summary
Fields Modifier and Type Field Description (package private) boolean
attemptRecorded
private java.util.concurrent.atomic.AtomicInteger
attempts
private java.time.Instant
attemptStartTime
(package private) CheckedRunnable
cancelCallback
(package private) int
cancelledIndex
(package private) boolean
completed
private java.util.concurrent.atomic.AtomicInteger
executions
private java.util.concurrent.atomic.AtomicReference<ExecutionInternal<R>>
latest
(package private) java.util.List<PolicyExecutor<R>>
policyExecutors
private boolean
preExecuted
private ExecutionResult<R>
previousResult
(package private) ExecutionResult<R>
result
private java.time.Instant
startTime
-
Constructor Summary
Constructors Constructor Description ExecutionImpl(ExecutionImpl<R> execution)
Create an execution for a new attempt.ExecutionImpl(ExecutionResult<R> previousResult)
Used for testing purposes onlyExecutionImpl(java.util.List<? extends Policy<R>> policies)
Creates a new execution for thepolicies
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
cancel()
Called indirectly by users.void
cancel(PolicyExecutor<R> policyExecutor)
Called by policies.int
getAttemptCount()
Gets the number of execution attempts so far, including attempts that are blocked before being executed, such as when aCircuitBreaker
is open.java.time.Duration
getElapsedAttemptTime()
Returns the elapsed time since the last execution attempt began.java.time.Duration
getElapsedTime()
Returns the elapsed time since initial execution began.int
getExecutionCount()
Gets the number of completed executions so far.<T extends java.lang.Throwable>
TgetLastException()
Returns the last exception that was recorded elsenull
.R
getLastResult()
Returns the last result that was recorded elsenull
.R
getLastResult(R defaultValue)
Returns the last result that was recorded else thedefaultValue
.ExecutionInternal<R>
getLatest()
Returns the most recent execution to be attempted.java.lang.Object
getLock()
Returns a lock object that is common across all execution attempts.ExecutionResult<R>
getResult()
Returns the recorded result for an execution attempt.java.time.Instant
getStartTime()
Returns the time that the initial execution started.boolean
isCancelled()
Returns whether the execution has been cancelled.boolean
isCancelled(PolicyExecutor<R> policyExecutor)
Returns whether the execution is considered cancelled for thepolicyExecutor
.boolean
isFirstAttempt()
Returnstrue
when an execution result has not yet been recorded, meaning this is the first execution attempt.boolean
isPreExecuted()
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)
(package private) ExecutionResult<R>
postExecute(ExecutionResult<R> result)
Externally called.void
preExecute()
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
recordAttempt()
Records an execution attempt which may correspond with an execution result.java.lang.String
toString()
-
-
-
Field Detail
-
policyExecutors
final java.util.List<PolicyExecutor<R>> policyExecutors
-
startTime
private volatile java.time.Instant startTime
-
attempts
private final java.util.concurrent.atomic.AtomicInteger attempts
-
executions
private final java.util.concurrent.atomic.AtomicInteger executions
-
latest
private final java.util.concurrent.atomic.AtomicReference<ExecutionInternal<R>> latest
-
previousResult
private final ExecutionResult<R> previousResult
-
result
volatile ExecutionResult<R> result
-
attemptStartTime
private volatile java.time.Instant attemptStartTime
-
cancelledIndex
volatile int cancelledIndex
-
cancelCallback
volatile CheckedRunnable cancelCallback
-
preExecuted
private volatile boolean preExecuted
-
attemptRecorded
volatile boolean attemptRecorded
-
completed
volatile boolean completed
-
-
Constructor Detail
-
ExecutionImpl
ExecutionImpl(java.util.List<? extends Policy<R>> policies)
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 Detail
-
getResult
public ExecutionResult<R> getResult()
Description copied from interface:ExecutionInternal
Returns the recorded result for an execution attempt.- Specified by:
getResult
in interfaceExecutionInternal<R>
-
onCancel
public void onCancel(CheckedRunnable cancelCallback)
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
public void record(ExecutionResult<R> result)
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
ExecutionResult<R> postExecute(ExecutionResult<R> result)
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:
java.lang.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
public void cancel(PolicyExecutor<R> policyExecutor)
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
public boolean isCancelled(PolicyExecutor<R> policyExecutor)
Description copied from interface:ExecutionInternal
Returns whether the execution is considered cancelled for thepolicyExecutor
.- Specified by:
isCancelled
in interfaceExecutionInternal<R>
-
getLock
public java.lang.Object 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
public ExecutionInternal<R> getLatest()
Description copied from interface:ExecutionInternal
Returns the most recent execution to be attempted.- Specified by:
getLatest
in interfaceExecutionInternal<R>
-
getElapsedTime
public java.time.Duration getElapsedTime()
Description copied from interface:ExecutionContext
Returns the elapsed time since initial execution began.- Specified by:
getElapsedTime
in interfaceExecutionContext<R>
-
getElapsedAttemptTime
public java.time.Duration 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
public <T extends java.lang.Throwable> T getLastException()
Description copied from interface:ExecutionContext
Returns the last exception that was recorded elsenull
.- Specified by:
getLastException
in interfaceExecutionContext<R>
-
getLastResult
public R getLastResult()
Description copied from interface:ExecutionContext
Returns the last result that was recorded elsenull
.- Specified by:
getLastResult
in interfaceExecutionContext<R>
-
getLastResult
public R getLastResult(R defaultValue)
Description copied from interface:ExecutionContext
Returns the last result that was recorded else thedefaultValue
.- Specified by:
getLastResult
in interfaceExecutionContext<R>
-
getStartTime
public java.time.Instant 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
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-