Search code examples
javaspringspring-bootspring-cloudhystrix

Can we use Spring-cloud-netflix and Hystrix to retry failed exectuion


I am using Spring-Cloud-netflix library.

I wonder if there is a way to take this code and add configure it instead of executing the fallback method right away to retry to execute it N times and in case of N times than execute the fallback method:

 @HystrixCommand(fallbackMethod = "defaultInvokcation")
    public String getRemoteBro(String name) {
        return(executeRemoteService(name));
    }

     private String defaultInvokcation(String name) {
   return "something";
}

Thanks, ray.


Solution

  • From my comment:

    Handle this behavior in your code. It's not the job of hystrix to know your "special" business logic. As an example

    private final static int MAX_RETRIES = 5;
    
    @HystrixCommand(fallbackMethod = "defaultInvokcation")
    public String getRemoteBro(String name) {
        return(executeRemoteService(name));
    }
    
    private String executeRemoteService(String serviceName) {
        for (int i = 0; i < MAX_RETRIES; i++) {
            try {
                return reallyExecuteRemoteService(serviceName);
            } catch (ServiceException se) { 
              // handle or log execption
            }
        }
        throw new RuntimeException("bam");
    }
    

    Don't know if you prefer to use an exception inside the loop ;) You could also wrap your answer from reallyExecuteRemoteServicein some kind of ServiceReturnMessage with a status code.