[SECIOTSRK-681] *Add health check for server MQ: remove cloud dependency
authorm.dalakov <m.dalakov@samsung.com>
Thu, 9 Nov 2017 09:31:17 +0000 (11:31 +0200)
committerm.dalakov <m.dalakov@samsung.com>
Thu, 9 Nov 2017 09:31:17 +0000 (11:31 +0200)
servers/mq/src/main/java/com/samsung/servermq/HealthCheck.java

index be759a7..73e7509 100644 (file)
@@ -4,10 +4,9 @@ import org.apache.log4j.Logger;
 import org.springframework.context.annotation.*;
 import java.io.IOException;
 import java.net.HttpURLConnection;
+import java.net.Socket;
 import java.net.URL;
 
-import static org.iotivity.cloud.util.HealthCheck.checkSocket;
-
 /**
  * The type Policy witout cloud test.
  *
@@ -55,6 +54,29 @@ public class HealthCheck {
         return false;
     }
 
+    public static boolean checkSocket(String socket) {
+        LOG.info("health check "+socket+"...");
+        String host="";
+        String port="";
+        int hasParts = socket.indexOf(":");
+        if (hasParts > -1) {
+            String[] parts = socket.split(":");
+            host = parts[0];
+            port = parts[1];
+        }
+        else {
+            LOG.info("socket has an incorrect format...");
+            return false;
+        }
+        try (Socket s = new Socket(host, Integer.valueOf(port))) {
+            LOG.info("health check was successful");
+            return true;
+        } catch (IOException ex) {
+            LOG.info(socket+" not available...");
+            return false;
+        }
+    }
+
     private static void checkHost(String host) {
         while (!checkSocket(host)) {
             sleep();