<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>
}
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() {
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();
--- /dev/null
+/*
+ * 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
+++ /dev/null
-/*
- * 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
--- /dev/null
+/*
+ * 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
+++ /dev/null
-/*
- * 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