We were using the Hystrix functionality by directly extending the HystrixCommand class. But for some of the business exceptions, Hystrix's fallback method is being triggered.
I don't want to trigger the Hystrix fallback for some of the business specific exceptions. How I can achieve it without annotation based?
Simply add a Throwable parameter to the fallback method and it will receive the exception which the original command produced.
We'll implement Hystrix fallback as a static inner class annotated with @Component. Alternatively, we could define a @Bean annotated method returning an instance of this fallback class. For more on using Spring Netflix Eureka for service discovery have a look at this article.
If the fallback method throws ValidationException then the original ConnectException is thrown back to client. This behaviour is undesired because its just a ValidationException and ideally we should be sending a message to client saying there is some validation exception.
The @HystrixCommand is provided by a Netflix contrib library called “javanica”. Spring Cloud automatically wraps Spring beans with that annotation in a proxy that is connected to the Hystrix circuit breaker. The circuit breaker calculates when to open and close the circuit and what to do in case of a failure.
Use the ignoreExceptions annotation param
@HystrixCommand(ignoreExceptions = { BaseException.class, MissingServletRequestParameterException.class, TypeMismatchException.class })
See https://github.com/Netflix/Hystrix/tree/master/hystrix-contrib/hystrix-javanica#error-propagation
I see you are extending the HystrixCommand instead of using the annotation, but that doesn't matter, just set that property in the command and it should have the same effect.
Unfortunately, a Hystrix Command is created by a Builder pattern, so you will have to do some hacking. The ignoreExceptions was added to DefaultProperties.java, which is used in the HystrixCommandBuilder
If you wrap your logic in a try/catch and re-throw any exceptions in a HystrixBadRequestException then it will not trigger the fallback.
@Override
protected Object run() throws Exception {
try {
return //call goes here
}
catch (Throwable e) {
//We wrap any exceptions in a HystrixBadRequestException because this way any other errors will not
//trip the short circuit
throw new HystrixBadRequestException("Exception thrown hystrix call", e);
}
}
From the docs: http://netflix.github.io/Hystrix/javadoc/com/netflix/hystrix/exception/HystrixBadRequestException.html
An exception representing an error with provided arguments or state rather than an execution failure. Unlike all other exceptions thrown by a HystrixCommand this will not trigger fallback, not count against failure metrics and thus not trigger the circuit breaker.
NOTE: This should only be used when an error is due to user input such as IllegalArgumentException otherwise it defeats the purpose of fault-tolerance and fallback behavior.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With