From d070fcda2258da7dde32bd56f6d62a6d03742617 Mon Sep 17 00:00:00 2001 From: GuangrongFu Date: Thu, 3 Mar 2022 11:34:32 +0800 Subject: bugfix - fixed the healthcheck problem Issue-ID: HOLMES-512 Signed-off-by: GuangrongFu Change-Id: I95891f918b0abb2ea74caf8f1d39aed92c80624c --- .../java/org/onap/holmes/engine/Initializer.java | 34 +++++++++++++++++++--- .../org/onap/holmes/engine/InitializerTest.java | 17 +++++++++++ 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/engine-d/src/main/java/org/onap/holmes/engine/Initializer.java b/engine-d/src/main/java/org/onap/holmes/engine/Initializer.java index 9eca2a6..f3dae34 100644 --- a/engine-d/src/main/java/org/onap/holmes/engine/Initializer.java +++ b/engine-d/src/main/java/org/onap/holmes/engine/Initializer.java @@ -29,6 +29,8 @@ import javax.annotation.PostConstruct; import javax.inject.Inject; import java.util.HashSet; import java.util.Set; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; import static org.onap.holmes.common.config.MicroServiceConfig.getMicroServiceIpAndPort; import static org.onap.holmes.common.utils.CommonUtils.getEnv; @@ -37,6 +39,7 @@ import static org.onap.holmes.common.utils.CommonUtils.isIpAddress; @Service public class Initializer { private static final Logger logger = LoggerFactory.getLogger(Initializer.class); + private volatile static boolean readyForMsbReg = false; private MsbRegister msbRegister; @Inject @@ -46,13 +49,36 @@ public class Initializer { @PostConstruct private void init() { - try { - msbRegister.register2Msb(createMicroServiceInfo()); - } catch (CorrelationException e) { - logger.error(e.getMessage(), e); + Executors.newSingleThreadExecutor().execute(() -> { + waitUntilReady(); + try { + msbRegister.register2Msb(createMicroServiceInfo()); + } catch (CorrelationException e) { + logger.error(e.getMessage(), e); + } + }); + } + + private void waitUntilReady() { + int count = 1; + while (!readyForMsbReg) { + if (count > 20) { + break; + } + int interval = 5 * count++; + logger.info("Not ready for MSB registration. Try again after {} seconds...", interval); + try { + TimeUnit.SECONDS.sleep(interval); + } catch (InterruptedException e) { + logger.info(e.getMessage(), e); + } } } + public static void setReadyForMsbReg(boolean readyForMsbReg) { + Initializer.readyForMsbReg = readyForMsbReg; + } + private MicroServiceInfo createMicroServiceInfo() { String[] serviceIpAndPort = getMicroServiceIpAndPort(); MicroServiceInfo msinfo = new MicroServiceInfo(); diff --git a/engine-d/src/test/java/org/onap/holmes/engine/InitializerTest.java b/engine-d/src/test/java/org/onap/holmes/engine/InitializerTest.java index d6d0b67..788c3ae 100644 --- a/engine-d/src/test/java/org/onap/holmes/engine/InitializerTest.java +++ b/engine-d/src/test/java/org/onap/holmes/engine/InitializerTest.java @@ -25,6 +25,8 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.reflect.internal.WhiteboxImpl; +import java.util.concurrent.TimeUnit; + @RunWith(PowerMockRunner.class) @PrepareForTest(MicroServiceConfig.class) public class InitializerTest { @@ -43,8 +45,23 @@ public class InitializerTest { PowerMock.replayAll(); + setReadyFlagAfter(3); + WhiteboxImpl.invokeMethod(initializer, "init"); + TimeUnit.SECONDS.sleep(6); + PowerMock.verifyAll(); } + + private void setReadyFlagAfter(final int second) { + new Thread(() -> { + try { + TimeUnit.SECONDS.sleep(second); + } catch (InterruptedException e) { + e.printStackTrace(); + } + Initializer.setReadyForMsbReg(true); + }).start(); + } } \ No newline at end of file -- cgit 1.2.3-korg