R - result typepublic class FailsafeExecutor<R> extends PolicyListeners<FailsafeExecutor<R>,R>
An executor that handles failures according to configured policies. Can be created via Failsafe.with(Policy[]).
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 via the returned
CompletableFuture, even those run by a ForkJoinPool implementation.
Executions that are cancelled or timed out while blocked or waiting will be interrupted with an InterruptedException. Executions that do not block can cooperate with cancellation by periodiically checking for
Thread.currentThread().isInterrupted() and exit if true.
| Modifier and Type | Method and Description |
|---|---|
<T extends R> |
get(CheckedSupplier<T> supplier)
Executes the
supplier until a successful result is returned or the configured policies are exceeded. |
<T extends R> |
get(ContextualSupplier<T> supplier)
Executes the
supplier until a successful result is returned or the configured policies are exceeded. |
<T extends R> |
getAsync(CheckedSupplier<T> supplier)
Executes the
supplier asynchronously until a successful result is returned or the configured policies are
exceeded. |
<T extends R> |
getAsync(ContextualSupplier<T> supplier)
Executes the
supplier asynchronously until a successful result is returned or the configured policies are
exceeded. |
<T extends R> |
getAsyncExecution(AsyncSupplier<T> supplier)
Executes the
supplier asynchronously until a successful result is returned or the configured policies are
exceeded. |
<T extends R> |
getStageAsync(CheckedSupplier<? extends CompletionStage<T>> supplier)
Executes the
supplier asynchronously until the resulting future is successfully completed or the configured
policies are exceeded. |
<T extends R> |
getStageAsync(ContextualSupplier<? extends CompletionStage<T>> supplier)
Executes the
supplier asynchronously until the resulting future is successfully completed or the configured
policies are exceeded. |
<T extends R> |
getStageAsyncExecution(AsyncSupplier<? extends CompletionStage<T>> supplier)
Executes the
supplier asynchronously until the resulting future is successfully completed or the configured
policies are exceeded. |
FailsafeExecutor<R> |
onComplete(CheckedConsumer<? extends ExecutionCompletedEvent<R>> listener)
Registers the
listener to be called when an execution is complete for all of the configured policies are
exceeded. |
FailsafeExecutor<R> |
onFailure(CheckedConsumer<? extends ExecutionCompletedEvent<R>> listener)
Registers the
listener to be called when an execution fails. |
FailsafeExecutor<R> |
onSuccess(CheckedConsumer<? extends ExecutionCompletedEvent<R>> listener)
Registers the
listener to be called when an execution is successful. |
void |
run(CheckedRunnable runnable)
Executes the
runnable until successful or until the configured policies are exceeded. |
void |
run(ContextualRunnable runnable)
Executes the
runnable until successful or until the configured policies are exceeded. |
CompletableFuture<Void> |
runAsync(CheckedRunnable runnable)
Executes the
runnable asynchronously until successful or until the configured policies are exceeded. |
CompletableFuture<Void> |
runAsync(ContextualRunnable runnable)
Executes the
runnable asynchronously until successful or until the configured policies are exceeded. |
CompletableFuture<Void> |
runAsyncExecution(AsyncRunnable runnable)
Executes the
runnable asynchronously until successful or until the configured policies are exceeded. |
FailsafeExecutor<R> |
with(ExecutorService executor)
Configures the
executor to use for performing asynchronous executions and listener callbacks. |
FailsafeExecutor<R> |
with(ScheduledExecutorService executor)
Configures the
executor to use for performing asynchronous executions and listener callbacks. |
FailsafeExecutor<R> |
with(Scheduler scheduler)
Configures the
scheduler to use for performing asynchronous executions and listener callbacks. |
public <T extends R> T get(CheckedSupplier<T> supplier)
supplier until a successful result is returned or the configured policies are exceeded.NullPointerException - if the supplier is nullFailsafeException - if the supplier fails with a checked Exception or if interrupted while
waiting to perform a retry.CircuitBreakerOpenException - if a configured circuit is open.public <T extends R> T get(ContextualSupplier<T> supplier)
supplier until a successful result is returned or the configured policies are exceeded.NullPointerException - if the supplier is nullFailsafeException - if the supplier fails with a checked Exception or if interrupted while
waiting to perform a retry.CircuitBreakerOpenException - if a configured circuit is open.public <T extends R> CompletableFuture<T> getAsync(CheckedSupplier<T> supplier)
supplier asynchronously until a successful result is returned or the configured policies are
exceeded.
If a configured circuit breaker is open, the resulting future is completed with CircuitBreakerOpenException.
NullPointerException - if the supplier is nullRejectedExecutionException - if the supplier cannot be scheduled for executionpublic <T extends R> CompletableFuture<T> getAsync(ContextualSupplier<T> supplier)
supplier asynchronously until a successful result is returned or the configured policies are
exceeded.
If a configured circuit breaker is open, the resulting future is completed with CircuitBreakerOpenException.
NullPointerException - if the supplier is nullRejectedExecutionException - if the supplier cannot be scheduled for executionpublic <T extends R> CompletableFuture<T> getAsyncExecution(AsyncSupplier<T> supplier)
supplier asynchronously until a successful result is returned or the configured policies are
exceeded. This method is intended for integration with asynchronous code. Retries must be manually scheduled via
one of the AsyncExecution.retry methods.
If a configured circuit breaker is open, the resulting future is completed with CircuitBreakerOpenException.
NullPointerException - if the supplier is nullRejectedExecutionException - if the supplier cannot be scheduled for executionpublic FailsafeExecutor<R> onComplete(CheckedConsumer<? extends ExecutionCompletedEvent<R>> listener)
listener to be called when an execution is complete for all of the configured policies are
exceeded.
Note: Any exceptions that are thrown from within the listener are ignored.
public FailsafeExecutor<R> onFailure(CheckedConsumer<? extends ExecutionCompletedEvent<R>> listener)
listener to be called when an execution fails. If multiple policies, are configured, this
handler is called when execution is complete and any policy fails.
Note: Any exceptions that are thrown from within the listener are ignored.
onFailure in class PolicyListeners<FailsafeExecutor<R>,R>public FailsafeExecutor<R> onSuccess(CheckedConsumer<? extends ExecutionCompletedEvent<R>> listener)
listener to 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 the onFailure(CheckedConsumer) registered listener is called instead.
Note: Any exceptions that are thrown from within the listener are ignored.
onSuccess in class PolicyListeners<FailsafeExecutor<R>,R>public <T extends R> CompletableFuture<T> getStageAsync(CheckedSupplier<? extends CompletionStage<T>> supplier)
supplier asynchronously until the resulting future is successfully completed or the configured
policies are exceeded.
If a configured circuit breaker is open, the resulting future is completed exceptionally with CircuitBreakerOpenException.
NullPointerException - if the supplier is nullRejectedExecutionException - if the supplier cannot be scheduled for executionpublic <T extends R> CompletableFuture<T> getStageAsync(ContextualSupplier<? extends CompletionStage<T>> supplier)
supplier asynchronously until the resulting future is successfully completed or the configured
policies are exceeded.
If a configured circuit breaker is open, the resulting future is completed exceptionally with CircuitBreakerOpenException.
NullPointerException - if the supplier is nullRejectedExecutionException - if the supplier cannot be scheduled for executionpublic <T extends R> CompletableFuture<T> getStageAsyncExecution(AsyncSupplier<? extends CompletionStage<T>> supplier)
supplier asynchronously until the resulting future is successfully completed or the configured
policies are exceeded. This method is intended for integration with asynchronous code. Retries must be manually
scheduled via one of the AsyncExecution.retry methods.
If a configured circuit breaker is open, the resulting future is completed exceptionally with CircuitBreakerOpenException.
NullPointerException - if the supplier is nullRejectedExecutionException - if the supplier cannot be scheduled for executionpublic void run(CheckedRunnable runnable)
runnable until successful or until the configured policies are exceeded.NullPointerException - if the runnable is nullFailsafeException - if the supplier fails with a checked Exception or if interrupted while
waiting to perform a retry.CircuitBreakerOpenException - if a configured circuit is open.public void run(ContextualRunnable runnable)
runnable until successful or until the configured policies are exceeded.NullPointerException - if the runnable is nullFailsafeException - if the runnable fails with a checked Exception or if interrupted while
waiting to perform a retry.CircuitBreakerOpenException - if a configured circuit is open.public CompletableFuture<Void> runAsync(CheckedRunnable runnable)
runnable asynchronously until successful or until the configured policies are exceeded.
If a configured circuit breaker is open, the resulting future is completed with CircuitBreakerOpenException.
NullPointerException - if the runnable is nullRejectedExecutionException - if the runnable cannot be scheduled for executionpublic CompletableFuture<Void> runAsync(ContextualRunnable runnable)
runnable asynchronously until successful or until the configured policies are exceeded.
If a configured circuit breaker is open, the resulting future is completed with CircuitBreakerOpenException.
NullPointerException - if the runnable is nullRejectedExecutionException - if the runnable cannot be scheduled for executionpublic CompletableFuture<Void> runAsyncExecution(AsyncRunnable runnable)
runnable asynchronously until successful or until the configured policies are exceeded. This
method is intended for integration with asynchronous code. Retries must be manually scheduled via one of the AsyncExecution.retry methods.
If a configured circuit breaker is open, the resulting future is completed with CircuitBreakerOpenException.
NullPointerException - if the runnable is nullRejectedExecutionException - if the runnable cannot be scheduled for executionpublic FailsafeExecutor<R> with(ScheduledExecutorService executor)
executor to use for performing asynchronous executions and listener callbacks.NullPointerException - if executor is nullpublic FailsafeExecutor<R> with(ExecutorService executor)
executor to use for performing asynchronous executions and listener callbacks. For
executions that require a delay, an internal ScheduledExecutorService will be used for the delay, then the executor will be used for actual execution.NullPointerException - if executor is nullpublic FailsafeExecutor<R> with(Scheduler scheduler)
scheduler to use for performing asynchronous executions and listener callbacks.NullPointerException - if scheduler is nullCopyright © 2019. All rights reserved.