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
public abstract class PolicyExecutor<R> extends java.lang.Object
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
Fields Modifier and Type Field Description private EventHandler<R>
failureHandler
private FailurePolicy<R>
failurePolicy
Optional APIs for policies that support themprivate int
policyIndex
Index of the policy relative to other policies in a composition, innermost firstprivate EventHandler<R>
successHandler
-
Constructor Summary
Constructors Modifier Constructor Description protected
PolicyExecutor(Policy<R> policy, int policyIndex)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.function.Function<SyncExecutionInternal<R>,ExecutionResult<R>>
apply(java.util.function.Function<SyncExecutionInternal<R>,ExecutionResult<R>> innerFn, Scheduler scheduler)
Performs an execution by calling pre-execute else calling the supplier and doing a post-execute.java.util.function.Function<AsyncExecutionInternal<R>,java.util.concurrent.CompletableFuture<ExecutionResult<R>>>
applyAsync(java.util.function.Function<AsyncExecutionInternal<R>,java.util.concurrent.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
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
.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 java.util.concurrent.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)
.ExecutionResult<R>
postExecute(ExecutionInternal<R> execution, ExecutionResult<R> result)
Performs synchronous post-execution handling for aresult
.protected java.util.concurrent.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>
preExecute()
Called before execution to return an alternative result or exception such as if execution is not allowed or needed.protected java.util.concurrent.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 Detail
-
policyIndex
private final int policyIndex
Index of the policy relative to other policies in a composition, innermost first
-
failurePolicy
private final FailurePolicy<R> failurePolicy
Optional APIs for policies that support them
-
successHandler
private final EventHandler<R> successHandler
-
failureHandler
private final EventHandler<R> failureHandler
-
-
Method Detail
-
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
protected ExecutionResult<R> preExecute()
Called before execution to return an alternative result or exception such as if execution is not allowed or needed.
-
preExecuteAsync
protected java.util.concurrent.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 java.util.function.Function<SyncExecutionInternal<R>,ExecutionResult<R>> apply(java.util.function.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 java.util.function.Function<AsyncExecutionInternal<R>,java.util.concurrent.CompletableFuture<ExecutionResult<R>>> applyAsync(java.util.function.Function<AsyncExecutionInternal<R>,java.util.concurrent.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
public ExecutionResult<R> postExecute(ExecutionInternal<R> execution, ExecutionResult<R> result)
Performs synchronous post-execution handling for aresult
.
-
postExecuteAsync
protected java.util.concurrent.CompletableFuture<ExecutionResult<R>> postExecuteAsync(AsyncExecutionInternal<R> execution, ExecutionResult<R> result, Scheduler scheduler, FailsafeFuture<R> future)
Performs potentially asynchronous post-execution handling for aresult
.
-
isFailure
protected boolean isFailure(ExecutionResult<R> result)
Returns whether theresult
is a success according to the policy. If the {code result} has no result, it is not a failure.
-
onSuccess
protected void onSuccess(ExecutionResult<R> result)
Performs post-execution handling for aresult
that is considered a success according toisFailure(ExecutionResult)
.
-
onFailure
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
.
-
onFailureAsync
protected java.util.concurrent.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
private void handleSuccess(ExecutionResult<R> result, ExecutionContext<R> context)
-
handleFailure
private void handleFailure(ExecutionResult<R> result, ExecutionContext<R> context)
-
-