This repository has been archived by the owner on May 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#13] Automatic retry of selected calls using asyn-retry library
- Loading branch information
1 parent
40f18bf
commit 0c32d27
Showing
32 changed files
with
466 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 0 additions & 23 deletions
23
...fg/infrastructure/web/resttemplate/fluent/common/response/executor/HttpEntityUtils.groovy
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...m/ofg/infrastructure/web/resttemplate/fluent/common/response/executor/RestExecutor.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.ofg.infrastructure.web.resttemplate.fluent.common.response.executor | ||
|
||
import com.google.common.util.concurrent.ListenableFuture | ||
import com.nurkiewicz.asyncretry.RetryExecutor | ||
import groovy.transform.CompileStatic | ||
import org.springframework.http.HttpEntity | ||
import org.springframework.http.HttpHeaders | ||
import org.springframework.http.HttpMethod | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.web.client.RestOperations | ||
|
||
import static com.ofg.infrastructure.web.resttemplate.fluent.common.response.executor.UrlParsingUtils.appendPathToHost | ||
|
||
/** | ||
* Utility class that extracts {@link HttpEntity} from the provided map of passed parameters | ||
*/ | ||
@CompileStatic | ||
final class RestExecutor<T> { | ||
private final RestOperations restOperations | ||
private final RetryExecutor retryExecutor | ||
|
||
RestExecutor(RestOperations restOperations, RetryExecutor retryExecutor) { | ||
this.restOperations = restOperations | ||
this.retryExecutor = retryExecutor | ||
} | ||
|
||
ResponseEntity<T> exchange(HttpMethod httpMethod, Map params, Class<T> responseType) { | ||
return exchangeAsync(httpMethod, params, responseType).get() | ||
} | ||
|
||
ListenableFuture<ResponseEntity<T>> exchangeAsync(HttpMethod httpMethod, Map params, Class<T> responseType) { | ||
if (params.url) { | ||
return callUrlWithRetry(httpMethod, params, responseType) | ||
} else if (params.urlTemplate) { | ||
return callUrlTemplateWithRetry(httpMethod, params, responseType) | ||
} | ||
throw new InvalidHttpMethodParametersException(params) | ||
} | ||
|
||
protected ListenableFuture<ResponseEntity<T>> callUrlTemplateWithRetry(HttpMethod httpMethod, Map params, Class<T> responseType) { | ||
return retryExecutor.getWithRetry { | ||
//TODO Correlation ID | ||
return restOperations.exchange( | ||
appendPathToHost(params.host as String, params.urlTemplate as String), | ||
httpMethod, | ||
getHttpEntityFrom(params), | ||
responseType, | ||
params.urlVariablesArray as Object[] ?: params.urlVariablesMap as Map<String, ?>) | ||
} | ||
} | ||
|
||
protected ListenableFuture<ResponseEntity<T>> callUrlWithRetry(HttpMethod httpMethod, Map params, Class<T> responseType) { | ||
return retryExecutor.getWithRetry { | ||
//TODO Correlation ID | ||
restOperations.exchange( | ||
new URI(appendPathToHost(params.host as String, params.url as URI)), | ||
httpMethod, | ||
getHttpEntityFrom(params), | ||
responseType) | ||
} | ||
} | ||
|
||
static HttpEntity<Object> getHttpEntityFrom(Map params) { | ||
if (params.httpEntity) { | ||
return params.httpEntity as HttpEntity | ||
} | ||
HttpHeaders headers = params.headers as HttpHeaders | ||
HttpEntity<?> httpEntity = new HttpEntity<Object>(params.request, headers) | ||
return httpEntity | ||
} | ||
} |
11 changes: 9 additions & 2 deletions
11
...ofg/infrastructure/web/resttemplate/fluent/common/response/receive/ObjectReceiving.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,17 @@ | ||
package com.ofg.infrastructure.web.resttemplate.fluent.common.response.receive | ||
|
||
import com.google.common.util.concurrent.ListenableFuture | ||
|
||
|
||
/** | ||
* Interface that defines what is the type of the received response. | ||
* It will return an object of provided class. | ||
*/ | ||
interface ObjectReceiving { | ||
abstract class ObjectReceiving { | ||
|
||
public <T> T ofType(Class<T> responseType) | ||
public <T> T ofType(Class<T> responseType) { | ||
return ofTypeAsync(responseType).get() | ||
} | ||
|
||
public abstract <T> ListenableFuture<T> ofTypeAsync(Class<T> responseType) | ||
} |
8 changes: 6 additions & 2 deletions
8
...astructure/web/resttemplate/fluent/common/response/receive/ResponseEntityReceiving.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,15 @@ | ||
package com.ofg.infrastructure.web.resttemplate.fluent.common.response.receive | ||
|
||
import com.google.common.util.concurrent.ListenableFuture | ||
import org.springframework.http.ResponseEntity | ||
|
||
/** | ||
* Interface that defines what is the type of the received response. | ||
* It will return a {@link ResponseEntity} of the provided class. | ||
*/ | ||
interface ResponseEntityReceiving { | ||
public <T> ResponseEntity<T> ofType(Class<T> responseType) | ||
abstract class ResponseEntityReceiving { | ||
public abstract <T> ListenableFuture<ResponseEntity<T>> ofTypeAsync(Class<T> responseType) | ||
public <T> ResponseEntity<T> ofType(Class<T> responseType) { | ||
return ofTypeAsync(responseType).get() | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...fg/infrastructure/web/resttemplate/fluent/common/response/receive/ResponseIgnoring.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
package com.ofg.infrastructure.web.resttemplate.fluent.common.response.receive | ||
|
||
import com.google.common.util.concurrent.ListenableFuture | ||
|
||
interface ResponseIgnoring { | ||
/** | ||
* When you do not care about the received response for your HTTP request | ||
*/ | ||
void ignoringResponse() | ||
ListenableFuture<Void> ignoringResponseAsync() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.