Retry java RestTemplate HTTP request if host offline
Asked Answered
T

4

36

Hi I'm using the spring RestTemplate for calling a REST API. The API can be very slow or even offline. My application is building the cache by sending thousands of requests one after the other. The responses can be very slow too, because they contains a lot of data.

I have already increased the Timeout to 120 seconds. My problem now it that the API can be offline and I get a org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection from pool exception.

In the case when the API ist offline, the application should wait and try again until the API is online again.

Can I achieve this in RestTemplate out of the box without building exception-loops on my own?

Thanks!

Treble answered 2/9, 2015 at 11:55 Comment(1)
No, you cannot, you have to manually do it. Also, if 120 seconds time is not sufficient to get data, then you must contact the API developer.Khachaturian
E
34

I had same situation and done some googling found the solution. Giving answer in hope it help someone else. You can set max try and time interval for each try.

@Bean
  public RetryTemplate retryTemplate() {

    int maxAttempt = Integer.parseInt(env.getProperty("maxAttempt"));
    int retryTimeInterval = Integer.parseInt(env.getProperty("retryTimeInterval"));

    SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
    retryPolicy.setMaxAttempts(maxAttempt);

    FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
    backOffPolicy.setBackOffPeriod(retryTimeInterval); // 1.5 seconds

    RetryTemplate template = new RetryTemplate();
    template.setRetryPolicy(retryPolicy);
    template.setBackOffPolicy(backOffPolicy);

    return template;
  }

And my rest service that i want to execute is below.

retryTemplate.execute(context -> {
        System.out.println("inside retry method");
        ResponseEntity<?> requestData = RestTemplateProvider.getInstance().postAsNewRequest(bundle, ServiceResponse.class, serivceURL,
                CommonUtils.getHeader("APP_Name"));

        _LOGGER.info("Response ..."+ requestData);
            throw new IllegalStateException("Something went wrong");
        });
Ellyellyn answered 28/4, 2017 at 11:51 Comment(3)
Which library is it?Wyant
It is spring lib. mvnrepository.com/artifact/org.springframework.retry/…Ellyellyn
If you are working with Spring-boot you can just include the starter spring-boot-starter-batch to get that dependency with the version aligned to the spring-boot version.Nabila
F
29

You can also tackle this annotation-driven using Spring Retry. This way you will avoid to implement the template.

Add it to your pom.xml

<dependency>
    <groupId>org.springframework.retry</groupId>
    <artifactId>spring-retry</artifactId>
    <version>1.1.2.RELEASE</version>
</dependency>

Enable it for your application/configuration

@SpringBootApplication
@EnableRetry
public class MyApplication {
  //...
}

Guard methods that are in danger of failure with @Retryable

@Service
public class MyService {

  @Retryable(maxAttempts=5, value = RuntimeException.class, 
             backoff = @Backoff(delay = 15000, multiplier = 2))
  public List<String> doDangerousOperationWithExternalResource() {
     // ...
  }

}
Frivolous answered 12/11, 2018 at 12:51 Comment(0)
B
10

Use Spring Retry project (https://dzone.com/articles/spring-retry-ways-integrate, https://github.com/spring-projects/spring-retry).

It is designed to solve problems like yours.

Back answered 2/9, 2015 at 12:18 Comment(0)
M
0

This approach saved my day.!!!Resilience saved my day.[Resilience retry][1]

       RetryConfig config = RetryConfig.custom()
                .maxAttempts(4)
                .waitDuration(Duration.ofMillis(2000))
                .failAfterMaxAttempts(true)
                .build();
        RetryRegistry registry = RetryRegistry.of(config);


        HttpEntity<String> request =
                new HttpEntity<>(body, headers);
        Retry retry = registry.retry("notification-endpoint-"+System.currentTimeMillis());
        AtomicReference<Integer> retries = new AtomicReference<>(0);
        retry.getEventPublisher().onRetry(e -> {
            log.info("Retrying here!!!. Count: {}", retries.updateAndGet(v -> v + 1));
        }).onError(e->{
            log.error("Failed to get to client.");
        });
        if(requestPojo.getMethod().equalsIgnoreCase("GET")) {
             response = Retry.decorateCheckedSupplier(
                    retry,
                    () -> restTemplateConfig.restTemplate()
                            .exchange(url, HttpMethod.GET, request, String.class)).unchecked().get();
        }
        else if(requestPojo.getMethod().equalsIgnoreCase("POST")) {
            response = Retry.decorateCheckedSupplier(
                    retry,
                    () -> restTemplateConfig.restTemplate()
                            .exchange(url, HttpMethod.POST, request, String.class)).unchecked().get();
        }```


  [1]: https://resilience4j.readme.io/docs/retry
Mattias answered 12/11, 2021 at 3:42 Comment(1)
Please add your import-statements and groupid/artifactid libraries.Polyzoarium

© 2022 - 2024 — McMap. All rights reserved.