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) booleanattemptRecordedprivate java.util.concurrent.atomic.AtomicIntegerattemptsprivate java.time.InstantattemptStartTime(package private) CheckedRunnablecancelCallback(package private) intcancelledIndex(package private) booleancompletedprivate java.util.concurrent.atomic.AtomicIntegerexecutionsprivate java.util.concurrent.atomic.AtomicReference<ExecutionInternal<R>>latest(package private) java.util.List<PolicyExecutor<R>>policyExecutorsprivate booleanpreExecutedprivate ExecutionResult<R>previousResult(package private) ExecutionResult<R>resultprivate java.time.InstantstartTime
-
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 booleancancel()Called indirectly by users.voidcancel(PolicyExecutor<R> policyExecutor)Called by policies.intgetAttemptCount()Gets the number of execution attempts so far, including attempts that are blocked before being executed, such as when aCircuitBreakeris open.java.time.DurationgetElapsedAttemptTime()Returns the elapsed time since the last execution attempt began.java.time.DurationgetElapsedTime()Returns the elapsed time since initial execution began.intgetExecutionCount()Gets the number of completed executions so far.<T extends java.lang.Throwable>
TgetLastException()Returns the last exception that was recorded elsenull.RgetLastResult()Returns the last result that was recorded elsenull.RgetLastResult(R defaultValue)Returns the last result that was recorded else thedefaultValue.ExecutionInternal<R>getLatest()Returns the most recent execution to be attempted.java.lang.ObjectgetLock()Returns a lock object that is common across all execution attempts.ExecutionResult<R>getResult()Returns the recorded result for an execution attempt.java.time.InstantgetStartTime()Returns the time that the initial execution started.booleanisCancelled()Returns whether the execution has been cancelled.booleanisCancelled(PolicyExecutor<R> policyExecutor)Returns whether the execution is considered cancelled for thepolicyExecutor.booleanisFirstAttempt()Returnstruewhen an execution result has not yet been recorded, meaning this is the first execution attempt.booleanisPreExecuted()Returns whether the execution has been pre-executed, indicating the attempt has started.booleanisRetry()Returnstruewhen an execution result has already been recorded, meaning the execution is being retried.voidonCancel(CheckedRunnable cancelCallback)(package private) ExecutionResult<R>postExecute(ExecutionResult<R> result)Externally called.voidpreExecute()Called when execution of the user's supplier is about to begin.voidrecord(ExecutionResult<R> result)Records theresultif the execution has beenpre-executedand a result has not already been recorded.voidrecordAttempt()Records an execution attempt which may correspond with an execution result.java.lang.StringtoString()
-
-
-
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:ExecutionInternalReturns the recorded result for an execution attempt.- Specified by:
getResultin interfaceExecutionInternal<R>
-
onCancel
public void onCancel(CheckedRunnable cancelCallback)
Description copied from interface:ExecutionContextSets thecancelCallbackto be called if the execution is cancelled, such as by the resultingCallorCompletableFuture, or aTimeout. Any exception thrown by thecancelCallbackis ignored.- Specified by:
onCancelin interfaceExecutionContext<R>
-
preExecute
public void preExecute()
Description copied from interface:ExecutionInternalCalled when execution of the user's supplier is about to begin.- Specified by:
preExecutein interfaceExecutionInternal<R>
-
isPreExecuted
public boolean isPreExecuted()
Description copied from interface:ExecutionInternalReturns whether the execution has been pre-executed, indicating the attempt has started.- Specified by:
isPreExecutedin interfaceExecutionInternal<R>
-
recordAttempt
public void recordAttempt()
Description copied from interface:ExecutionInternalRecords an execution attempt which may correspond with an execution result. Async executions will have results recorded separately.- Specified by:
recordAttemptin interfaceExecutionInternal<R>
-
record
public void record(ExecutionResult<R> result)
Description copied from interface:ExecutionInternalRecords theresultif the execution has beenpre-executedand a result has not already been recorded.- Specified by:
recordin interfaceExecutionInternal<R>
-
postExecute
ExecutionResult<R> postExecute(ExecutionResult<R> result)
Externally called. Records an execution and performs post-execution handling for theresultagainst 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:
cancelin interfaceExecutionInternal<R>- Returns:
- whether cancellation was successful or not. Returns
falseif the execution was already cancelled or completed.
-
cancel
public void cancel(PolicyExecutor<R> policyExecutor)
Called by policies.- Specified by:
cancelin interfaceExecutionInternal<R>
-
isCancelled
public boolean isCancelled()
Description copied from interface:ExecutionContextReturns whether the execution has been cancelled. In this case the implementor should attempt to stop execution.- Specified by:
isCancelledin interfaceExecutionContext<R>
-
isCancelled
public boolean isCancelled(PolicyExecutor<R> policyExecutor)
Description copied from interface:ExecutionInternalReturns whether the execution is considered cancelled for thepolicyExecutor.- Specified by:
isCancelledin interfaceExecutionInternal<R>
-
getLock
public java.lang.Object getLock()
Description copied from interface:ExecutionInternalReturns a lock object that is common across all execution attempts. Useful for guarding against races when mutating an execution.- Specified by:
getLockin interfaceExecutionInternal<R>
-
getLatest
public ExecutionInternal<R> getLatest()
Description copied from interface:ExecutionInternalReturns the most recent execution to be attempted.- Specified by:
getLatestin interfaceExecutionInternal<R>
-
getElapsedTime
public java.time.Duration getElapsedTime()
Description copied from interface:ExecutionContextReturns the elapsed time since initial execution began.- Specified by:
getElapsedTimein interfaceExecutionContext<R>
-
getElapsedAttemptTime
public java.time.Duration getElapsedAttemptTime()
Description copied from interface:ExecutionContextReturns the elapsed time since the last execution attempt began.- Specified by:
getElapsedAttemptTimein interfaceExecutionContext<R>
-
getAttemptCount
public int getAttemptCount()
Description copied from interface:ExecutionContextGets the number of execution attempts so far, including attempts that are blocked before being executed, such as when aCircuitBreakeris open. Will return0when the first attempt is in progress or has yet to begin.- Specified by:
getAttemptCountin interfaceExecutionContext<R>
-
getExecutionCount
public int getExecutionCount()
Description copied from interface:ExecutionContextGets the number of completed executions so far. Executions that are blocked, such as when aCircuitBreakeris open, are not counted. Will return0when the first attempt is in progress or has yet to begin.- Specified by:
getExecutionCountin interfaceExecutionContext<R>
-
getLastException
public <T extends java.lang.Throwable> T getLastException()
Description copied from interface:ExecutionContextReturns the last exception that was recorded elsenull.- Specified by:
getLastExceptionin interfaceExecutionContext<R>
-
getLastResult
public R getLastResult()
Description copied from interface:ExecutionContextReturns the last result that was recorded elsenull.- Specified by:
getLastResultin interfaceExecutionContext<R>
-
getLastResult
public R getLastResult(R defaultValue)
Description copied from interface:ExecutionContextReturns the last result that was recorded else thedefaultValue.- Specified by:
getLastResultin interfaceExecutionContext<R>
-
getStartTime
public java.time.Instant getStartTime()
Description copied from interface:ExecutionContextReturns the time that the initial execution started.- Specified by:
getStartTimein interfaceExecutionContext<R>
-
isFirstAttempt
public boolean isFirstAttempt()
Description copied from interface:ExecutionContextReturnstruewhen an execution result has not yet been recorded, meaning this is the first execution attempt.- Specified by:
isFirstAttemptin interfaceExecutionContext<R>
-
isRetry
public boolean isRetry()
Description copied from interface:ExecutionContextReturnstruewhen an execution result has already been recorded, meaning the execution is being retried.- Specified by:
isRetryin interfaceExecutionContext<R>
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-