Class FailsafeExecutor<R>
- Type Parameters:
R- result type
An executor that handles failures according to configured policies. Can be created via
Failsafe.with(Policy, Policy[]) to support policy based execution failure handling, or Failsafe.none() to support execution with no failure handling.
Async executions are run by default on the ForkJoinPool.commonPool(). Alternative executors can be configured
via with(ScheduledExecutorService) and similar methods. All async executions are cancellable and
interruptable via the returned CompletableFuture, even those run by a ForkJoinPool or CompletionStage.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate EventHandler<R> (package private) final BiConsumer<ExecutionResult<R>, ExecutionContext<R>> private Executorprivate EventHandler<R> Policies sorted outermost firstprivate Schedulerprivate EventHandler<R> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate <T> Tcall(ContextualSupplier<T, T> innerSupplier) Calls theinnerSuppliersynchronously, handling results according to the configured policies.private <T> CompletableFuture<T> callAsync(Function<FailsafeFuture<T>, Function<AsyncExecutionInternal<T>, CompletableFuture<ExecutionResult<T>>>> innerFn, boolean asyncExecution) Calls the asynchronousinnerFnvia the configured Scheduler, handling results according to the configured policies.private <T> Call<T> callSync(ContextualSupplier<T, T> innerSupplier) Returns a Call that calls theinnerSuppliersynchronously, handling results according to the configured policies.<P extends Policy<R>>
FailsafeExecutor<R> compose(P innerPolicy) Returns a newFailsafeExecutorthat composes the currently configured policies around the giveninnerPolicy.<T extends R>
Tget(CheckedSupplier<T> supplier) Executes thesupplieruntil a successful result is returned or the configured policies are exceeded.<T extends R>
Tget(ContextualSupplier<T, T> supplier) Executes thesupplieruntil a successful result is returned or the configured policies are exceeded.<T extends R>
CompletableFuture<T> getAsync(CheckedSupplier<T> supplier) Executes thesupplierasynchronously until a successful result is returned or the configured policies are exceeded.<T extends R>
CompletableFuture<T> getAsync(ContextualSupplier<T, T> supplier) Executes thesupplierasynchronously until a successful result is returned or the configured policies are exceeded.<T extends R>
CompletableFuture<T> getAsyncExecution(AsyncRunnable<T> runnable) This method is intended for integration with asynchronous code.Returns the currently configured policies.<T extends R>
CompletableFuture<T> getStageAsync(CheckedSupplier<? extends CompletionStage<T>> supplier) Executes thesupplierasynchronously until the resulting future is successfully completed or the configured policies are exceeded.<T extends R>
CompletableFuture<T> getStageAsync(ContextualSupplier<T, ? extends CompletionStage<T>> supplier) Executes thesupplierasynchronously until the resulting future is successfully completed or the configured policies are exceeded.newCall(ContextualRunnable<Void> runnable) Returns a call that can execute therunnableuntil a successful result is returned or the configured policies are exceeded.newCall(ContextualSupplier<T, T> supplier) Returns a call that can execute thesupplieruntil a successful result is returned or the configured policies are exceeded.onComplete(EventListener<ExecutionCompletedEvent<R>> listener) Registers thelistenerto be called when an execution is complete.onFailure(EventListener<ExecutionCompletedEvent<R>> listener) Registers thelistenerto be called when an execution fails.onSuccess(EventListener<ExecutionCompletedEvent<R>> listener) Registers thelistenerto be called when an execution is successful.voidrun(CheckedRunnable runnable) Executes therunnableuntil successful or until the configured policies are exceeded.voidrun(ContextualRunnable<Void> runnable) Executes therunnableuntil successful or until the configured policies are exceeded.runAsync(CheckedRunnable runnable) Executes therunnableasynchronously until successful or until the configured policies are exceeded.runAsync(ContextualRunnable<Void> runnable) Executes therunnableasynchronously until successful or until the configured policies are exceeded.runAsyncExecution(AsyncRunnable<Void> runnable) This method is intended for integration with asynchronous code.Configures theschedulerto use for performing asynchronous executions and listener callbacks.Configures theexecutorto use as a wrapper around executions.with(ExecutorService executorService) Configures theexecutorServiceto use for performing asynchronous executions and listener callbacks.with(ScheduledExecutorService scheduledExecutorService) Configures thescheduledExecutorServiceto use for performing asynchronous executions and listener callbacks.
-
Field Details
-
scheduler
-
executor
-
policies
-
completeHandler
-
failureHandler
-
successHandler
-
completionHandler
-
-
Constructor Details
-
FailsafeExecutor
- Throws:
IllegalArgumentException- ifpoliciesis empty
-
-
Method Details
-
getPolicies
-
compose
Returns a newFailsafeExecutorthat composes the currently configured policies around the giveninnerPolicy. For example, consider:Failsafe.with(fallback).compose(retryPolicy).compose(circuitBreaker);
This results in the following internal composition when executing arunnableorsupplierand handling its result:Fallback(RetryPolicy(CircuitBreaker(Supplier)))
This means theCircuitBreakeris first to evaluate theSupplier's result, then theRetryPolicy, then theFallback. Each policy makes its own determination as to whether the result represents a failure. This allows different policies to be used for handling different types of failures.- Throws:
NullPointerException- ifinnerPolicyis null- See Also:
-
get
Executes thesupplieruntil a successful result is returned or the configured policies are exceeded.- Throws:
NullPointerException- if thesupplieris nullFailsafeException- if the execution fails with a checked Exception.Throwable.getCause()can be used to learn the underlying checked exception.
-
get
Executes thesupplieruntil a successful result is returned or the configured policies are exceeded.- Throws:
NullPointerException- if thesupplieris nullFailsafeException- if the execution fails with a checked Exception.Throwable.getCause()can be used to learn the underlying checked exception.
-
newCall
Returns a call that can execute therunnableuntil a successful result is returned or the configured policies are exceeded.- Throws:
NullPointerException- if therunnableis null
-
newCall
Returns a call that can execute thesupplieruntil a successful result is returned or the configured policies are exceeded.- Throws:
NullPointerException- if thesupplieris null
-
getAsync
Executes thesupplierasynchronously until a successful result is returned or the configured policies are exceeded.- Throws:
NullPointerException- if thesupplieris nullRejectedExecutionException- if thesuppliercannot be scheduled for execution
-
getAsync
Executes thesupplierasynchronously until a successful result is returned or the configured policies are exceeded.- Throws:
NullPointerException- if thesupplieris nullRejectedExecutionException- if thesuppliercannot be scheduled for execution
-
getAsyncExecution
This method is intended for integration with asynchronous code.Executes the
runnableasynchronously until a successful result is recorded or the configured policies are exceeded. Executions must be recorded via one of theAsyncExecution.recordmethods which will trigger failure handling, if needed, by the configured policies, else the resultingCompletableFuturewill be completed. Any exception that is thrown from therunnablewill automatically be recorded viaAsyncExecution.recordException(Throwable).- Throws:
NullPointerException- if therunnableis nullRejectedExecutionException- if therunnablecannot be scheduled for execution
-
getStageAsync
public <T extends R> CompletableFuture<T> getStageAsync(CheckedSupplier<? extends CompletionStage<T>> supplier) Executes thesupplierasynchronously until the resulting future is successfully completed or the configured policies are exceeded.Cancelling the resulting
CompletableFuturewill automatically cancels the suppliedCompletionStageif it's aFuture.- Throws:
NullPointerException- if thesupplieris nullRejectedExecutionException- if thesuppliercannot be scheduled for execution
-
getStageAsync
public <T extends R> CompletableFuture<T> getStageAsync(ContextualSupplier<T, ? extends CompletionStage<T>> supplier) Executes thesupplierasynchronously until the resulting future is successfully completed or the configured policies are exceeded.Cancelling the resulting
CompletableFuturewill automatically cancels the suppliedCompletionStageif it's aFuture.- Throws:
NullPointerException- if thesupplieris nullRejectedExecutionException- if thesuppliercannot be scheduled for execution
-
run
Executes therunnableuntil successful or until the configured policies are exceeded.- Throws:
NullPointerException- if therunnableis nullFailsafeException- if the execution fails with a checked Exception.Throwable.getCause()can be used to learn the underlying checked exception.
-
run
Executes therunnableuntil successful or until the configured policies are exceeded.- Throws:
NullPointerException- if therunnableis nullFailsafeException- if the execution fails with a checked Exception.Throwable.getCause()can be used to learn the underlying checked exception.
-
runAsync
Executes therunnableasynchronously until successful or until the configured policies are exceeded.- Throws:
NullPointerException- if therunnableis nullRejectedExecutionException- if therunnablecannot be scheduled for execution
-
runAsync
Executes therunnableasynchronously until successful or until the configured policies are exceeded.- Throws:
NullPointerException- if therunnableis nullRejectedExecutionException- if therunnablecannot be scheduled for execution
-
runAsyncExecution
This method is intended for integration with asynchronous code.Executes the
runnableasynchronously until a successful result is recorded or the configured policies are exceeded. Executions must be recorded via one of theAsyncExecution.recordmethods which will trigger failure handling, if needed, by the configured policies, else the resultingCompletableFuturewill be completed. Any exception that is thrown from therunnablewill automatically be recorded viaAsyncExecution.recordException(Throwable).- Throws:
NullPointerException- if therunnableis nullRejectedExecutionException- if therunnablecannot be scheduled for execution
-
onComplete
Registers thelistenerto be called when an execution is complete. This occurs when an execution is successful according to all policies, or all policies have been exceeded.Note: Any exceptions that are thrown from within the
listenerare ignored. -
onFailure
Registers thelistenerto be called when an execution fails. This occurs when the execution fails according to some policy, and all policies have been exceeded.Note: Any exceptions that are thrown from within the
listenerare ignored. To provide an alternative result for a failed execution, use aFallback. -
onSuccess
Registers thelistenerto be called when an execution is successful. If multiple policies, are configured, this handler is called when execution is complete and all policies succeed. If all policies do not succeed, then theonFailure(EventListener)registered listener is called instead.Note: Any exceptions that are thrown from within the
listenerare ignored. -
with
Configures thescheduledExecutorServiceto use for performing asynchronous executions and listener callbacks.Note: The
scheduledExecutorServiceshould have a core pool size of at least 2 in order fortimeoutsto work.- Throws:
NullPointerException- ifscheduledExecutorServiceis nullIllegalArgumentException- if thescheduledExecutorServicehas a core pool size of less than 2
-
with
Configures theexecutorServiceto use for performing asynchronous executions and listener callbacks. For async executions that require a delay, an internal ScheduledExecutorService will be used for the delay, then theexecutorServicewill be used for actual execution.Note: The
executorServiceshould have a core pool size or parallelism of at least 2 in order fortimeoutsto work.- Throws:
NullPointerException- ifexecutorServiceis null
-
with
Configures theexecutorto use as a wrapper around executions. If theexecutoris actually an instance ofExecutorService, then theexecutorwill be configured viawith(ExecutorService)instead.The
executoris responsible for propagating executions. Executions that normally return a result, such asget(CheckedSupplier)will returnnullsince theExecutorinterface does not support results.The
executorwill not be used forgetStageAsynccalls since those require a returned result.- Throws:
NullPointerException- ifexecutoris null
-
with
Configures theschedulerto use for performing asynchronous executions and listener callbacks.- Throws:
NullPointerException- ifscheduleris null
-
call
Calls theinnerSuppliersynchronously, handling results according to the configured policies. -
callSync
Returns a Call that calls theinnerSuppliersynchronously, handling results according to the configured policies. -
callAsync
private <T> CompletableFuture<T> callAsync(Function<FailsafeFuture<T>, Function<AsyncExecutionInternal<T>, CompletableFuture<ExecutionResult<T>>>> innerFn, boolean asyncExecution) Calls the asynchronousinnerFnvia the configured Scheduler, handling results according to the configured policies.- Parameters:
asyncExecution- whether this is a detached, async execution that must be manually completed- Throws:
NullPointerException- if theinnerFnis nullRejectedExecutionException- if theinnerFncannot be scheduled for execution
-