Interface FaultTolerance<T>
-
- Type Parameters:
T- type of value of the guarded action
@Experimental("first attempt at providing programmatic API") public interface FaultTolerance<T>Allows guarding an action with various fault tolerance strategies: bulkhead, circuit breaker, fallback, retry, and timeout. Synchronous as well as asynchronous actions may be guarded, asynchronous actions may optionally be offloaded to another thread. The only supported type for asynchronous actions isCompletionStage.An instance of this interface represents a configured set of fault tolerance strategies. It can be used to guard a
Callable,SupplierorRunnableinvocation, or adapt an unguardedCallable,SupplierorRunnableto a guarded one.The
create*andcreateAsync*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 > timeout > bulkhead > thread offload > guarded action.Two styles of usage are possible. The
createCallable(Callable)andcreateAsyncCallable(Callable)methods return a builder that, at the end, returns aCallable. This is convenient in case you only want to guard a single action (which is possibly invoked multiple times). Similar methods exist for theSupplierandRunnabletypes.The
create()andcreateAsync()methods return a builder that, at the end, returns aFaultToleranceinstance, which is useful when you need to guard multiple actions with the same set of fault tolerance strategies. Note that circuit breakers and bulkheads are stateful, so there's a big difference between guarding multiple actions using the sameFaultToleranceobject and using a separateFaultToleranceobject for each action. Using a singleFaultToleranceinstance to guard multiple actions means that a single circuit breaker and/or bulkhead 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 Classes Modifier and Type Interface Description static interfaceFaultTolerance.Builder<T,R>A builder for configuring fault tolerance strategies.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default Callable<T>adaptCallable(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.default Supplier<T>adaptSupplier(Supplier<T> action)Adapts givenactionto an action guarded by this configured set of fault tolerance strategies.Tcall(Callable<T> action)Calls givenactionand guards the call by this configured set of fault tolerance strategies.static CircuitBreakerMaintenancecircuitBreakerMaintenance()Returns 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>>>createAsync()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.static FaultTolerance.Builder<CompletionStage<Void>,Runnable>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 Tget(Supplier<T> action)Calls givenactionand guards the call by this configured set of fault tolerance strategies.default voidrun(Runnable action)Calls givenactionand guards the call by this configured set of fault tolerance strategies.
-
-
-
Method Detail
-
circuitBreakerMaintenance
static CircuitBreakerMaintenance circuitBreakerMaintenance()
Returns aCircuitBreakerMaintenanceinstance that provides maintenance access to existing circuit breakers.
-
createCallable
static <T> FaultTolerance.Builder<T,Callable<T>> createCallable(Callable<T> action)
Returns a builder that, at the end, returns aCallableguarding the givenaction. Theactionis synchronous and is always executed on the original thread.
-
createSupplier
static <T> FaultTolerance.Builder<T,Supplier<T>> createSupplier(Supplier<T> action)
Returns a builder that, at the end, returns aSupplierguarding the givenaction. Theactionis synchronous and is always executed on the original thread.
-
createRunnable
static FaultTolerance.Builder<Void,Runnable> createRunnable(Runnable action)
Returns a builder that, at the end, returns aRunnableguarding the givenaction. Theactionis synchronous and is always executed on the original thread.
-
create
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. 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
static FaultTolerance.Builder<CompletionStage<Void>,Runnable> createAsyncRunnable(Runnable action)
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
T call(Callable<T> action) throws Exception
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
default T get(Supplier<T> action)
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
default void run(Runnable action)
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
default Callable<T> adaptCallable(Callable<T> action)
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:
call(Callable)
-
adaptSupplier
default Supplier<T> adaptSupplier(Supplier<T> action)
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:
get(Supplier)
-
adaptRunnable
default Runnable adaptRunnable(Runnable action)
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:
run(Runnable)
-
-