bugfix - fixed the healthcheck problem 61/127461/1
authorGuangrongFu <fu.guangrong@zte.com.cn>
Thu, 3 Mar 2022 03:34:32 +0000 (11:34 +0800)
committerGuangrongFu <fu.guangrong@zte.com.cn>
Thu, 3 Mar 2022 03:34:32 +0000 (11:34 +0800)
Issue-ID: HOLMES-512
Signed-off-by: GuangrongFu <fu.guangrong@zte.com.cn>
Change-Id: I95891f918b0abb2ea74caf8f1d39aed92c80624c

engine-d/src/main/java/org/onap/holmes/engine/Initializer.java
engine-d/src/test/java/org/onap/holmes/engine/InitializerTest.java

index 9eca2a6..f3dae34 100644 (file)
@@ -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();
index d6d0b67..788c3ae 100644 (file)
@@ -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