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
Futureare 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 interfaceA builder for configuring fault tolerance strategies. -
Method Summary
Modifier and TypeMethodDescriptionadaptCallable(Callable<T> action) Adapts givenactionto an action guarded by this configured set of fault tolerance strategies.default RunnableadaptRunnable(Runnable action) Adapts givenactionto an action guarded by this configured set of fault tolerance strategies.adaptSupplier(Supplier<T> action) Adapts givenactionto an action guarded by this configured set of fault tolerance strategies.Calls givenactionand guards the call by this configured set of fault tolerance strategies.static CircuitBreakerMaintenanceReturns aCircuitBreakerMaintenanceinstance that provides maintenance access to existing circuit breakers.static <T> FaultTolerance.Builder<T, FaultTolerance<T>> create()Returns a builder that, at the end, returns aFaultToleranceobject 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 aFaultToleranceobject 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 aCallableguarding the givenaction.createAsyncRunnable(Runnable action) Returns a builder that, at the end, returns aRunnableguarding the givenaction.static <T> FaultTolerance.Builder<CompletionStage<T>, Supplier<CompletionStage<T>>> createAsyncSupplier(Supplier<CompletionStage<T>> action) Returns a builder that, at the end, returns aSupplierguarding the givenaction.static <T> FaultTolerance.Builder<T, Callable<T>> createCallable(Callable<T> action) Returns a builder that, at the end, returns aCallableguarding the givenaction.static FaultTolerance.Builder<Void, Runnable> createRunnable(Runnable action) Returns a builder that, at the end, returns aRunnableguarding the givenaction.static <T> FaultTolerance.Builder<T, Supplier<T>> createSupplier(Supplier<T> action) Returns a builder that, at the end, returns aSupplierguarding the givenaction.default TCalls givenactionand guards the call by this configured set of fault tolerance strategies.default voidCalls givenactionand guards the call by this configured set of fault tolerance strategies.
-
Method Details
-
circuitBreakerMaintenance
Returns aCircuitBreakerMaintenanceinstance that provides maintenance access to existing circuit breakers. -
createCallable
Returns a builder that, at the end, returns aCallableguarding the givenaction. Theactionis synchronous and is always executed on the original thread. -
createSupplier
Returns a builder that, at the end, returns aSupplierguarding the givenaction. Theactionis synchronous and is always executed on the original thread. -
createRunnable
Returns a builder that, at the end, returns aRunnableguarding the givenaction. Theactionis synchronous and is always executed on the original thread. -
create
Returns a builder that, at the end, returns aFaultToleranceobject 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 aCallableguarding the givenaction. Theactionis 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 aSupplierguarding the givenaction. Theactionis asynchronous and may be offloaded to another thread. -
createAsyncRunnable
Returns a builder that, at the end, returns aRunnableguarding the givenaction. Theactionis 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 aFaultToleranceobject 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 givenactionand guards the call by this configured set of fault tolerance strategies. If thisFaultToleranceinstance was created usingcreate(), the action is synchronous and is always executed on the same thread that calls this method. If thisFaultToleranceinstance 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 givenactionand guards the call by this configured set of fault tolerance strategies. If thisFaultToleranceinstance was created usingcreate*, the action is synchronous and is always executed on the same thread that calls this method. If thisFaultToleranceinstance was created usingcreateAsync*, the action is asynchronous and may be offloaded to another thread depending on how the builder was configured. -
run
Calls givenactionand guards the call by this configured set of fault tolerance strategies. If thisFaultToleranceinstance was created usingcreate(), the action is synchronous and is always executed on the same thread that calls this method. If thisFaultToleranceinstance was created usingcreateAsync(), the action is asynchronous and may be offloaded to another thread depending on how the builder was configured. -
adaptCallable
Adapts givenactionto 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 givenactionto 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 givenactionto 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:
-