Interface FaultTolerance<T>
- Type Parameters:
T
- type of value of the guarded action
CompletionStage
.
An instance of this interface represents a configured set of fault tolerance strategies. It can be used to
guard a Callable
, Supplier
or Runnable
invocation, or adapt an unguarded Callable
, Supplier
or Runnable
to a guarded one.
The create*
and createAsync*
methods return a builder that allows configuring all fault tolerance
strategies. Order of builder method invocations does not matter, the fault tolerance strategies are always applied
in a predefined order: fallback > retry > circuit breaker > rate limit > timeout > bulkhead >
thread offload > guarded action.
Two styles of usage are possible. The createCallable(Callable)
and createAsyncCallable(Callable)
methods return a builder that, at the end, returns a Callable
. This is convenient in case you only want to
guard a single action (which is possibly invoked multiple times). Similar methods exist for the Supplier
and Runnable
types.
The create()
and createAsync()
methods return a builder that, at the end, returns
a FaultTolerance
instance, which is useful when you need to guard multiple actions with the same set
of fault tolerance strategies. Note that bulkheads, circuit breakers and rate limits are stateful, so there's
a big difference between guarding multiple actions using the same FaultTolerance
object and using a separate
FaultTolerance
object for each action. Using a single FaultTolerance
instance to guard multiple
actions means that a single bulkhead, circuit breaker and/or rate limit will be shared among all those actions.
This API is essentially a programmatic equivalent to the declarative, annotation-based API of MicroProfile Fault Tolerance and SmallRye Fault Tolerance. It shares the set of fault tolerance strategies, their invocation order and behavior, their configuration properties, etc. Notable differences are:
- asynchronous actions of type
Future
are not supported; - the fallback, circuit breaker and retry strategies always inspect the cause chain of exceptions, following the behavior of SmallRye Fault Tolerance in the non-compatible mode.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interface
A builder for configuring fault tolerance strategies. -
Method Summary
Modifier and TypeMethodDescriptionadaptCallable
(Callable<T> action) Adapts givenaction
to an action guarded by this configured set of fault tolerance strategies.default Runnable
adaptRunnable
(Runnable action) Adapts givenaction
to an action guarded by this configured set of fault tolerance strategies.adaptSupplier
(Supplier<T> action) Adapts givenaction
to an action guarded by this configured set of fault tolerance strategies.Calls givenaction
and guards the call by this configured set of fault tolerance strategies.<U> FaultTolerance
<U> cast()
Casts thisFaultTolerance
object so that it guards actions of a different type.<U> FaultTolerance
<U> Casts thisFaultTolerance
object so that it guards actions of a different type.static CircuitBreakerMaintenance
Returns aCircuitBreakerMaintenance
instance that provides maintenance access to existing circuit breakers.static <T> FaultTolerance.Builder
<T, FaultTolerance<T>> create()
Returns a builder that, at the end, returns aFaultTolerance
object representing a set of configured fault tolerance strategies.static <T> FaultTolerance.Builder
<CompletionStage<T>, FaultTolerance<CompletionStage<T>>> Returns a builder that, at the end, returns aFaultTolerance
object representing a set of configured fault tolerance strategies.static <T> FaultTolerance.Builder
<CompletionStage<T>, Callable<CompletionStage<T>>> createAsyncCallable
(Callable<CompletionStage<T>> action) Returns a builder that, at the end, returns aCallable
guarding the givenaction
.createAsyncRunnable
(Runnable action) Returns a builder that, at the end, returns aRunnable
guarding the givenaction
.static <T> FaultTolerance.Builder
<CompletionStage<T>, Supplier<CompletionStage<T>>> createAsyncSupplier
(Supplier<CompletionStage<T>> action) Returns a builder that, at the end, returns aSupplier
guarding the givenaction
.static <T> FaultTolerance.Builder
<T, Callable<T>> createCallable
(Callable<T> action) Returns a builder that, at the end, returns aCallable
guarding the givenaction
.static FaultTolerance.Builder
<Void, Runnable> createRunnable
(Runnable action) Returns a builder that, at the end, returns aRunnable
guarding the givenaction
.static <T> FaultTolerance.Builder
<T, Supplier<T>> createSupplier
(Supplier<T> action) Returns a builder that, at the end, returns aSupplier
guarding the givenaction
.default T
Calls givenaction
and guards the call by this configured set of fault tolerance strategies.default void
Calls givenaction
and guards the call by this configured set of fault tolerance strategies.
-
Method Details
-
circuitBreakerMaintenance
Returns aCircuitBreakerMaintenance
instance that provides maintenance access to existing circuit breakers. -
createCallable
Returns a builder that, at the end, returns aCallable
guarding the givenaction
. Theaction
is synchronous and is always executed on the original thread. -
createSupplier
Returns a builder that, at the end, returns aSupplier
guarding the givenaction
. Theaction
is synchronous and is always executed on the original thread. -
createRunnable
Returns a builder that, at the end, returns aRunnable
guarding the givenaction
. Theaction
is synchronous and is always executed on the original thread. -
create
Returns a builder that, at the end, returns aFaultTolerance
object representing a set of configured fault tolerance strategies. It can be used to execute synchronous actions usingcall(Callable)
,get(Supplier)
orrun(Runnable)
.This method usually has to be called with an explicitly provided type argument. For example:
FaultTolerance.<String>create()
. -
createAsyncCallable
static <T> FaultTolerance.Builder<CompletionStage<T>,Callable<CompletionStage<T>>> createAsyncCallable(Callable<CompletionStage<T>> action) Returns a builder that, at the end, returns aCallable
guarding the givenaction
. Theaction
is asynchronous and may be offloaded to another thread. -
createAsyncSupplier
static <T> FaultTolerance.Builder<CompletionStage<T>,Supplier<CompletionStage<T>>> createAsyncSupplier(Supplier<CompletionStage<T>> action) Returns a builder that, at the end, returns aSupplier
guarding the givenaction
. Theaction
is asynchronous and may be offloaded to another thread. -
createAsyncRunnable
Returns a builder that, at the end, returns aRunnable
guarding the givenaction
. Theaction
is asynchronous and may be offloaded to another thread. -
createAsync
static <T> FaultTolerance.Builder<CompletionStage<T>,FaultTolerance<CompletionStage<T>>> createAsync()Returns a builder that, at the end, returns aFaultTolerance
object representing a set of configured fault tolerance strategies. It can be used to execute asynchronous actions usingcall(Callable)
,get(Supplier)
orrun(Runnable)
.This method usually has to be called with an explicitly provided type argument. For example:
FaultTolerance.<String>createAsync()
. -
call
Calls givenaction
and guards the call by this configured set of fault tolerance strategies. If thisFaultTolerance
instance was created usingcreate()
, the action is synchronous and is always executed on the same thread that calls this method. If thisFaultTolerance
instance was created usingcreateAsync()
, the action is asynchronous and may be offloaded to another thread depending on how the builder was configured.- Throws:
Exception
-
get
Calls givenaction
and guards the call by this configured set of fault tolerance strategies. If thisFaultTolerance
instance was created usingcreate*
, the action is synchronous and is always executed on the same thread that calls this method. If thisFaultTolerance
instance was created usingcreateAsync*
, the action is asynchronous and may be offloaded to another thread depending on how the builder was configured. -
run
Calls givenaction
and guards the call by this configured set of fault tolerance strategies. If thisFaultTolerance
instance was created usingcreate()
, the action is synchronous and is always executed on the same thread that calls this method. If thisFaultTolerance
instance was created usingcreateAsync()
, the action is asynchronous and may be offloaded to another thread depending on how the builder was configured. -
adaptCallable
Adapts givenaction
to an action guarded by this configured set of fault tolerance strategies. Useful when the action has to be called multiple times.Equivalent to
() -> call(action)
.- See Also:
-
adaptSupplier
Adapts givenaction
to an action guarded by this configured set of fault tolerance strategies. Useful when the action has to be called multiple times.Equivalent to
() -> get(action)
.- See Also:
-
adaptRunnable
Adapts givenaction
to an action guarded by this configured set of fault tolerance strategies. Useful when the action has to be called multiple times.Equivalent to
() -> run(action)
.- See Also:
-
cast
Casts thisFaultTolerance
object so that it guards actions of a different type. Since the type of the action is only used in fallback, this is usually safe; if thisFaultTolerance
object contains a fallback, this method throws an exception.Note that this method may only be used to cast synchronous
FaultTolerance
. If thisFaultTolerance
object guards asynchronous actions, this method throws an exception.- Type Parameters:
U
- type of value of the guarded action
-
castAsync
Casts thisFaultTolerance
object so that it guards actions of a different type. Since the type of the action is only used in fallback, this is usually safe; if thisFaultTolerance
object contains a fallback, this method throws an exception.Note that this method may only be used to cast asynchronous
FaultTolerance
of given type (such asCompletionStage
orUni
). If thisFaultTolerance
object guards synchronous actions or asynchronous actions of different type, this method throws an exception.- Type Parameters:
U
- type of value of the guarded action- Parameters:
asyncType
- the asynchronous type, such asCompletionStage
orUni
-