From: Stephane Gamard Date: Wed, 9 Jul 2014 14:11:00 +0000 (+0200) Subject: SONAR-5409 - Renamed to Process and moved heartbeat in CTor X-Git-Tag: 4.5-RC1~662 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=edaf2799933ba8fa46c58ca1abf50eaf673c7d62;p=sonarqube.git SONAR-5409 - Renamed to Process and moved heartbeat in CTor --- diff --git a/sonar-process/pom.xml b/sonar-process/pom.xml index 72a440e1f59..8707e09fdf1 100644 --- a/sonar-process/pom.xml +++ b/sonar-process/pom.xml @@ -14,19 +14,22 @@ SonarQube :: Process + - ch.qos.logback - logback-access + org.slf4j + slf4j-api + ch.qos.logback logback-classic + - ch.qos.logback - logback-core + junit + junit + test - org.easytesting fest-assert diff --git a/sonar-process/src/main/java/org/sonar/process/Launcher.java b/sonar-process/src/main/java/org/sonar/process/Launcher.java index 8c7ed2e8b71..96fee525fdc 100644 --- a/sonar-process/src/main/java/org/sonar/process/Launcher.java +++ b/sonar-process/src/main/java/org/sonar/process/Launcher.java @@ -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 index 00000000000..3e493ec08c5 --- /dev/null +++ b/sonar-process/src/main/java/org/sonar/process/Process.java @@ -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 index 089b16fc709..00000000000 --- a/sonar-process/src/main/java/org/sonar/process/Runner.java +++ /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 index 00000000000..d4ec56c7c91 --- /dev/null +++ b/sonar-process/src/test/java/org/sonar/process/ProcessTest.java @@ -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 index 26cbb6b80dd..00000000000 --- a/sonar-process/src/test/java/org/sonar/process/RunnerTest.java +++ /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