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) booleanprivate final AtomicIntegerprivate Instant(package private) CheckedRunnable(package private) int(package private) booleanprivate final AtomicIntegerprivate final AtomicReference<ExecutionInternal<R>> (package private) final List<PolicyExecutor<R>> private booleanprivate 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 TypeMethodDescriptionbooleancancel()Called indirectly by users.voidcancel(PolicyExecutor<R> policyExecutor) Called by policies.intGets the number of execution attempts so far, including attempts that are blocked before being executed, such as when aCircuitBreakeris open.Returns the elapsed time since the last execution attempt began.Returns the elapsed time since initial execution began.intGets 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.booleanReturns whether the execution has been cancelled.booleanisCancelled(PolicyExecutor<R> policyExecutor) Returns whether the execution is considered cancelled for thepolicyExecutor.booleanReturnstruewhen an execution result has not yet been recorded, meaning this is the first execution attempt.booleanReturns 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) Sets thecancelCallbackto be called if the execution is cancelled, such as by the resultingCallorCompletableFuture, or aTimeout.(package private) ExecutionResult<R> postExecute(ExecutionResult<R> result) Externally called.voidCalled 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.voidRecords 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:ExecutionInternalReturns the recorded result for an execution attempt.- Specified by:
getResultin interfaceExecutionInternal<R>
-
onCancel
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
Description copied from interface:ExecutionInternalRecords theresultif the execution has beenpre-executedand a result has not already been recorded.- Specified by:
recordin interfaceExecutionInternal<R>
-
postExecute
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:
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
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
Description copied from interface:ExecutionInternalReturns whether the execution is considered cancelled for thepolicyExecutor.- Specified by:
isCancelledin interfaceExecutionInternal<R>
-
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
Description copied from interface:ExecutionInternalReturns the most recent execution to be attempted.- Specified by:
getLatestin interfaceExecutionInternal<R>
-
getElapsedTime
Description copied from interface:ExecutionContextReturns the elapsed time since initial execution began.- Specified by:
getElapsedTimein interfaceExecutionContext<R>
-
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
Description copied from interface:ExecutionContextReturns the last exception that was recorded elsenull.- Specified by:
getLastExceptionin interfaceExecutionContext<R>
-
getLastResult
Description copied from interface:ExecutionContextReturns the last result that was recorded elsenull.- Specified by:
getLastResultin interfaceExecutionContext<R>
-
getLastResult
Description copied from interface:ExecutionContextReturns the last result that was recorded else thedefaultValue.- Specified by:
getLastResultin interfaceExecutionContext<R>
-
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
-