Switched from Dropwizard to Springboot
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / utils / MsbRegister.java
index 3b72aaf..957b818 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * Copyright 2017-2020 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.
 
 package org.onap.holmes.common.utils;
 
+import lombok.Setter;
 import org.apache.commons.lang3.StringUtils;
-import org.jvnet.hk2.annotations.Service;
 import org.onap.holmes.common.config.MicroServiceConfig;
 import org.onap.holmes.common.exception.CorrelationException;
 import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;
 import org.onap.msb.sdk.discovery.entity.MicroServiceInfo;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Service;
 
 import javax.ws.rs.client.Entity;
 import javax.ws.rs.core.MediaType;
@@ -38,6 +39,12 @@ public class MsbRegister {
 
     private JerseyClient client = JerseyClient.newInstance();
 
+    @Setter
+    private int totalRetryTimes = 20;
+
+    @Setter
+    private int interval = 20;
+
     public MsbRegister() {
     }
 
@@ -48,14 +55,14 @@ public class MsbRegister {
                 && msbAddrInfo[1].equals("443");
 
         log.info("Start to register Holmes Service to MSB...");
+        log.info("Registration information: {}", GsonUtil.beanToJson(msinfo));
 
         MicroServiceFullInfo microServiceFullInfo = null;
         int retry = 0;
-        int interval = 5;
-        while (null == microServiceFullInfo && retry < 20) {
+        while (null == microServiceFullInfo && retry < totalRetryTimes) {
+            int time  = interval * ++retry;
             try {
-                log.info("Holmes Service Registration. Retry: " + retry++);
-
+                log.info("Holmes Service Registration. Times: " + retry);
                 microServiceFullInfo = client
                         .header("Accept", MediaType.APPLICATION_JSON)
                         .queryParam("createOrUpdate", true)
@@ -65,15 +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.", interval));
-                    threadSleep(TimeUnit.SECONDS.toSeconds(interval));
-                    interval += 5;
+                    retry(time);
                 } else {
                     log.info("Registration succeeded!");
                     break;
                 }
             } catch (Exception e) {
                 log.warn("Unexpected exception: " + e.getMessage(), e);
+                retry(time);
             }
         }
 
@@ -84,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 {