Package dev.failsafe

Class Failsafe


  • public class Failsafe
    extends java.lang.Object
    Simple, sophisticated failure handling.
    • Constructor Summary

      Constructors 
      Constructor Description
      Failsafe()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <R> FailsafeExecutor<R> none()
      Creates and returns a noop FailsafeExecutor instance that treats any exception as a failure for the purposes of calling event listeners, and provides no additional failure handling.
      static <R> FailsafeExecutor<R> with​(java.util.List<? extends Policy<R>> policies)
      Creates and returns a new FailsafeExecutor instance that will handle failures according to the given policies.
      static <R,​P extends Policy<R>>
      FailsafeExecutor<R>
      with​(P outerPolicy, P... policies)
      Creates and returns a new FailsafeExecutor instance that will handle failures according to the given outerPolicy and policies.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Failsafe

        public Failsafe()
    • Method Detail

      • with

        @SafeVarargs
        public static <R,​P extends Policy<R>> FailsafeExecutor<R> with​(P outerPolicy,
                                                                             P... policies)
        Creates and returns a new FailsafeExecutor instance that will handle failures according to the given outerPolicy and policies. The policies are composed around an execution and will handle execution results in reverse, with the last policy being applied first. For example, consider:

           Failsafe.with(fallback, retryPolicy, circuitBreaker).get(supplier);
         

        This is equivalent to composition using the the compose method:

           Failsafe.with(fallback).compose(retryPolicy).compose(circuitBreaker).get(supplier);
         

        These result in the following internal composition when executing a runnable or supplier and handling its result:

           Fallback(RetryPolicy(CircuitBreaker(Supplier)))
         

        This means the CircuitBreaker is first to evaluate the Supplier's result, then the RetryPolicy, then the Fallback. Each policy makes its own determination as to whether the result represents a failure. This allows different policies to be used for handling different types of failures.
        Type Parameters:
        R - result type
        P - policy type
        Throws:
        java.lang.NullPointerException - if outerPolicy is null
      • with

        public static <R> FailsafeExecutor<R> with​(java.util.List<? extends Policy<R>> policies)
        Creates and returns a new FailsafeExecutor instance that will handle failures according to the given policies. The policies are composed around an execution and will handle execution results in reverse, with the last policy being applied first. For example, consider:

           Failsafe.with(Arrays.asList(fallback, retryPolicy, circuitBreaker)).get(supplier);
         

        This results in the following internal composition when executing a runnable or supplier and handling its result:

           Fallback(RetryPolicy(CircuitBreaker(Supplier)))
         

        This means the CircuitBreaker is first to evaluate the Supplier's result, then the RetryPolicy, then the Fallback. Each policy makes its own determination as to whether the result represents a failure. This allows different policies to be used for handling different types of failures.
        Type Parameters:
        R - result type
        Throws:
        java.lang.NullPointerException - if policies is null
        java.lang.IllegalArgumentException - if policies is empty
      • none

        public static <R> FailsafeExecutor<R> none()
        Creates and returns a noop FailsafeExecutor instance that treats any exception as a failure for the purposes of calling event listeners, and provides no additional failure handling.
        Type Parameters:
        R - result type
        Throws:
        java.lang.NullPointerException - if policies is null
        java.lang.IllegalArgumentException - if policies is empty