bugfix - fixed the healthcheck problem 42/127442/1
authorGuangrongFu <fu.guangrong@zte.com.cn>
Wed, 2 Mar 2022 13:00:36 +0000 (21:00 +0800)
committerGuangrongFu <fu.guangrong@zte.com.cn>
Wed, 2 Mar 2022 13:00:36 +0000 (21:00 +0800)
Issue-ID: HOLMES-512
Signed-off-by: GuangrongFu <fu.guangrong@zte.com.cn>
Change-Id: Ice381df0b40853815b2f7e2bd8ab70af304629c1

rulemgt/src/main/java/org/onap/holmes/rulemgt/Initializer.java
rulemgt/src/main/java/org/onap/holmes/rulemgt/RuleActiveApp.java
rulemgt/src/test/java/org/onap/holmes/rulemgt/InitializerTest.java

index c39de60..812ee48 100644 (file)
@@ -30,6 +30,7 @@ import javax.annotation.PostConstruct;
 import javax.inject.Inject;
 import java.util.HashSet;
 import java.util.Set;
 import javax.inject.Inject;
 import java.util.HashSet;
 import java.util.Set;
+import java.util.concurrent.TimeUnit;
 
 import static org.onap.holmes.common.utils.CommonUtils.getEnv;
 import static org.onap.holmes.common.utils.CommonUtils.isIpAddress;
 
 import static org.onap.holmes.common.utils.CommonUtils.getEnv;
 import static org.onap.holmes.common.utils.CommonUtils.isIpAddress;
@@ -37,6 +38,7 @@ import static org.onap.holmes.common.utils.CommonUtils.isIpAddress;
 @Service
 public class Initializer {
     private static final Logger logger = LoggerFactory.getLogger(Initializer.class);
 @Service
 public class Initializer {
     private static final Logger logger = LoggerFactory.getLogger(Initializer.class);
+    private static boolean readyForMsbReg = false;
     private MsbRegister msbRegister;
 
     @Inject
     private MsbRegister msbRegister;
 
     @Inject
@@ -46,6 +48,7 @@ public class Initializer {
 
     @PostConstruct
     private void init() {
 
     @PostConstruct
     private void init() {
+        waitUntilReady();
         try {
             msbRegister.register2Msb(createMicroServiceInfo());
         } catch (CorrelationException e) {
         try {
             msbRegister.register2Msb(createMicroServiceInfo());
         } catch (CorrelationException e) {
@@ -53,6 +56,26 @@ public class Initializer {
         }
     }
 
         }
     }
 
+    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 = MicroServiceConfig.getMicroServiceIpAndPort();
         MicroServiceInfo msinfo = new MicroServiceInfo();
     private MicroServiceInfo createMicroServiceInfo() {
         String[] serviceIpAndPort = MicroServiceConfig.getMicroServiceIpAndPort();
         MicroServiceInfo msinfo = new MicroServiceInfo();
index a9a78c8..cedcde0 100644 (file)
@@ -45,6 +45,8 @@ public class RuleActiveApp extends IOCApplication<RuleAppConfig> {
 
         environment.servlets().addFilter("customFilter", new TransactionIdFilter()).addMappingForUrlPatterns(EnumSet
                 .allOf(DispatcherType.class), true, "/*");
 
         environment.servlets().addFilter("customFilter", new TransactionIdFilter()).addMappingForUrlPatterns(EnumSet
                 .allOf(DispatcherType.class), true, "/*");
+
+        Initializer.setReadyForMsbReg(true);
     }
 }
 
     }
 }
 
index 120aba2..f658d35 100644 (file)
@@ -26,6 +26,8 @@ import org.powermock.core.classloader.annotations.SuppressStaticInitializationFo
 import org.powermock.modules.junit4.PowerMockRunner;
 import org.powermock.reflect.internal.WhiteboxImpl;
 
 import org.powermock.modules.junit4.PowerMockRunner;
 import org.powermock.reflect.internal.WhiteboxImpl;
 
+import java.util.concurrent.TimeUnit;
+
 @RunWith(PowerMockRunner.class)
 @PrepareForTest(MicroServiceConfig.class)
 @SuppressStaticInitializationFor("org.onap.holmes.common.utils.CommonUtils")
 @RunWith(PowerMockRunner.class)
 @PrepareForTest(MicroServiceConfig.class)
 @SuppressStaticInitializationFor("org.onap.holmes.common.utils.CommonUtils")
@@ -45,8 +47,21 @@ public class InitializerTest {
 
         PowerMock.replayAll();
 
 
         PowerMock.replayAll();
 
+        setReadyFlagAfter(3);
+
         WhiteboxImpl.invokeMethod(initializer, "init");
 
         PowerMock.verifyAll();
     }
         WhiteboxImpl.invokeMethod(initializer, "init");
 
         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
 }
\ No newline at end of file