From c00264599085dc0e29ca4f2cd4e24f00b0301ff1 Mon Sep 17 00:00:00 2001 From: Guangrong Fu Date: Fri, 6 May 2022 09:07:20 +0800 Subject: Optimized retry in the exception branch Missed a conditional branch before, now fixed that. Issue-ID: HOLMES-536 Signed-off-by: Guangrong Fu Change-Id: I682d31e14b30d1fbeb903760ae0788a25806d9b0 (cherry picked from commit b1cf3bb5648e23c65d78a808d8185d44d0727b1c) --- .../main/java/org/onap/holmes/common/utils/MsbRegister.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'holmes-actions/src/main') diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/utils/MsbRegister.java b/holmes-actions/src/main/java/org/onap/holmes/common/utils/MsbRegister.java index b8f6b86..33b53c7 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/utils/MsbRegister.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/utils/MsbRegister.java @@ -60,9 +60,9 @@ public class MsbRegister { MicroServiceFullInfo microServiceFullInfo = null; int retry = 0; while (null == microServiceFullInfo && retry < totalRetryTimes) { + int time = interval * ++retry; try { - log.info("Holmes Service Registration. Times: " + ++retry); - int time = interval * retry; + log.info("Holmes Service Registration. Times: " + retry); microServiceFullInfo = client .header("Accept", MediaType.APPLICATION_JSON) .queryParam("createOrUpdate", true) @@ -72,14 +72,14 @@ public class MsbRegister { MicroServiceFullInfo.class); if (null == microServiceFullInfo) { - log.warn(String.format("Failed to register the service to MSB. Sleep %ds and try again.", time)); - threadSleep(TimeUnit.SECONDS.toSeconds(time)); + retry(time); } else { log.info("Registration succeeded!"); break; } } catch (Exception e) { log.warn("Unexpected exception: " + e.getMessage(), e); + retry(time); } } @@ -90,6 +90,11 @@ public class MsbRegister { log.info("Service registration completed."); } + private void retry(int intervalInSecond) { + log.warn(String.format("Failed to register the service to MSB. Sleep %ds and try again.", intervalInSecond)); + threadSleep(TimeUnit.SECONDS.toSeconds(intervalInSecond)); + } + private void threadSleep(long second) { log.info("Start sleeping..."); try { -- cgit 1.2.3-korg