generated from pagopa/template-java-spring-microservice
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PAGOPA-2370] fix: Add retry delete session-id (#177)
Co-authored-by: pagopa-github-bot <[email protected]>
- Loading branch information
1 parent
2e4cd25
commit dd605c6
Showing
3 changed files
with
129 additions
and
16 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
75 changes: 75 additions & 0 deletions
75
src/main/java/it/gov/pagopa/wispconverter/custom/DecouplerApiClient.java
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,75 @@ | ||
package it.gov.pagopa.wispconverter.custom; | ||
|
||
import it.gov.pagopa.wispconverter.exception.AppException; | ||
import org.springframework.core.ParameterizedTypeReference; | ||
import org.springframework.http.*; | ||
import org.springframework.util.MultiValueMap; | ||
import org.springframework.web.client.*; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class DecouplerApiClient extends it.gov.pagopa.gen.wispconverter.client.decouplercaching.invoker.ApiClient { | ||
|
||
public DecouplerApiClient(RestTemplate restTemplate) { | ||
super(restTemplate); | ||
} | ||
|
||
@Override | ||
public <T> ResponseEntity<T> invokeAPI(String path, | ||
HttpMethod method, | ||
Map<String, Object> pathParams, | ||
MultiValueMap<String, String> queryParams, | ||
Object body, | ||
HttpHeaders headerParams, | ||
MultiValueMap<String, String> cookieParams, | ||
MultiValueMap<String, Object> formParams, | ||
List<MediaType> accept, | ||
MediaType contentType, | ||
String[] authNames, | ||
ParameterizedTypeReference<T> returnType) | ||
throws RestClientException { | ||
|
||
int attempts = 0; | ||
ResponseEntity<T> response = null; | ||
while (attempts < super.getMaxAttemptsForRetry()) { | ||
try { | ||
response = super.invokeAPI(path, method, pathParams, queryParams, body, headerParams, cookieParams, formParams, accept, contentType, authNames, returnType); | ||
break; | ||
} catch (AppException ex) { | ||
attempts = handleAppException(ex, attempts); | ||
} catch (ResourceAccessException ex) { | ||
attempts = handleRetry(ex, attempts); | ||
} | ||
} | ||
return response; | ||
} | ||
|
||
private int handleAppException(AppException ex, int attempts) { | ||
Throwable cause = ex.getCause(); | ||
if (cause instanceof RuntimeException runtimeException) { | ||
if (cause instanceof HttpServerErrorException || ((HttpClientErrorException) cause).getStatusCode().equals(HttpStatus.TOO_MANY_REQUESTS)) { | ||
attempts = handleRetry(runtimeException, attempts); | ||
} | ||
} else { | ||
throw ex; | ||
} | ||
return attempts; | ||
} | ||
|
||
|
||
private int handleRetry(RuntimeException ex, int attempts) { | ||
attempts++; | ||
if (attempts < super.getMaxAttemptsForRetry()) { | ||
try { | ||
Thread.sleep(super.getWaitTimeMillis()); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
} else { | ||
throw ex; | ||
} | ||
return attempts; | ||
} | ||
|
||
} |
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