package cn.howardliu.monitor.cynomys.agent.counter;

import cn.howardliu.monitor.cynomys.agent.conf.Parameters;
import cn.howardliu.monitor.cynomys.agent.dto.JavaInformations;
import cn.howardliu.monitor.cynomys.agent.handler.MonitorChecker;

import static cn.howardliu.monitor.cynomys.agent.common.Constant.started;
import static cn.howardliu.monitor.cynomys.common.Constant.SERVER_PORT;

/**
 * <br>created at 17-4-12
 *
 * @author liuxh
 * @version 0.0.1
 * @since 0.0.1
 */
public class MonitorStarter {
    public static void run() {
        if (!started) {
            JavaInformations javaInformations = JavaInformations.instance(true);
            Parameters.initialize();
            int port;
            if (javaInformations.getTomcatInformationsList().isEmpty()) {
                // TODO read port use System.getProperty(String, String)
                port = Integer.valueOf(System.getProperty("server.port", SERVER_PORT + ""));
            } else {
                port = Integer.valueOf(javaInformations.getTomcatInformationsList().get(0).getHttpPort());
            }
            SERVER_PORT = port;
            SLACounter.init();
            new MonitorChecker(port).startHealth("Active");
            started = true;
        }
    }
}

@@ -234,16 +234,129 @@
 Checker;
+%0Aimport cn.howardliu.monitor.cynomys.common.LaunchLatch;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Aimport
@@ -623,58 +623,211 @@
    p
-ublic static void run() %7B%0A        if (!started) %7B%0A
+rivate static final Logger logger = LoggerFactory.getLogger(MonitorStarter.class);%0A%0A    public static synchronized void run() %7B%0A        if (!started) %7B%0A            Thread thread = new Thread(() -%3E %7B%0A    
     
@@ -915,16 +915,20 @@
         
+    
 Paramete
@@ -956,16 +956,20 @@
         
+    
 int port
@@ -966,24 +966,28 @@
   int port;%0A
+    
             
@@ -1056,77 +1056,8 @@
     
-            // TODO read port use System.getProperty(String, String)%0A
     
@@ -1157,16 +1157,20 @@
         
+    
 %7D else %7B
@@ -1170,16 +1170,20 @@
  else %7B%0A
+    
         
@@ -1289,24 +1289,28 @@
             
+    
 %7D%0A          
@@ -1311,16 +1311,20 @@
         
+ 
+   
 SERVER_P
@@ -1347,16 +1347,20 @@
         
+    
 SLACount
@@ -1374,67 +1374,470 @@
 ();%0A
-            new MonitorChecker(port).startHealth(%22Active%22);
+%0A                try %7B%0A                    LaunchLatch.LATCH.waitForMillis(120_000);%0A                %7D catch (InterruptedException e) %7B%0A                    e.printStackTrace();%0A                    logger.error(%22LaunchLatch was interrupted!%22, e);%0A                %7D%0A                new MonitorChecker(port).startHealth(%22Active%22);%0A            %7D);%0A            thread.setName(%22monitor-starter-thread%22);%0A            thread.setDaemon(true);%0A            thread.start();%0A
 %0A   

use thread to wait for START signal