Search code examples
javaspring-bootresilience4j

Resilience4J - global fallback method


Question regarding Resilience4j combined with SpringBoot starter.

In the doc here: https://resilience4j.readme.io/docs/getting-started-3 It mentions: "You can define one global fallback method with an exception parameter only if multiple methods has the same return type and you want to define the same fallback method for them once and for all."

Anyone has an example please?

I had a look at the demo https://github.com/resilience4j/resilience4j-spring-boot2-demo and could see duplicated methods

 private Flux<String> fluxFallback(Exception ex) {
        return Flux.just("Recovered: " + ex.toString());
    }

I am having a same case, I have some twelve classes that all share one same fallback method. A default list of something.

Looking at the demo, I am going to copy paste this default list fallback twelve times. I am sure there is a smarter way to do this. Just that I could not find it.

Anyone has a snippet please?

Thank you


Solution

  • A global fallback method is only possible for a single class. Currently there is no way to define a global fallback method which can be used by multiple classes.

    You can only extra the fallback logic into a single class so that you don't have to copy&paste the fallback logic multiple times.