From a792a79059441c6b75db1d26b849cb93a5d4c465 Mon Sep 17 00:00:00 2001 From: rubiniselvaraj Date: Tue, 12 Dec 2023 19:31:36 +0530 Subject: [PATCH] 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 Co-authored-by: William Cheng --- .../codegen/CodegenConstants.java | 4 ++ .../codegen/languages/JavaClientCodegen.java | 25 ++++++++ .../libraries/resttemplate/ApiClient.mustache | 63 ++++++++++++++++++- .../org/openapitools/client/ApiClient.java | 63 ++++++++++++++++++- .../org/openapitools/client/CustomTest.java | 54 ++++++++++++++++ .../org/openapitools/client/ApiClient.java | 63 ++++++++++++++++++- .../org/openapitools/client/ApiClient.java | 63 ++++++++++++++++++- .../org/openapitools/client/ApiClient.java | 63 ++++++++++++++++++- .../org/openapitools/client/ApiClient.java | 63 ++++++++++++++++++- .../org/openapitools/client/ApiClient.java | 63 ++++++++++++++++++- .../org/openapitools/client/ApiClient.java | 63 ++++++++++++++++++- 11 files changed, 579 insertions(+), 8 deletions(-) create mode 100644 samples/client/echo_api/java/resttemplate/src/test/java/org/openapitools/client/CustomTest.java diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java index d9a09c03792..545fea104f4 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java @@ -438,4 +438,8 @@ public class CodegenConstants { public static final String GENERATE_MARSHAL_JSON = "generateMarshalJSON"; public static final String GENERATE_MARSHAL_JSON_DESC = "Generate MarshalJSON method"; + + public static final String MAX_ATTEMPTS_FOR_RETRY = "maxAttemptsForRetry"; + + public static final String WAIT_TIME_OF_THREAD = "waitTimeMillis"; } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java index 36d1646b18a..8dfa3cbee3e 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java @@ -139,6 +139,9 @@ public class JavaClientCodegen extends AbstractJavaCodegen protected boolean generateClientAsBean = false; protected boolean useEnumCaseInsensitive = false; + protected int maxAttemptsForRetry = 1; + protected long waitTimeMillis = 10l; + private static class MpRestClientVersion { public final String rootPackage; public final String pomTemplate; @@ -465,6 +468,20 @@ public class JavaClientCodegen extends AbstractJavaCodegen if (additionalProperties.containsKey(USE_ENUM_CASE_INSENSITIVE)) { this.setUseEnumCaseInsensitive(Boolean.parseBoolean(additionalProperties.get(USE_ENUM_CASE_INSENSITIVE).toString())); } + + if (additionalProperties.containsKey(CodegenConstants.MAX_ATTEMPTS_FOR_RETRY)) { + this.setMaxAttemptsForRetry(Integer.parseInt(additionalProperties.get(CodegenConstants.MAX_ATTEMPTS_FOR_RETRY).toString())); + } + else { + additionalProperties.put(CodegenConstants.MAX_ATTEMPTS_FOR_RETRY, maxAttemptsForRetry); + } + + if (additionalProperties.containsKey(CodegenConstants.WAIT_TIME_OF_THREAD)) { + this.setWaitTimeMillis(Long.parseLong((additionalProperties.get(CodegenConstants.WAIT_TIME_OF_THREAD).toString()))); + } + else { + additionalProperties.put(CodegenConstants.WAIT_TIME_OF_THREAD, waitTimeMillis); + } writePropertyBack(USE_ENUM_CASE_INSENSITIVE, useEnumCaseInsensitive); final String invokerFolder = (sourceFolder + '/' + invokerPackage).replace(".", "/"); @@ -1240,6 +1257,14 @@ public class JavaClientCodegen extends AbstractJavaCodegen this.useEnumCaseInsensitive = useEnumCaseInsensitive; } + public void setMaxAttemptsForRetry(int maxAttemptsForRetry) { + this.maxAttemptsForRetry= maxAttemptsForRetry; + } + + public void setWaitTimeMillis(long waitTimeMillis) { + this.waitTimeMillis= waitTimeMillis; + } + /** * Serialization library. * diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/ApiClient.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/ApiClient.mustache index 383a4904c8e..f641d8e16d9 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/ApiClient.mustache @@ -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.HttpServerErrorException; import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; import org.springframework.web.util.UriComponentsBuilder; @@ -105,6 +106,10 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { private HttpHeaders defaultHeaders = new HttpHeaders(); private MultiValueMap defaultCookies = new LinkedMultiValueMap(); + private int maxAttemptsForRetry = {{maxAttemptsForRetry}}; + + private long waitTimeMillis = {{waitTimeMillis}}; + private String basePath = "{{basePath}}"; private RestTemplate restTemplate; @@ -167,6 +172,46 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { 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). * @@ -721,7 +766,23 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { RequestEntity requestEntity = requestBuilder.body(selectBody(body, formParams, contentType)); - ResponseEntity responseEntity = restTemplate.exchange(requestEntity, returnType); + ResponseEntity 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; diff --git a/samples/client/echo_api/java/resttemplate/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/echo_api/java/resttemplate/src/main/java/org/openapitools/client/ApiClient.java index 220e61461c7..cd4196b5c21 100644 --- a/samples/client/echo_api/java/resttemplate/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/echo_api/java/resttemplate/src/main/java/org/openapitools/client/ApiClient.java @@ -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; @@ -79,6 +80,10 @@ public class ApiClient extends JavaTimeFormatter { private HttpHeaders defaultHeaders = new HttpHeaders(); private MultiValueMap defaultCookies = new LinkedMultiValueMap(); + private int maxAttemptsForRetry = 1; + + private long waitTimeMillis = 10; + private String basePath = "http://localhost:3000"; private RestTemplate restTemplate; @@ -136,6 +141,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). * @@ -637,7 +682,23 @@ public class ApiClient extends JavaTimeFormatter { RequestEntity requestEntity = requestBuilder.body(selectBody(body, formParams, contentType)); - ResponseEntity responseEntity = restTemplate.exchange(requestEntity, returnType); + ResponseEntity 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; diff --git a/samples/client/echo_api/java/resttemplate/src/test/java/org/openapitools/client/CustomTest.java b/samples/client/echo_api/java/resttemplate/src/test/java/org/openapitools/client/CustomTest.java new file mode 100644 index 00000000000..0b3827baf4f --- /dev/null +++ b/samples/client/echo_api/java/resttemplate/src/test/java/org/openapitools/client/CustomTest.java @@ -0,0 +1,54 @@ +/* + * Echo Server API + * Echo Server API + * + * The version of the OpenAPI document: 0.1.0 + * Contact: team@openapitools.oprg + * + * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). + * https://openapi-generator.tech + * Do not edit the class manually. + */ + + +package org.openapitools.client; + +import org.junit.Assert; +import org.openapitools.client.api.*; +import org.openapitools.client.model.*; +import org.junit.Test; +import org.junit.Ignore; + +import java.io.IOException; +import java.util.*; + + +/** + * API tests + */ +public class CustomTest { + + private final QueryApi api = new QueryApi(); + private final BodyApi bodyApi = new BodyApi(); + + /** + * Test body parameter(s) + *

+ * Test body parameter(s) + * + */ + @Test + public void testEchoBodyPet() { + Pet pet = new Pet().id(12345L).name("Hello World"). + photoUrls(Arrays.asList(new String[]{"http://a.com", "http://b.com"})).category(new Category().id(987L).name("new category")); + + Pet p = bodyApi.testEchoBodyPet(pet); + Assert.assertNotNull(p); + Assert.assertEquals("Hello World", p.getName()); + Assert.assertEquals(Long.valueOf(12345L), p.getId()); + + // response is empty body + Pet p2 = bodyApi.testEchoBodyPet(null); + Assert.assertNull(p2); + } +} diff --git a/samples/client/others/java/resttemplate-useAbstractionForFiles/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/others/java/resttemplate-useAbstractionForFiles/src/main/java/org/openapitools/client/ApiClient.java index 6fb215e298d..14e600e6446 100644 --- a/samples/client/others/java/resttemplate-useAbstractionForFiles/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/others/java/resttemplate-useAbstractionForFiles/src/main/java/org/openapitools/client/ApiClient.java @@ -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 defaultCookies = new LinkedMultiValueMap(); + 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 requestEntity = requestBuilder.body(selectBody(body, formParams, contentType)); - ResponseEntity responseEntity = restTemplate.exchange(requestEntity, returnType); + ResponseEntity 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; diff --git a/samples/client/petstore/java/resttemplate-jakarta/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/resttemplate-jakarta/src/main/java/org/openapitools/client/ApiClient.java index a58613e725a..6d065b370a7 100644 --- a/samples/client/petstore/java/resttemplate-jakarta/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/resttemplate-jakarta/src/main/java/org/openapitools/client/ApiClient.java @@ -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; @@ -79,6 +80,10 @@ public class ApiClient extends JavaTimeFormatter { private HttpHeaders defaultHeaders = new HttpHeaders(); private MultiValueMap defaultCookies = new LinkedMultiValueMap(); + private int maxAttemptsForRetry = 1; + + private long waitTimeMillis = 10; + private String basePath = "http://petstore.swagger.io/v2"; private RestTemplate restTemplate; @@ -136,6 +141,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). * @@ -629,7 +674,23 @@ public class ApiClient extends JavaTimeFormatter { RequestEntity requestEntity = requestBuilder.body(selectBody(body, formParams, contentType)); - ResponseEntity responseEntity = restTemplate.exchange(requestEntity, returnType); + ResponseEntity 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; diff --git a/samples/client/petstore/java/resttemplate-swagger1/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/resttemplate-swagger1/src/main/java/org/openapitools/client/ApiClient.java index cfa530eaca2..446c2c721a3 100644 --- a/samples/client/petstore/java/resttemplate-swagger1/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/resttemplate-swagger1/src/main/java/org/openapitools/client/ApiClient.java @@ -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; @@ -79,6 +80,10 @@ public class ApiClient extends JavaTimeFormatter { private HttpHeaders defaultHeaders = new HttpHeaders(); private MultiValueMap defaultCookies = new LinkedMultiValueMap(); + private int maxAttemptsForRetry = 1; + + private long waitTimeMillis = 10; + private String basePath = "http://petstore.swagger.io/v2"; private RestTemplate restTemplate; @@ -136,6 +141,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). * @@ -629,7 +674,23 @@ public class ApiClient extends JavaTimeFormatter { RequestEntity requestEntity = requestBuilder.body(selectBody(body, formParams, contentType)); - ResponseEntity responseEntity = restTemplate.exchange(requestEntity, returnType); + ResponseEntity 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; diff --git a/samples/client/petstore/java/resttemplate-swagger2/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/resttemplate-swagger2/src/main/java/org/openapitools/client/ApiClient.java index cfa530eaca2..446c2c721a3 100644 --- a/samples/client/petstore/java/resttemplate-swagger2/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/resttemplate-swagger2/src/main/java/org/openapitools/client/ApiClient.java @@ -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; @@ -79,6 +80,10 @@ public class ApiClient extends JavaTimeFormatter { private HttpHeaders defaultHeaders = new HttpHeaders(); private MultiValueMap defaultCookies = new LinkedMultiValueMap(); + private int maxAttemptsForRetry = 1; + + private long waitTimeMillis = 10; + private String basePath = "http://petstore.swagger.io/v2"; private RestTemplate restTemplate; @@ -136,6 +141,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). * @@ -629,7 +674,23 @@ public class ApiClient extends JavaTimeFormatter { RequestEntity requestEntity = requestBuilder.body(selectBody(body, formParams, contentType)); - ResponseEntity responseEntity = restTemplate.exchange(requestEntity, returnType); + ResponseEntity 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; diff --git a/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/ApiClient.java index 6de08db3168..72282e70426 100644 --- a/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/ApiClient.java @@ -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.HttpServerErrorException; import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; import org.springframework.web.util.UriComponentsBuilder; @@ -86,6 +87,10 @@ public class ApiClient extends JavaTimeFormatter { private HttpHeaders defaultHeaders = new HttpHeaders(); private MultiValueMap defaultCookies = new LinkedMultiValueMap(); + private int maxAttemptsForRetry = 1; + + private long waitTimeMillis = 10; + private String basePath = "http://petstore.swagger.io:80/v2"; private RestTemplate restTemplate; @@ -146,6 +151,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). * @@ -692,7 +737,23 @@ public class ApiClient extends JavaTimeFormatter { RequestEntity requestEntity = requestBuilder.body(selectBody(body, formParams, contentType)); - ResponseEntity responseEntity = restTemplate.exchange(requestEntity, returnType); + ResponseEntity 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; diff --git a/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/ApiClient.java index 1dbc33b8f2d..f20fc5eb7cc 100644 --- a/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/ApiClient.java @@ -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; @@ -81,6 +82,10 @@ public class ApiClient extends JavaTimeFormatter { private HttpHeaders defaultHeaders = new HttpHeaders(); private MultiValueMap defaultCookies = new LinkedMultiValueMap(); + private int maxAttemptsForRetry = 1; + + private long waitTimeMillis = 10; + private String basePath = "http://petstore.swagger.io:80/v2"; private RestTemplate restTemplate; @@ -141,6 +146,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). * @@ -687,7 +732,23 @@ public class ApiClient extends JavaTimeFormatter { RequestEntity requestEntity = requestBuilder.body(selectBody(body, formParams, contentType)); - ResponseEntity responseEntity = restTemplate.exchange(requestEntity, returnType); + ResponseEntity 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;