]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5409 - Renamed to Process and moved heartbeat in CTor
authorStephane Gamard <stephane.gamard@searchbox.com>
Wed, 9 Jul 2014 14:11:00 +0000 (16:11 +0200)
committerStephane Gamard <stephane.gamard@searchbox.com>
Wed, 9 Jul 2014 14:32:26 +0000 (16:32 +0200)
sonar-process/pom.xml
sonar-process/src/main/java/org/sonar/process/Launcher.java
sonar-process/src/main/java/org/sonar/process/Process.java [new file with mode: 0644]
sonar-process/src/main/java/org/sonar/process/Runner.java [deleted file]
sonar-process/src/test/java/org/sonar/process/ProcessTest.java [new file with mode: 0644]
sonar-process/src/test/java/org/sonar/process/RunnerTest.java [deleted file]

index 72a440e1f59b80e85c1f3ef302ce83d6ddd705e2..8707e09fdf14a8e6d95ded3c20c8842e9215b6cf 100644 (file)
   <name>SonarQube :: Process</name>
 
   <dependencies>
+
     <dependency>
-      <groupId>ch.qos.logback</groupId>
-      <artifactId>logback-access</artifactId>
+      <groupId>org.slf4j</groupId>
+      <artifactId>slf4j-api</artifactId>
     </dependency>
+
     <dependency>
       <groupId>ch.qos.logback</groupId>
       <artifactId>logback-classic</artifactId>
     </dependency>
+
     <dependency>
-      <groupId>ch.qos.logback</groupId>
-      <artifactId>logback-core</artifactId>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <scope>test</scope>
     </dependency>
-
     <dependency>
       <groupId>org.easytesting</groupId>
       <artifactId>fest-assert</artifactId>
