Implementing retry logic to restTemplate (#17375)

* Implementing retry logic to restTemplate

* Fixing the issue

* Adding import

* Fix

* Fix

* minor update, add tests

* fix

* Adding the maxRetryAttempt, threadWaitTime as additionalProperty

* Updating the apiClient

* Removing reduntant variable

* Generating samples

* Fixing format

---------

Co-authored-by: Rubini <rubini@Rubinis-MacBook-Air.local>
Co-authored-by: William Cheng <wing328hk@gmail.com>
This commit is contained in:
rubiniselvaraj
2023-12-12 19:31:36 +05:30
committed by GitHub
parent 809b3331a9
commit a792a79059
11 changed files with 579 additions and 8 deletions

View File

@@ -22,6 +22,7 @@ import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
@@ -77,6 +78,10 @@ public class ApiClient extends JavaTimeFormatter {
private HttpHeaders defaultHeaders = new HttpHeaders();
private MultiValueMap<String, String> defaultCookies = new LinkedMultiValueMap<String, String>();
private int maxAttemptsForRetry = 1;
private long waitTimeMillis = 10;
private String basePath = "http://localhost";
private RestTemplate restTemplate;
@@ -132,6 +137,46 @@ public class ApiClient extends JavaTimeFormatter {
return this;
}
/**
* Get the max attempts for retry
*
* @return int the max attempts
*/
public int getMaxAttemptsForRetry() {
return maxAttemptsForRetry;
}
/**
* Set the max attempts for retry
*
* @param getMaxAttemptsForRetry the max attempts for retry
* @return ApiClient this client
*/
public ApiClient setMaxAttemptsForRetry(int maxAttemptsForRetry) {
this.maxAttemptsForRetry = maxAttemptsForRetry;
return this;
}
/**
* Get the wait time in milliseconds
*
* @return long wait time in milliseconds
*/
public long getWaitTimeMillis() {
return waitTimeMillis;
}
/**
* Set the wait time in milliseconds
*
* @param waitTimeMillis the wait time in milliseconds
* @return ApiClient this client
*/
public ApiClient setWaitTimeMillis(long waitTimeMillis) {
this.waitTimeMillis = waitTimeMillis;
return this;
}
/**
* Get authentications (key: authentication name, value: authentication).
*
@@ -580,7 +625,23 @@ public class ApiClient extends JavaTimeFormatter {
RequestEntity<Object> requestEntity = requestBuilder.body(selectBody(body, formParams, contentType));
ResponseEntity<T> responseEntity = restTemplate.exchange(requestEntity, returnType);
ResponseEntity<T> responseEntity = null;
int attempts = 0;
while (attempts < maxAttemptsForRetry) {
try {
responseEntity = restTemplate.exchange(requestEntity, returnType);
break;
} catch (HttpServerErrorException ex) {
attempts++;
if (attempts < maxAttemptsForRetry) {
try {
Thread.sleep(waitTimeMillis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
if (responseEntity.getStatusCode().is2xxSuccessful()) {
return responseEntity;