[java][resttemplate] rethrow original exception when retry limits exceeded (#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
This commit is contained in:
Ilamparithi Natarajan 2024-01-05 01:57:24 +00:00 committed by GitHub
parent 23bd5d413c
commit ddc7d4b1b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 144 additions and 64 deletions

View File

@ -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;
@ -772,7 +773,11 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
try {
responseEntity = restTemplate.exchange(requestEntity, returnType);
break;
} catch (HttpServerErrorException ex) {
} catch (HttpServerErrorException | HttpClientErrorException ex) {
if (ex instanceof HttpServerErrorException
|| ((HttpClientErrorException) ex)
.getStatusCode()
.equals(HttpStatus.TOO_MANY_REQUESTS)) {
attempts++;
if (attempts < maxAttemptsForRetry) {
try {
@ -780,12 +785,17 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
} 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()) {

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.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
@ -688,7 +689,11 @@ public class ApiClient extends JavaTimeFormatter {
try {
responseEntity = restTemplate.exchange(requestEntity, returnType);
break;
} catch (HttpServerErrorException ex) {
} catch (HttpServerErrorException | HttpClientErrorException ex) {
if (ex instanceof HttpServerErrorException
|| ((HttpClientErrorException) ex)
.getStatusCode()
.equals(HttpStatus.TOO_MANY_REQUESTS)) {
attempts++;
if (attempts < maxAttemptsForRetry) {
try {
@ -696,12 +701,17 @@ public class ApiClient extends JavaTimeFormatter {
} 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()) {

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.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
@ -631,7 +632,11 @@ public class ApiClient extends JavaTimeFormatter {
try {
responseEntity = restTemplate.exchange(requestEntity, returnType);
break;
} catch (HttpServerErrorException ex) {
} catch (HttpServerErrorException | HttpClientErrorException ex) {
if (ex instanceof HttpServerErrorException
|| ((HttpClientErrorException) ex)
.getStatusCode()
.equals(HttpStatus.TOO_MANY_REQUESTS)) {
attempts++;
if (attempts < maxAttemptsForRetry) {
try {
@ -639,12 +644,17 @@ public class ApiClient extends JavaTimeFormatter {
} 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()) {

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.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
@ -680,7 +681,11 @@ public class ApiClient extends JavaTimeFormatter {
try {
responseEntity = restTemplate.exchange(requestEntity, returnType);
break;
} catch (HttpServerErrorException ex) {
} catch (HttpServerErrorException | HttpClientErrorException ex) {
if (ex instanceof HttpServerErrorException
|| ((HttpClientErrorException) ex)
.getStatusCode()
.equals(HttpStatus.TOO_MANY_REQUESTS)) {
attempts++;
if (attempts < maxAttemptsForRetry) {
try {
@ -688,12 +693,17 @@ public class ApiClient extends JavaTimeFormatter {
} 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()) {

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.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
@ -680,7 +681,11 @@ public class ApiClient extends JavaTimeFormatter {
try {
responseEntity = restTemplate.exchange(requestEntity, returnType);
break;
} catch (HttpServerErrorException ex) {
} catch (HttpServerErrorException | HttpClientErrorException ex) {
if (ex instanceof HttpServerErrorException
|| ((HttpClientErrorException) ex)
.getStatusCode()
.equals(HttpStatus.TOO_MANY_REQUESTS)) {
attempts++;
if (attempts < maxAttemptsForRetry) {
try {
@ -688,12 +693,17 @@ public class ApiClient extends JavaTimeFormatter {
} 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()) {

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.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
@ -680,7 +681,11 @@ public class ApiClient extends JavaTimeFormatter {
try {
responseEntity = restTemplate.exchange(requestEntity, returnType);
break;
} catch (HttpServerErrorException ex) {
} catch (HttpServerErrorException | HttpClientErrorException ex) {
if (ex instanceof HttpServerErrorException
|| ((HttpClientErrorException) ex)
.getStatusCode()
.equals(HttpStatus.TOO_MANY_REQUESTS)) {
attempts++;
if (attempts < maxAttemptsForRetry) {
try {
@ -688,12 +693,17 @@ public class ApiClient extends JavaTimeFormatter {
} 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()) {

View File

@ -27,6 +27,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;
@ -743,7 +744,11 @@ public class ApiClient extends JavaTimeFormatter {
try {
responseEntity = restTemplate.exchange(requestEntity, returnType);
break;
} catch (HttpServerErrorException ex) {
} catch (HttpServerErrorException | HttpClientErrorException ex) {
if (ex instanceof HttpServerErrorException
|| ((HttpClientErrorException) ex)
.getStatusCode()
.equals(HttpStatus.TOO_MANY_REQUESTS)) {
attempts++;
if (attempts < maxAttemptsForRetry) {
try {
@ -751,12 +756,17 @@ public class ApiClient extends JavaTimeFormatter {
} 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()) {

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.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
@ -738,7 +739,11 @@ public class ApiClient extends JavaTimeFormatter {
try {
responseEntity = restTemplate.exchange(requestEntity, returnType);
break;
} catch (HttpServerErrorException ex) {
} catch (HttpServerErrorException | HttpClientErrorException ex) {
if (ex instanceof HttpServerErrorException
|| ((HttpClientErrorException) ex)
.getStatusCode()
.equals(HttpStatus.TOO_MANY_REQUESTS)) {
attempts++;
if (attempts < maxAttemptsForRetry) {
try {
@ -746,12 +751,17 @@ public class ApiClient extends JavaTimeFormatter {
} 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()) {