3

I want to use a fallback in case of failing, so the behavior in "open" state should be to call a fallback instead of throwing an exception.

Problem is that the fallback is called during the "closed" state, while for "open" state I still get the exception. Is this the intended behavior? No way to attain what I am searching for?

I have defined my custom circuit break:

resilience4j:
  circuitbreaker:
 configs:
  default:
    register-health-indicator: true
    slidingWindowSize: 10
    minimumNumberOfCalls: 2
    permittedNumberOfCallsInHalfOpenState: 2
    automaticTransitionFromOpenToHalfOpenEnabled: true
    waitDurationInOpenState: 20s
    failureRateThreshold: 20
    slowCallDurationThreshold: 2s
    slowCallRateThreshold: 20
instances:
  backendA:
    base-config: default

Now, I defined my method as follows:

public class ExampleService {


@CircuitBreaker(name = "default", fallbackMethod = "fall")
public List<String> doSomething(Long id) {
    return Arrays.asList("a", "b", "c");

}

private List<String> fall(Long id, Exception ex) {
    return Arrays.asList("faaaallingggg");
}
 }
Phate
  • 6,066
  • 15
  • 73
  • 138

1 Answers1

4

Just change your signature of the fallback method to

private List<String> fall(Long id, CallNotPermittedException ex) {
    return Arrays.asList("faaaallingggg");
}
Robert Winkler
  • 1,734
  • 9
  • 8
  • So Exception does not catch them all? – Phate Mar 13 '20 at 14:07
  • 2
    It's like a try/catch block. If you narrow down the scope it catches less. If you catch Exception, it catches everything and not only CallNotPermittedException. – Robert Winkler Mar 18 '20 at 08:27
  • Ok, but in my case I would have expected to catch also the CallNotPermitted as it extends exception – Phate Mar 18 '20 at 08:49
  • @Phate, right. And your circuit will become `open`, while you thought it's still `close`. Please accept Robert Winkler's answer, as it is correct. Thanks. – jumping_monkey Mar 06 '22 at 02:07