-1

I have a spring-boot application which which needs to call external APIs. If there are 'n' external calls, 'n' future tasks will be created and rest calls will be made. Problem here is if any of the rest call fails with exception, i need to retry the call for 3 times. I tried using Spring- retry but it doesn,t retry on failure.

Following is the code snippet i tried so far with retry. I straight away receive IO exception when any service being called is down and the program does not retry or go into recover block. Is it because proxies are not being created for new threads.

@SpringBootApplication
@EnableRetry
public class SpringBootWebApplication implements CommandLineRunner{

    @Autowired
    RestClient client;

    public static void main(String[] args) {
       SpringApplication.run(SpringBootWebApplication.class, args);
    }

      @Override
    public void run(String... args) throws Exception {

       String a[] = new String[] {args[0],args[1],args[2] }; 

            // getting the list view of Array 
            List<String> list = Arrays.asList(a); 

        client.executeRest(list)
    }

}

######

I have another class where future tasks will be created.

public class RestClient(){
     public void executeRest(List<String> uris)){

       ExecutorService executor = Executors.newFixedThreadPool(2);
       for(String uri : uris){
           Future<String> t = executor.execute(new MyCallable(uri));
        }
     }
}


public class MyCallable implements Callable<String> {

    private String endPoint;

    public MyCallable(String endPoint){
        this.endPoint=endPoint;
    }

    @Override
    public void call() throws Exception {     
         system.out.println(makeRestGetCall(String uri));
    }

     @Retryable(
      value = { Excelption.class }, 
      maxAttempts = 2,
      backoff = @Backoff(delay = 5000))
        public String makeRestGetCall(String uri){
           return restTemplate.exchange(uri, HttpMethod.GET,String.class);
        }

      @Recover
    void recover(Exception e){
      system.out.println("Max retries done")
     }

}

1
  • is this spelling mistakes Excelption.class?
    – Ryuzaki L
    Commented Apr 7, 2019 at 15:19

1 Answer 1

0

Retry will be attempted only if the method throws an Exception that configured in value ( in your case it is Exception) , restTemplate.exchange method throws multiple Exceptions so try using custom Exception

public class CustomException extends RuntimeException {

    }

Retry Method

 @Retryable(
  value = { CustomException.class }, 
  maxAttempts = 2,
  backoff = @Backoff(delay = 5000))
    public String makeRestGetCall(String uri){
         try {
       return restTemplate.exchange(uri, HttpMethod.GET,String.class);
          }catch(Exception ex) {

           // do something or log something 
             throw new CustomException();
           }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.