This can be achieved by defining an ErrorDecoder
and taking manual control of the Hystrix
Circuit Breaker. You can inspect the response codes from the exceptions and provide your own fallback. In addition, if you wish to retry the request, wrap and throw your exception in a RetryException
.
To meet your Retry requirement, also register a Retryer
bean with the appropriate configuration. Keep in mind that using a Retryer
will tie up a thread for the duration. The default implementation of Retryer
does use an exponential backoff policy as well.
Here is an example ErrorDecoder taken from the OpenFeign
documentation:
public class StashErrorDecoder implements ErrorDecoder {
@Override
public Exception decode(String methodKey, Response response) {
if (response.status() >= 400 && response.status() <= 499) {
return new StashClientException(
response.status(),
response.reason()
);
}
if (response.status() >= 500 && response.status() <= 599) {
return new StashServerException(
response.status(),
response.reason()
);
}
return errorStatus(methodKey, response);
}
}
In your case, you would react to 419
as desired.
You can forcibly open the Circuit Breaker setting this property at runtime
hystrix.command.HystrixCommandKey.circuitBreaker.forceOpen
ConfigurationManager.getConfigInstance()
.setProperty(
"hystrix.command.HystrixCommandKey.circuitBreaker.forceOpen", true);
Replace HystrixCommandKey
with your own command. You will need to restore this circuit breaker back to closed after the desired time.