Fixed an Issue on API Calling via MSB
[holmes/rule-management.git] / rulemgt / src / main / java / org / onap / holmes / rulemgt / msb / MsbQuery.java
index ed9b9af..df0783d 100644 (file)
@@ -1,12 +1,12 @@
 /**
- * Copyright 2017 ZTE Corporation.
- *
+ * Copyright 2017-2020 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.
  * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  */
 package org.onap.holmes.rulemgt.msb;
 
-import lombok.extern.slf4j.Slf4j;
-import org.jvnet.hk2.annotations.Service;
-import org.onap.holmes.rulemgt.send.RuleAllocation;
+import org.onap.holmes.common.dropwizard.ioc.utils.ServiceLocatorHolder;
 import org.onap.holmes.rulemgt.send.Ip4AddingRule;
-import org.onap.holmes.rulemgt.wrapper.RuleMgtWrapper;
+import org.onap.holmes.rulemgt.send.RuleAllocator;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
-import javax.annotation.PostConstruct;
-import javax.inject.Inject;
 import java.util.List;
 import java.util.Timer;
 import java.util.TimerTask;
 
+import static java.util.concurrent.TimeUnit.SECONDS;
 
-@Service
-@Slf4j
 public class MsbQuery {
 
-    @Inject
-    private RuleAllocation ruleAllocation;
-
-    @Inject
+    static final private Logger log = LoggerFactory.getLogger(MsbQuery.class);
+    final private RuleAllocator ruleAllocator;
     private Ip4AddingRule ip4AddingRule;
+    private EngineInsQueryTool engineInsQueryTool;
 
-    @Inject
-    private EngineIpList engineIpList;
-
-    @Inject
-    private RuleMgtWrapper ruleMgtWrapper;
-
-    private  List<String> timerIpList;
-
-    @PostConstruct
-    public void init() {
-
-        try{
-            timer();
-        }catch(Exception e){
-            log.error("MSBQuery init timer task failed !" + e.getMessage());
-        }
-
+    public MsbQuery() {
+        ruleAllocator = new RuleAllocator();
+        ip4AddingRule = ServiceLocatorHolder.getLocator().getService(Ip4AddingRule.class);
+        engineInsQueryTool = ServiceLocatorHolder.getLocator().getService(EngineInsQueryTool.class);
     }
 
-    public void timer() throws Exception{
-        Timer timer = new Timer();
-        timer.schedule(new TimerTask() {
+    public void startTimer() {
+        try {
+            new Timer().schedule(new TimerTask() {
 
-            public void run() {
-                try {
-                    timerIpList = engineIpList.getServiceCount();
-                    ip4AddingRule.getIpList(timerIpList);
-                    ruleAllocation.judgeAndAllocateRule(timerIpList);
+                public void run() {
+                    try {
+                        List<String> timerIpList = engineInsQueryTool.getInstanceList();
+                        log.info(String.format("There are %d engine instance(s) running currently.", timerIpList.size()));
 
-                } catch (Exception e) {
-                    log.error("The timing query engine instance failed " ,e);
+                        ip4AddingRule.setIpList(timerIpList);
+                        ruleAllocator.allocateRules(timerIpList);
+                    } catch (Exception e) {
+                        log.error("The timing query engine instance failed ", e);
+                    }
                 }
-            }
-
-        }, 5000, 30000);
 
+            }, SECONDS.toMillis(10), SECONDS.toMillis(30));
+        } catch (Exception e) {
+            log.error("MSBQuery startTimer timer task failed !" + e.getMessage(), e);
+            try {
+                SECONDS.sleep(30);
+            } catch (InterruptedException e1) {
+                Thread.currentThread().interrupt();
+            }
+        }
     }
-
 }