Skip to content

Commit

Permalink
[java][resttemplate] rethrow original exception when retry limits exc…
Browse files Browse the repository at this point in the history
…eeded (#17488)

in rest template, when the retry limits exceeded
rethrow the original exception

also add 429 (Too many requests) status code to the
retry logic

fix #17478
  • Loading branch information
ilam-natarajan authored Jan 5, 2024
1 parent 23bd5d4 commit ddc7d4b
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,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.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
Expand Down Expand Up @@ -772,20 +773,29 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
try {
responseEntity = restTemplate.exchange(requestEntity, returnType);
break;
} catch (HttpServerErrorException ex) {
attempts++;
if (attempts < maxAttemptsForRetry) {
try {
Thread.sleep(waitTimeMillis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (HttpServerErrorException | HttpClientErrorException ex) {
if (ex instanceof HttpServerErrorException
|| ((HttpClientErrorException) ex)
.getStatusCode()
.equals(HttpStatus.TOO_MANY_REQUESTS)) {
attempts++;
if (attempts < maxAttemptsForRetry) {
try {
Thread.sleep(waitTimeMillis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
} else {
throw ex;
}
} else {
throw ex;
}
}
}
if (responseEntity == null) {
throw new RestClientException("API returned HttpServerErrorException");
throw new RestClientException("ResponseEntity is null");
}
if (responseEntity.getStatusCode().is2xxSuccessful()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
Expand Down Expand Up @@ -688,20 +689,29 @@ public <T> ResponseEntity<T> invokeAPI(String path, HttpMethod method, Map<Strin
try {
responseEntity = restTemplate.exchange(requestEntity, returnType);
break;
} catch (HttpServerErrorException ex) {
attempts++;
if (attempts < maxAttemptsForRetry) {
try {
Thread.sleep(waitTimeMillis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (HttpServerErrorException | HttpClientErrorException ex) {
if (ex instanceof HttpServerErrorException
|| ((HttpClientErrorException) ex)
.getStatusCode()
.equals(HttpStatus.TOO_MANY_REQUESTS)) {
attempts++;
if (attempts < maxAttemptsForRetry) {
try {
Thread.sleep(waitTimeMillis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
} else {
throw ex;
}
} else {
throw ex;
}
}
}

if (responseEntity == null) {
throw new RestClientException("API returned HttpServerErrorException");
throw new RestClientException("ResponseEntity is null");
}

if (responseEntity.getStatusCode().is2xxSuccessful()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
Expand Down Expand Up @@ -631,20 +632,29 @@ public <T> ResponseEntity<T> invokeAPI(String path, HttpMethod method, Map<Strin
try {
responseEntity = restTemplate.exchange(requestEntity, returnType);
break;
} catch (HttpServerErrorException ex) {
attempts++;
if (attempts < maxAttemptsForRetry) {
try {
Thread.sleep(waitTimeMillis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (HttpServerErrorException | HttpClientErrorException ex) {
if (ex instanceof HttpServerErrorException
|| ((HttpClientErrorException) ex)
.getStatusCode()
.equals(HttpStatus.TOO_MANY_REQUESTS)) {
attempts++;
if (attempts < maxAttemptsForRetry) {
try {
Thread.sleep(waitTimeMillis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
} else {
throw ex;
}
} else {
throw ex;
}
}
}

if (responseEntity == null) {
throw new RestClientException("API returned HttpServerErrorException");
throw new RestClientException("ResponseEntity is null");
}

if (responseEntity.getStatusCode().is2xxSuccessful()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
Expand Down Expand Up @@ -680,20 +681,29 @@ public <T> ResponseEntity<T> invokeAPI(String path, HttpMethod method, Map<Strin
try {
responseEntity = restTemplate.exchange(requestEntity, returnType);
break;
} catch (HttpServerErrorException ex) {
attempts++;
if (attempts < maxAttemptsForRetry) {
try {
Thread.sleep(waitTimeMillis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (HttpServerErrorException | HttpClientErrorException ex) {
if (ex instanceof HttpServerErrorException
|| ((HttpClientErrorException) ex)
.getStatusCode()
.equals(HttpStatus.TOO_MANY_REQUESTS)) {
attempts++;
if (attempts < maxAttemptsForRetry) {
try {
Thread.sleep(waitTimeMillis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
} else {
throw ex;
}
} else {
throw ex;
}
}
}

if (responseEntity == null) {
throw new RestClientException("API returned HttpServerErrorException");
throw new RestClientException("ResponseEntity is null");
}

if (responseEntity.getStatusCode().is2xxSuccessful()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
Expand Down Expand Up @@ -680,20 +681,29 @@ public <T> ResponseEntity<T> invokeAPI(String path, HttpMethod method, Map<Strin
try {
responseEntity = restTemplate.exchange(requestEntity, returnType);
break;
} catch (HttpServerErrorException ex) {
attempts++;
if (attempts < maxAttemptsForRetry) {
try {
Thread.sleep(waitTimeMillis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (HttpServerErrorException | HttpClientErrorException ex) {
if (ex instanceof HttpServerErrorException
|| ((HttpClientErrorException) ex)
.getStatusCode()
.equals(HttpStatus.TOO_MANY_REQUESTS)) {
attempts++;
if (attempts < maxAttemptsForRetry) {
try {
Thread.sleep(waitTimeMillis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
} else {
throw ex;
}
} else {
throw ex;
}
}
}

if (responseEntity == null) {
throw new RestClientException("API returned HttpServerErrorException");
throw new RestClientException("ResponseEntity is null");
}

if (responseEntity.getStatusCode().is2xxSuccessful()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
Expand Down Expand Up @@ -680,20 +681,29 @@ public <T> ResponseEntity<T> invokeAPI(String path, HttpMethod method, Map<Strin
try {
responseEntity = restTemplate.exchange(requestEntity, returnType);
break;
} catch (HttpServerErrorException ex) {
attempts++;
if (attempts < maxAttemptsForRetry) {
try {
Thread.sleep(waitTimeMillis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (HttpServerErrorException | HttpClientErrorException ex) {
if (ex instanceof HttpServerErrorException
|| ((HttpClientErrorException) ex)
.getStatusCode()
.equals(HttpStatus.TOO_MANY_REQUESTS)) {
attempts++;
if (attempts < maxAttemptsForRetry) {
try {
Thread.sleep(waitTimeMillis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
} else {
throw ex;
}
} else {
throw ex;
}
}
}

if (responseEntity == null) {
throw new RestClientException("API returned HttpServerErrorException");
throw new RestClientException("ResponseEntity is null");
}

if (responseEntity.getStatusCode().is2xxSuccessful()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
Expand Down Expand Up @@ -743,20 +744,29 @@ public <T> ResponseEntity<T> invokeAPI(String path, HttpMethod method, Map<Strin
try {
responseEntity = restTemplate.exchange(requestEntity, returnType);
break;
} catch (HttpServerErrorException ex) {
attempts++;
if (attempts < maxAttemptsForRetry) {
try {
Thread.sleep(waitTimeMillis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (HttpServerErrorException | HttpClientErrorException ex) {
if (ex instanceof HttpServerErrorException
|| ((HttpClientErrorException) ex)
.getStatusCode()
.equals(HttpStatus.TOO_MANY_REQUESTS)) {
attempts++;
if (attempts < maxAttemptsForRetry) {
try {
Thread.sleep(waitTimeMillis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
} else {
throw ex;
}
} else {
throw ex;
}
}
}

if (responseEntity == null) {
throw new RestClientException("API returned HttpServerErrorException");
throw new RestClientException("ResponseEntity is null");
}

if (responseEntity.getStatusCode().is2xxSuccessful()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
Expand Down Expand Up @@ -738,20 +739,29 @@ public <T> ResponseEntity<T> invokeAPI(String path, HttpMethod method, Map<Strin
try {
responseEntity = restTemplate.exchange(requestEntity, returnType);
break;
} catch (HttpServerErrorException ex) {
attempts++;
if (attempts < maxAttemptsForRetry) {
try {
Thread.sleep(waitTimeMillis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (HttpServerErrorException | HttpClientErrorException ex) {
if (ex instanceof HttpServerErrorException
|| ((HttpClientErrorException) ex)
.getStatusCode()
.equals(HttpStatus.TOO_MANY_REQUESTS)) {
attempts++;
if (attempts < maxAttemptsForRetry) {
try {
Thread.sleep(waitTimeMillis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
} else {
throw ex;
}
} else {
throw ex;
}
}
}

if (responseEntity == null) {
throw new RestClientException("API returned HttpServerErrorException");
throw new RestClientException("ResponseEntity is null");
}

if (responseEntity.getStatusCode().is2xxSuccessful()) {
Expand Down

0 comments on commit ddc7d4b

Please sign in to comment.