Change the package name from openo to onap
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / utils / MSBRegisterUtil.java
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/utils/MSBRegisterUtil.java b/holmes-actions/src/main/java/org/onap/holmes/common/utils/MSBRegisterUtil.java
new file mode 100644 (file)
index 0000000..1bb3fe3
--- /dev/null
@@ -0,0 +1,74 @@
+/**\r
+ * Copyright 2017 ZTE Corporation.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package org.onap.holmes.common.utils;\r
+\r
+import com.eclipsesource.jaxrs.consumer.ConsumerFactory;\r
+import java.io.IOException;\r
+import lombok.extern.slf4j.Slf4j;\r
+import org.jvnet.hk2.annotations.Service;\r
+import org.onap.holmes.common.msb.MicroserviceBusRest;\r
+import org.onap.holmes.common.api.entity.ServiceRegisterEntity;\r
+import org.onap.holmes.common.config.MicroServiceConfig;\r
+\r
+@Slf4j\r
+@Service\r
+public class MSBRegisterUtil {\r
+\r
+    public void register(ServiceRegisterEntity entity) throws IOException {\r
+        log.info("start holmes micro service register");\r
+        boolean flag = false;\r
+        int retry = 0;\r
+        while (!flag && retry < 20) {\r
+            log.info("Holmes microservice register. retry:" + retry);\r
+            retry++;\r
+            flag = innerRegister(entity);\r
+            if (!flag) {\r
+                log.warn("micro service register failed, sleep 30S and try again.");\r
+                threadSleep(30000);\r
+            } else {\r
+                log.info("micro service register success!");\r
+                break;\r
+            }\r
+        }\r
+        log.info("holmes micro service register end.");\r
+    }\r
+\r
+    private boolean innerRegister(ServiceRegisterEntity entity) {\r
+        try {\r
+            log.info("msbServerAddr:" + MicroServiceConfig.getMsbServerAddr());\r
+            log.info("entity:" + entity);\r
+            MicroserviceBusRest resourceserviceproxy = ConsumerFactory.createConsumer(\r
+                    MicroServiceConfig.getMsbServerAddr(), MicroserviceBusRest.class);\r
+            resourceserviceproxy.registerServce("false", entity);\r
+        } catch (Exception error) {\r
+            log.error("microservice register failed!" + error.getMessage(), error);\r
+            return false;\r
+        }\r
+        return true;\r
+    }\r
+\r
+    private void threadSleep(int second) {\r
+        log.info("start sleep ....");\r
+        try {\r
+            Thread.sleep(second);\r
+        } catch (InterruptedException error) {\r
+            log.error("thread sleep error.errorMsg:" + error.getMessage(), error);\r
+            Thread.currentThread().interrupt();\r
+        }\r
+        log.info("sleep end .");\r
+    }\r
+}
\ No newline at end of file