Package dev.failsafe.spi
Class PolicyExecutor<R>
java.lang.Object
dev.failsafe.spi.PolicyExecutor<R>
- Type Parameters:
R
- result type
- Direct Known Subclasses:
BulkheadExecutor
,CircuitBreakerExecutor
,FallbackExecutor
,RateLimiterExecutor
,RetryPolicyExecutor
,TimeoutExecutor
Handles execution and execution results according to a policy. May contain pre-execution and post-execution
behaviors. Each PolicyExecutor makes its own determination about whether an execution result is a success or
failure.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final EventHandler
<R> private final FailurePolicy
<R> Optional APIs for policies that support themprivate final int
Index of the policy relative to other policies in a composition, innermost firstprivate final EventHandler
<R> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionapply
(Function<SyncExecutionInternal<R>, ExecutionResult<R>> innerFn, Scheduler scheduler) Performs an execution by calling pre-execute else calling the supplier and doing a post-execute.applyAsync
(Function<AsyncExecutionInternal<R>, CompletableFuture<ExecutionResult<R>>> innerFn, Scheduler scheduler, FailsafeFuture<R> future) Performs an async execution by calling pre-execute else calling the supplier and doing a post-execute.int
Returns the index of the policy relative to other policies in a composition, where the innermost policy in a composition has an index of0
.private void
handleFailure
(ExecutionResult<R> result, ExecutionContext<R> context) private void
handleSuccess
(ExecutionResult<R> result, ExecutionContext<R> context) protected boolean
isFailure
(ExecutionResult<R> result) Returns whether theresult
is a success according to the policy.protected ExecutionResult
<R> onFailure
(ExecutionContext<R> context, ExecutionResult<R> result) Performs post-execution handling for aresult
that is considered a failure according toisFailure(ExecutionResult)
, possibly creating a new result, else returning the originalresult
.protected CompletableFuture
<ExecutionResult<R>> onFailureAsync
(ExecutionContext<R> context, ExecutionResult<R> result, Scheduler scheduler, FailsafeFuture<R> future) Performs potentially asynchrononus post-execution handling for a failedresult
, possibly creating a new result, else returning the originalresult
.protected void
onSuccess
(ExecutionResult<R> result) Performs post-execution handling for aresult
that is considered a success according toisFailure(ExecutionResult)
.postExecute
(ExecutionInternal<R> execution, ExecutionResult<R> result) Performs synchronous post-execution handling for aresult
.protected CompletableFuture
<ExecutionResult<R>> postExecuteAsync
(AsyncExecutionInternal<R> execution, ExecutionResult<R> result, Scheduler scheduler, FailsafeFuture<R> future) Performs potentially asynchronous post-execution handling for aresult
.protected ExecutionResult
<R> Called before execution to return an alternative result or exception such as if execution is not allowed or needed.protected CompletableFuture
<ExecutionResult<R>> preExecuteAsync
(Scheduler scheduler, FailsafeFuture<R> future) Called before an async execution to return an alternative result or exception such as if execution is not allowed or needed.
-
Field Details
-
policyIndex
private final int policyIndexIndex of the policy relative to other policies in a composition, innermost first -
failurePolicy
Optional APIs for policies that support them -
successHandler
-
failureHandler
-
-
Constructor Details
-
PolicyExecutor
-
-
Method Details
-
getPolicyIndex
public int getPolicyIndex()Returns the index of the policy relative to other policies in a composition, where the innermost policy in a composition has an index of0
. -
preExecute
Called before execution to return an alternative result or exception such as if execution is not allowed or needed. -
preExecuteAsync
protected CompletableFuture<ExecutionResult<R>> preExecuteAsync(Scheduler scheduler, FailsafeFuture<R> future) Called before an async execution to return an alternative result or exception such as if execution is not allowed or needed. Returnsnull
if pre execution is not performed. If the resulting future is completed with anon-result
, then execution and post-execution should still be performed. If the resulting future is completed withnull
, then the execution is assumed to have been cancelled. -
apply
public Function<SyncExecutionInternal<R>,ExecutionResult<R>> apply(Function<SyncExecutionInternal<R>, ExecutionResult<R>> innerFn, Scheduler scheduler) Performs an execution by calling pre-execute else calling the supplier and doing a post-execute. -
applyAsync
public Function<AsyncExecutionInternal<R>,CompletableFuture<ExecutionResult<R>>> applyAsync(Function<AsyncExecutionInternal<R>, CompletableFuture<ExecutionResult<R>>> innerFn, Scheduler scheduler, FailsafeFuture<R> future) Performs an async execution by calling pre-execute else calling the supplier and doing a post-execute. Implementors must handle a null result from a supplier, which indicates that an async execution has occurred, that a result will be recorded separately, and that postExecute handling should not be performed. -
postExecute
Performs synchronous post-execution handling for aresult
. -
postExecuteAsync
protected CompletableFuture<ExecutionResult<R>> postExecuteAsync(AsyncExecutionInternal<R> execution, ExecutionResult<R> result, Scheduler scheduler, FailsafeFuture<R> future) Performs potentially asynchronous post-execution handling for aresult
. -
isFailure
Returns whether theresult
is a success according to the policy. If the {code result} has no result, it is not a failure. -
onSuccess
Performs post-execution handling for aresult
that is considered a success according toisFailure(ExecutionResult)
. -
onFailure
Performs post-execution handling for aresult
that is considered a failure according toisFailure(ExecutionResult)
, possibly creating a new result, else returning the originalresult
. -
onFailureAsync
protected CompletableFuture<ExecutionResult<R>> onFailureAsync(ExecutionContext<R> context, ExecutionResult<R> result, Scheduler scheduler, FailsafeFuture<R> future) Performs potentially asynchrononus post-execution handling for a failedresult
, possibly creating a new result, else returning the originalresult
. -
handleSuccess
-
handleFailure
-