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, rate limit, 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 is 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.
    • Method Detail

      • createCallable

        static <T> FaultTolerance.Builder<T,​Callable<T>> createCallable​(Callable<T> action)
        Returns a builder that, at the end, returns a Callable guarding the given action. The action is 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 a Supplier guarding the given action. The action is 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 a Runnable guarding the given action. The action is synchronous and is always executed on the original thread.
      • call

        T call​(Callable<T> action)
        throws Exception
        Calls given action and guards the call by this configured set of fault tolerance strategies. If this FaultTolerance instance was created using create(), the action is synchronous and is always executed on the same thread that calls this method. If this FaultTolerance instance was created using createAsync(), 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 given action and guards the call by this configured set of fault tolerance strategies. If this FaultTolerance instance was created using create*, the action is synchronous and is always executed on the same thread that calls this method. If this FaultTolerance instance was created using createAsync*, 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 given action and guards the call by this configured set of fault tolerance strategies. If this FaultTolerance instance was created using create(), the action is synchronous and is always executed on the same thread that calls this method. If this FaultTolerance instance was created using createAsync(), 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 given action 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:
        call(Callable)
      • adaptSupplier

        default Supplier<T> adaptSupplier​(Supplier<T> action)
        Adapts given action 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:
        get(Supplier)
      • adaptRunnable

        default Runnable adaptRunnable​(Runnable action)
        Adapts given action 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:
        run(Runnable)