Spring Boot - fallback not registering
See original GitHub issueI’m running into a problem with circuit breakers and fallbacks in my Spring Boot project. I have the following method.
@CircuitBreaker(name = "authorize", fallbackMethod = "fallback")
public TokenData authorize(MultiValueMap<String, String> body) {
String uri = UriComponentsBuilder.fromUriString(vendorData.getAuthenticationUri())
.build()
.encode()
.toString();
OAuth2AuthorizationResponse response = restTemplate
.postForEntity(uri, authorizationRequest, OAuth2AuthorizationResponse.class)
.getBody();
return buildToken(response);
}
private TokenData fallback(MultiValueMap<String, String> body) {
return null;
}
This is working as expected. If postForEntity() throws an exception, the fallback is invoked and the method returns null. If the fallback’s signature is changed to something invalid, a NoSuchMethodFoundException will be thrown by resilience4j.
However, if I separate the network call from the authorize method like so:
public TokenData authorize(MultiValueMap<String, String> body) {
String uri = UriComponentsBuilder.fromUriString(vendorData.getAuthenticationUri())
.build()
.encode()
.toString();
OAuth2AuthorizationResponse response = doAuthorizationRequest(uri, new HttpEntity<>(body, getHeaders()));
return buildToken(response);
}
@CircuitBreaker(name = "authorize", fallbackMethod = "fallback")
public OAuth2AuthorizationResponse doAuthorizationRequest(String uri,
HttpEntity<MultiValueMap<String, String>> authorizationRequest) {
return restTemplate
.postForEntity(uri, authorizationRequest, OAuth2AuthorizationResponse.class)
.getBody();
}
private OAuth2AuthorizationResponse fallback(String uri,
HttpEntity<MultiValueMap<String, String>> authorizationRequest,
Throwable t) {
return null;
}
The fallback is neither triggered, nor even registered in the first place. The exception thrown by postForEntity will not trigger the fallback in this case, and will be thrown as if the annotation wasn’t even there in the first place. Even if I intentionally give the fallback an invalid signature, I won’t get an exception. Am I doing something wrong? Are circuit breakers not supported for nested methods?
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (4 by maintainers)
Top GitHub Comments
@RobWin Thanks, that was both informative and helpful. Now I know how to achieve what I set out to do.
@bentein for fallback you do not need to have it public , we check if it is private we make it accessible for invocation then we set back to original access level after that through reflection ,
for fallback to be invoked , you need to add just ONE extra target exception parameter to the fallback method to make it work , it is not optional , so the fallback method should has the same original method signature plus the original method params and one extra exception param at the end :
So should works in ur case
if you want to have global fallback in case u have many methods that has the same return type , you can define one global fallback with JUST one parameter which the exception type
https://resilience4j.readme.io/docs/getting-started-3 here you can see the explanation of fallback logic
Last question are sure ur first version works ? can u debug and make sure it is triggered from ur fallback method ?