From 577bd39ac9e9495b0c6a38e6f7c567acfcba1c42 Mon Sep 17 00:00:00 2001 From: "Benjamin, Max" Date: Wed, 26 Jun 2019 11:26:55 -0400 Subject: Update failsafe dependency to 2.0.1 Updated calls to failsafe 2.0.1 API Change-Id: I2c86ca02d56a07262e330ee5f8f088d1a1f5b4a7 Issue-ID: SO-2057 Signed-off-by: Benjamin, Max (mb388a) --- .../src/main/java/org/onap/so/client/RestClient.java | 16 +++++++--------- .../src/main/java/org/onap/so/client/RestRequest.java | 6 +++--- .../so/rest/exceptions/ExhaustedRetriesException.java | 19 +++++++++++++++++++ 3 files changed, 29 insertions(+), 12 deletions(-) create mode 100644 common/src/main/java/org/onap/so/rest/exceptions/ExhaustedRetriesException.java (limited to 'common/src/main') diff --git a/common/src/main/java/org/onap/so/client/RestClient.java b/common/src/main/java/org/onap/so/client/RestClient.java index 0b3aa651a0..d3a4d444f7 100644 --- a/common/src/main/java/org/onap/so/client/RestClient.java +++ b/common/src/main/java/org/onap/so/client/RestClient.java @@ -26,6 +26,7 @@ import java.net.SocketTimeoutException; import java.net.URI; import java.net.URL; import java.security.GeneralSecurityException; +import java.time.Duration; import java.util.ArrayList; import java.util.Base64; import java.util.HashMap; @@ -33,7 +34,6 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Optional; -import java.util.concurrent.TimeUnit; import java.util.function.Predicate; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; @@ -275,22 +275,20 @@ public abstract class RestClient { } public Response method(String method, Object entity) { - RetryPolicy policy = new RetryPolicy(); List> items = retryOn(); Predicate pred = items.stream().reduce(Predicate::or).orElse(x -> false); - policy.retryOn(error -> pred.test(error)); + RetryPolicy policy = + new RetryPolicy<>().handleIf(pred).withDelay(Duration.ofMillis(this.props.getDelayBetweenRetries())) + .withMaxRetries(this.props.getRetries()); - policy.withDelay(this.props.getDelayBetweenRetries(), TimeUnit.MILLISECONDS) - .withMaxRetries(this.props.getRetries()); - - return Failsafe.with(policy).get(buildRequest(method, entity)); + return Failsafe.with(policy).get(() -> buildRequest(method, entity)); } - protected RestRequest buildRequest(String method, Object entity) { - return new RestRequest(this, method, entity); + protected Response buildRequest(String method, Object entity) throws Exception { + return new RestRequest(this, method, entity).get(); } private Optional format(Response response, Class resultClass) { diff --git a/common/src/main/java/org/onap/so/client/RestRequest.java b/common/src/main/java/org/onap/so/client/RestRequest.java index 9d2fa42d00..9e6e818171 100644 --- a/common/src/main/java/org/onap/so/client/RestRequest.java +++ b/common/src/main/java/org/onap/so/client/RestRequest.java @@ -23,15 +23,15 @@ package org.onap.so.client; import java.util.Optional; -import java.util.concurrent.Callable; import javax.ws.rs.HttpMethod; import javax.ws.rs.NotFoundException; import javax.ws.rs.client.Entity; import javax.ws.rs.core.Response; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import net.jodah.failsafe.function.CheckedSupplier; -public class RestRequest implements Callable { +public class RestRequest implements CheckedSupplier { private static final Logger logger = LoggerFactory.getLogger(RestRequest.class); @@ -46,7 +46,7 @@ public class RestRequest implements Callable { } @Override - public Response call() throws Exception { + public Response get() throws Exception { final Response response; if ("GET".equals(method)) { response = this.client.getBuilder().accept(this.client.getAccept()).get(); diff --git a/common/src/main/java/org/onap/so/rest/exceptions/ExhaustedRetriesException.java b/common/src/main/java/org/onap/so/rest/exceptions/ExhaustedRetriesException.java new file mode 100644 index 0000000000..6c91516b84 --- /dev/null +++ b/common/src/main/java/org/onap/so/rest/exceptions/ExhaustedRetriesException.java @@ -0,0 +1,19 @@ +package org.onap.so.rest.exceptions; + +public class ExhaustedRetriesException extends RuntimeException { + + private static final long serialVersionUID = -8303091412739222943L; + + public ExhaustedRetriesException(String s) { + super(s); + } + + public ExhaustedRetriesException(Throwable t) { + super(t); + } + + public ExhaustedRetriesException(String s, Throwable t) { + super(s, t); + } + +} -- cgit 1.2.3-korg