InterviewSolution
| 1. |
How will you ignore certain exceptions in Hystrix fallback execution? |
|
Answer» @HystrixCommand annotation provides attribute ignoreExceptions that can be USED to PROVIDE a list of ignored exceptions. Code
@Service public class HystrixService {@Autowired private LoadBalancerClient loadBalancer;@Autowired private RestTemplate restTemplate;@HystrixCommand(fallbackMethod = "reliable", ignoreExceptions = IllegalStateException.class, MissingServletRequestParameterException.class, TypeMismatchException.class) public String readingList() {ServiceInstance instance = loadBalancer.choose("product-service"); URI uri = URI.create("HTTP://product-service/product/recommended"); return this.restTemplate.getForObject(uri, String.class);} public String reliable(Throwable e) { return "Cloud Native Java (O'Reilly)";In the above example, if the ACTUAL method call throws IllegalStateException, MissingServletRequestParameterException or TypeMismatchException then hystrix will not trigger the fallback logic (reliable method), instead the actual EXCEPTION will be wrapped inside HystrixBadRequestException and re-thrown to the caller. It is taken care by javanica library under the hood. |
|