index 8c7ed2e8b71dc2b8c8bd375ac0086706237b3273..96fee525fdc295221e3fec77f3d00a3743369410 100644 (file)
@@ -45,12 +45,12 @@ public class Launcher extends Thread {
   }
 
   private void launch() {
-    new Thread(new Runnable() {
-      @Override
-      public void run() {
-        Runner.main(name, socket.getLocalPort() + "");
-      }
-    }).start();
+//    new Thread(new Runnable() {
+//      @Override
+//      public void run() {
+//        Runner.main(name, socket.getLocalPort() + "");
+//      }
+//    }).start();
   }
 
   private void shutdown() {
@@ -61,6 +61,7 @@ public class Launcher extends Thread {
     long ping = Long.MAX_VALUE;
     try {
       while (true) {
+        LOGGER.info("My heart is beating");
         DatagramPacket packet = new DatagramPacket(new byte[1024], 1024);
         socket.receive(packet);
         long newPing = System.currentTimeMillis();
diff --git a/sonar-process/src/main/java/org/sonar/process/Process.java b/sonar-process/src/main/java/org/sonar/process/Process.java
new file mode 100644 (file)
index 0000000..3e493ec
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.process;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.net.DatagramPacket;
+import java.net.DatagramSocket;
+import java.net.InetAddress;
+
+/**
+ * @Since 4.5
+ */
+public abstract class Process implements Runnable {
+
+  private final static Logger LOGGER = LoggerFactory.getLogger(Process.class);
+
+  protected Long heartBeatInterval = 1000L;
+  private final Thread monitor;
+
+  final String name;
+  final int port;
+
+  public Process(String name, int port) {
+    this.name = name;
+    this.port = port;
+
+    //Starting monitoring thread
+    this.monitor = new Thread(this);
+    this.monitor.start();
+  }
+
+  public abstract void execute();
+
+  @Override
+  public void run() {
+    LOGGER.info("Setting up heartbeat on port '{}'", port);
+    DatagramPacket client = null;
+    try {
+      byte[] data = new byte[name.length()];
+      name.getBytes(0, name.length(), data, 0);
+      DatagramPacket pack =
+        new DatagramPacket(data, data.length, InetAddress.getLocalHost(), port);
+      while (!Thread.currentThread().isInterrupted()) {
+        LOGGER.trace("My heart is beating");
+        DatagramSocket ds = new DatagramSocket();
+        ds.send(pack);
+        ds.close();
+        Thread.sleep(heartBeatInterval);
+      }
+    } catch (IOException e) {
+      throw new IllegalStateException("Monitoring Thread for " + name + " could not communicate to socket", e);
+    } catch (InterruptedException e) {
+      throw new IllegalStateException("Monitoring Thread for " + name + " is interrupted ", e);
+    }
+    System.out.println("Closing  application");
+  }
+}
\ No newline at end of file
diff --git a/sonar-process/src/main/java/org/sonar/process/Runner.java b/sonar-process/src/main/java/org/sonar/process/Runner.java
deleted file mode 100644 (file)
index 089b16f..0000000
+++ /dev/null
@@ -1,88 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-package org.sonar.process;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.io.IOException;
-import java.net.DatagramPacket;
-import java.net.DatagramSocket;
-import java.net.InetAddress;
-
-/**
- * @Since 4.5
- */
-public abstract class Runner implements Runnable {
-
-  private final static Logger LOGGER = LoggerFactory.getLogger(Launcher.class);
-
-  final String name;
-  final int port;
-
-  public Runner(String name, int port) {
-    this.name = name;
-    this.port = port;
-  }
-
-  public abstract void execute();
-
-  @Override
-  public void run() {
-    DatagramPacket client = null;
-    try {
-      byte[] data = new byte[name.length()];
-      name.getBytes(0, name.length(), data, 0);
-      DatagramPacket pack =
-        new DatagramPacket(data, data.length, InetAddress.getLocalHost(), port);
-      while (true) {
-        DatagramSocket ds = new DatagramSocket();
-        ds.send(pack);
-        ds.close();
-        Thread.sleep(1000);
-      }
-    } catch (IOException e) {
-      throw new IllegalStateException("Monitoring Thread for " + name + " could not communicate to socket", e);
-    } catch (InterruptedException e) {
-      throw new IllegalStateException("Monitoring Thread for " + name + " is interrupted ", e);
-    }
-  }
-
-  public static void main(final String... args) {
-
-    Runner process = new Runner(args[0], Integer.parseInt(args[1])) {
-      @Override
-      public void execute() {
-        while (true) {
-          LOGGER.info("pseudo running for process {}", args[0]);
-          try {
-            Thread.sleep(1000);
-          } catch (InterruptedException e) {
-            e.printStackTrace();
-          }
-        }
-      }
-    };
-    Thread monitor = new Thread(process);
-    monitor.start();
-    process.execute();
-    monitor.interrupt();
-  }
-}
\ No newline at end of file
diff --git a/sonar-process/src/test/java/org/sonar/process/ProcessTest.java b/sonar-process/src/test/java/org/sonar/process/ProcessTest.java
new file mode 100644 (file)
index 0000000..d4ec56c
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.process;
+
+import org.junit.Test;
+
+import java.io.IOException;
+import java.net.DatagramPacket;
+import java.net.DatagramSocket;
+
+public class ProcessTest {
+
+
+  @Test(timeout = 5000L)
+  public void heart_beats() throws InterruptedException, IOException {
+
+    DatagramSocket socket = new DatagramSocket(0);
+    Process test = testProcess("test", socket);
+
+    int ping = 0;
+    while (ping < 3) {
+      DatagramPacket packet = new DatagramPacket(new byte[1024], 1024);
+      socket.receive(packet);
+      ping++;
+    }
+
+    socket.close();
+  }
+
+  private Process testProcess(final String name, final DatagramSocket socket) {
+    return new Process(name, socket.getLocalPort()) {
+      @Override
+      public void execute() {
+        try {
+          Thread.sleep(10000L);
+        } catch (InterruptedException e) {
+          e.printStackTrace();
+        }
+      }
+    };
+  }
+}
\ No newline at end of file
diff --git a/sonar-process/src/test/java/org/sonar/process/RunnerTest.java b/sonar-process/src/test/java/org/sonar/process/RunnerTest.java
deleted file mode 100644 (file)
index 26cbb6b..0000000
+++ /dev/null
@@ -1,24 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-package org.sonar.process;
-
-public class RunnerTest  {
-
-}
\ No newline at end of file