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

  • import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
  • import org.springframework.beans.factory.annotation.Autowired;
  • import org.springframework.cloud.client.ServiceInstance;
  • import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; 
  • import org.springframework.stereotype.Service;
  • import org.springframework.web.client.RestTemplate; 
  • import java.net.URI;

@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.



Discussion

No Comment Found