Class FallbackMethod

java.lang.Object
io.github.resilience4j.fallback.FallbackMethod

public class FallbackMethod extends Object
Reflection utility for invoking a fallback method. Fallback method should have same return type and parameter types of original method but the last additional parameter. The last additional parameter should be a subclass of Throwable. When fallback(Throwable) is invoked, Throwable will be passed to that last parameter. If there are multiple fallback method, one of the methods that has most closest superclass parameter of thrown object will be invoked.
 For example, there are two fallback methods
 
 String fallbackMethod(String parameter, RuntimeException exception)
 String fallbackMethod(String parameter, IllegalArgumentException exception)
 
 and if try to fallback from NumberFormatException, String fallbackMethod(String parameter, IllegalArgumentException exception) will be invoked.
 
  • Method Details

    • create

      public static FallbackMethod create(String fallbackMethodName, Method originalMethod, Object[] args, Object target) throws NoSuchMethodException
      Parameters:
      fallbackMethodName - the configured fallback method name
      originalMethod - the original method which has fallback method configured
      args - the original method arguments
      target - the target class that own the original method and fallback method
      Returns:
      FallbackMethod instance
      Throws:
      NoSuchMethodException
    • fallback

      @Nullable public Object fallback(Throwable thrown) throws Throwable
      try to fallback from Throwable
      Parameters:
      thrown - Throwable that should be fallback
      Returns:
      fallback value
      Throws:
      Throwable - if throwable is unrecoverable, throwable will be thrown
    • getReturnType

      public Class<?> getReturnType()
      get return type of fallbackMethod method
      Returns:
      return type of fallbackMethod method