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
or CompletionStage
.
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. Throwable.getCause()
can be used to learn the checked exception that caused the failure.TimeoutExceededException
- if a configured Timeout
is exceeded.CircuitBreakerOpenException
- if a configured CircuitBreaker
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. Throwable.getCause()
can be used to learn the checked exception that caused the failure.TimeoutExceededException
- if a configured Timeout
is exceeded.CircuitBreakerOpenException
- if a configured CircuitBreaker
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 Timeout
is exceeded, the resulting future is completed exceptionally with TimeoutExceededException
.
If a configured CircuitBreaker
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> getAsync(ContextualSupplier<T> supplier)
supplier
asynchronously until a successful result is returned or the configured policies are
exceeded.
If a configured Timeout
is exceeded, the resulting future is completed exceptionally with TimeoutExceededException
.
If a configured CircuitBreaker
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> 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 Timeout
is exceeded, the resulting future is completed exceptionally with TimeoutExceededException
.
If a configured CircuitBreaker
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(CheckedSupplier<? extends CompletionStage<T>> supplier)
supplier
asynchronously until the resulting future is successfully completed or the configured
policies are exceeded.
If a configured Timeout
is exceeded, the resulting future is completed exceptionally with TimeoutExceededException
.
If a configured CircuitBreaker
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 Timeout
is exceeded, the resulting future is completed exceptionally with TimeoutExceededException
.
If a configured CircuitBreaker
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 Timeout
is exceeded, the resulting future is completed exceptionally with TimeoutExceededException
.
If a configured CircuitBreaker
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 runnable
fails with a checked Exception. Throwable.getCause()
can be used to learn the checked exception that caused the failure.TimeoutExceededException
- if a configured Timeout
is exceeded.CircuitBreakerOpenException
- if a configured CircuitBreaker
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. Throwable.getCause()
can be used to learn the checked exception that caused the failure.TimeoutExceededException
- if a configured Timeout
is exceeded.CircuitBreakerOpenException
- if a configured CircuitBreaker
is open.public CompletableFuture<Void> runAsync(CheckedRunnable runnable)
runnable
asynchronously until successful or until the configured policies are exceeded.
If a configured Timeout
is exceeded, the resulting future is completed exceptionally with TimeoutExceededException
.
If a configured CircuitBreaker
is open, the resulting future is completed exceptionally 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 Timeout
is exceeded, the resulting future is completed exceptionally with TimeoutExceededException
.
If a configured CircuitBreaker
is open, the resulting future is completed exceptionally 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 Timeout
is exceeded, the resulting future is completed exceptionally with TimeoutExceededException
.
If a configured CircuitBreaker
is open, the resulting future is completed exceptionally with CircuitBreakerOpenException
.
NullPointerException
- if the runnable
is nullRejectedExecutionException
- if the runnable
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 FailsafeExecutor<R> with(ScheduledExecutorService executor)
executor
to use for performing asynchronous executions and listener callbacks.
Note: The executor
should have a core pool size of at least 2 in order for timeouts
to
work.
NullPointerException
- if executor
is nullIllegalArgumentException
- if the executor
has a core pool size of less than 2public 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.
Note: The executor
should have a core pool size or parallelism of at least 2 in order for timeouts
to work.
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 © 2020. All rights reserved.