'Spring-Circuit-Breaker-Resilience4j-Nested Failover

I have multiple failover layers. If my getData1() throws any exception, it must fall back to getData2() and if this method too throws exception then it must fall back to getData3(). I tried annotating the fallback method with circuit breaker as below but fail over is not happening when there is an exception in getData2().

public List<MyResponse>  getData1(String id1, String id2){
          
    return service1.getDataById1(id1,id2);
}

@CircuitBreaker(name = "shared", fallbackMethod = "getData3")
public List<MyResponse>  getData2(String id1, String id2,Exception ex){
          
    return service2.getDataById2(id1,id2);
}

public List<MyResponse>  getData3(String id1, String id2){
    
    return service3.getDataById3(id1,id2);
}

I'm trying this nested circuit breaker implementation but could not find any solution.



Solution 1:[1]

The fallback method should accept one more parameter, ex. Throwable e, so that resilience4j will be able to invoked the fallback method and inject the error thrown in the first method.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ruelos Joel