Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(synapse-client-rest): adding retry method to reactive rest client #267

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
*/
package io.americanexpress.synapse.client.rest.client;

import java.time.Duration;
import java.util.List;

import io.americanexpress.synapse.client.rest.factory.BaseClientHttpHeadersFactory;
import io.americanexpress.synapse.framework.exception.ApplicationClientException;
import io.americanexpress.synapse.framework.exception.model.ErrorCode;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
Expand All @@ -29,6 +32,7 @@
import io.americanexpress.synapse.client.rest.model.QueryParameter;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.retry.Retry;

/**
* {@code BaseReactiveRestClient} class specifies the prototypes for all reactive REST clients.
Expand Down Expand Up @@ -113,6 +117,37 @@ public Mono<O> callMonoService(HttpHeaders headers, I clientRequest, List<QueryP
.bodyToMono(clientResponseType);
}

/**
* Get the mono response from the service given the HTTP headers and request body. If call to service fails,
* can retry after delayInMilliSeconds and will retry up to numberOfRetries.
*
* @param headers headers for the back end service
* @param clientRequest body of the request
* @param numberOfRetries the max number of retries
* @param delayInMilliSeconds the delay between retry attempts in milliseconds
* @param queryParameters parameters needed to be added to URI
* @param pathVariables variables needed to be added to URI
* @return the mono response body from the back end service
*/
public Mono<O> callMonoServiceWithRetry(HttpHeaders headers, I clientRequest, int numberOfRetries, long delayInMilliSeconds, List<QueryParameter> queryParameters, String... pathVariables) {
// Get the updated URL which may change in each client request due to path variables and/or query parameters
String updatedUrl = UrlBuilder.build(url, queryParameters, pathVariables);

return webClient.method(httpMethod)
.uri(updatedUrl)
.headers(httpHeaders ->
httpHeaders.addAll(httpHeadersFactory.create(headers, clientRequest, updatedUrl)))
.body(Mono.just(clientRequest), clientRequestType)
.retrieve()
.onStatus(HttpStatus::isError, reactiveRestResponseErrorHandler)
.bodyToMono(clientResponseType)
.retryWhen(Retry.fixedDelay(numberOfRetries, Duration.ofMillis(delayInMilliSeconds))
.filter(throwable -> throwable instanceof ApplicationClientException applicationClientException && applicationClientException.getErrorCode() == ErrorCode.GENERIC_5XX_ERROR)
.onRetryExhaustedThrow((retryBackoffSpec, retrySignal) -> {
throw new ApplicationClientException("External Service failed to process after max retries", ErrorCode.GENERIC_5XX_ERROR);
}));
}

/**
* Get the mono response entity from the service given the HTTP headers and request body.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<parent>
<artifactId>data-samples</artifactId>
<groupId>io.americanexpress.synapse</groupId>
<version>0.3.21-SNAPSHOT</version>
<version>0.3.23-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<parent>
<groupId>io.americanexpress.synapse</groupId>
<artifactId>service-samples</artifactId>
<version>0.3.21-SNAPSHOT</version>
<version>0.3.23-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down