diff --git a/resources/sdk/purecloudjava/templates/ApiClient.mustache b/resources/sdk/purecloudjava/templates/ApiClient.mustache index 661d87304..59599392c 100644 --- a/resources/sdk/purecloudjava/templates/ApiClient.mustache +++ b/resources/sdk/purecloudjava/templates/ApiClient.mustache @@ -830,7 +830,7 @@ public class ApiClient implements AutoCloseable { return new ApiResponseWrapper<>(statusCode, reasonPhrase, headers, body, entity); } else { - String message = "error"; + String message = (statusCode == 429) ? "Max number of retries exceeded" : "error"; String body = response.readBody(); throw new ApiException(statusCode, message, headers, body); } @@ -1586,6 +1586,7 @@ public class ApiClient implements AutoCloseable { private boolean waitBeforeRetry(long retryAfterMs){ try { + System.out.printf("Rate limit encountered. Retry the request in [%d] seconds\n", retryAfterMs/1000L); Thread.sleep(retryAfterMs); } catch (InterruptedException e) { Thread.currentThread().interrupt(); diff --git a/resources/sdk/purecloudjava/tests/ApiClientRetryTest.java b/resources/sdk/purecloudjava/tests/ApiClientRetryTest.java index cbe4b3003..581874900 100644 --- a/resources/sdk/purecloudjava/tests/ApiClientRetryTest.java +++ b/resources/sdk/purecloudjava/tests/ApiClientRetryTest.java @@ -120,6 +120,37 @@ public void invokeTestWith_429_And_No_MaxRetryTime() throws IOException { } } + @Test + public void invokeTestWith_429_MaxRetriesExceeded() throws IOException { + ApiClient.RetryConfiguration retryConfiguration = new ApiClient.RetryConfiguration(); + + retryConfiguration.setMaxRetryTimeSec(6); + retryConfiguration.setRetryAfterDefaultMs(100); + retryConfiguration.setRetryMax(0); + retryConfiguration.setMaxRetriesBeforeBackoff(2); + + apiClient = getApiClient(retryConfiguration); + + mockResponse = getMockCloseableHttpResponse(429); + + Header header = mock(Header.class); + when(header.getName()).thenReturn("Retry-After"); + when(header.getValue()).thenReturn("3"); + + when(mockResponse.getAllHeaders()).thenReturn(new Header[]{header}); + doReturn(mockResponse).when(spyClient).execute(any(HttpUriRequest.class)); + + try { + stopwatch = Stopwatch.createStarted(); + ApiResponse response = apiClient.invoke(getConnectorRequest(), getReturnType()); + } catch (ApiException ex) { + Assert.assertEquals(429, ex.getStatusCode()); + Assert.assertEquals("Max number of retries exceeded", ex.getMessage()); + Assert.assertTrue(stopwatch.elapsed(TimeUnit.MILLISECONDS) >= 3000 && stopwatch.elapsed(TimeUnit.MILLISECONDS) < 3100, "Since retryMax is 0, after one retry exception is thrown"); + stopwatch.stop(); + } + } + @Test public void invokeTestWith_502() throws IOException { ApiClient.RetryConfiguration retryConfiguration = new ApiClient.RetryConfiguration();