Migrate from DW to Springboot
[holmes/rule-management.git] / rulemgt / src / main / java / org / onap / holmes / rulemgt / Initializer.java
index 6769d65..fc3e798 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * Copyright 2017-2018 ZTE Corporation.
+ * Copyright 2017-2022 ZTE Corporation.
  * <p>
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -16,7 +16,6 @@
 
 package org.onap.holmes.rulemgt;
 
-import org.jvnet.hk2.annotations.Service;
 import org.onap.holmes.common.config.MicroServiceConfig;
 import org.onap.holmes.common.exception.CorrelationException;
 import org.onap.holmes.common.utils.CommonUtils;
@@ -25,34 +24,62 @@ import org.onap.msb.sdk.discovery.entity.MicroServiceInfo;
 import org.onap.msb.sdk.discovery.entity.Node;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.ApplicationArguments;
+import org.springframework.boot.ApplicationRunner;
+import org.springframework.stereotype.Component;
 
-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.utils.CommonUtils.getEnv;
 import static org.onap.holmes.common.utils.CommonUtils.isIpAddress;
 
-@Service
-public class Initializer {
+@Component
+public class Initializer implements ApplicationRunner {
     private static final Logger logger = LoggerFactory.getLogger(Initializer.class);
+    private volatile static boolean readyForMsbReg = false;
     private MsbRegister msbRegister;
 
-    @Inject
+    @Autowired
     public Initializer(MsbRegister msbRegister) {
         this.msbRegister = msbRegister;
     }
 
-    @PostConstruct
-    private void init() {
-        try {
-            msbRegister.register2Msb(createMicroServiceInfo());
-        } catch (CorrelationException e) {
-            logger.error(e.getMessage(), e);
+    @Override
+    public void run(ApplicationArguments args) {
+        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 = MicroServiceConfig.getMicroServiceIpAndPort();
         MicroServiceInfo msinfo = new MicroServiceInfo();
@@ -72,10 +99,10 @@ public class Initializer {
         String msbAddrTemplate = (CommonUtils.isHttpsEnabled() ? "https" : "http")
                 + "://%s:%s/api/holmes-rule-mgmt/v1/healthcheck";
         node.setCheckType("HTTP");
-        node.setCheckUrl(String.format(msbAddrTemplate, serviceAddrInfo[0], "9101"));
+        node.setCheckUrl(String.format(msbAddrTemplate, serviceIpAndPort[0], "9101"));
         node.setCheckTimeOut("60s");
-        node.setCheckInterval("60s");
-        */
+        node.setCheckInterval("60s");*/
+
         nodes.add(node);
         msinfo.setNodes(nodes);
         return msinfo;