diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2019-05-09 11:17:15 -0500 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2019-06-03 20:21:22 +0200 |
commit | c3f0b65a4dbf26dc8f1071317a87b3c297cf8702 (patch) | |
tree | dae0b35c2c0bd4c4ca6f36189b097babd8aa96b4 /server/sonar-process/src | |
parent | 37fd266fb3d3fd1965580e1a8038f3ea9fe53b8a (diff) | |
download | sonarqube-c3f0b65a4dbf26dc8f1071317a87b3c297cf8702.tar.gz sonarqube-c3f0b65a4dbf26dc8f1071317a87b3c297cf8702.zip |
SONAR-12043 Minor refactor
Diffstat (limited to 'server/sonar-process/src')
-rw-r--r-- | server/sonar-process/src/main/java/org/sonar/process/ProcessEntryPoint.java | 58 | ||||
-rw-r--r-- | server/sonar-process/src/main/java/org/sonar/process/StopWatcher.java (renamed from server/sonar-process/src/main/java/org/sonar/process/AbstractStopWatcher.java) | 6 | ||||
-rw-r--r-- | server/sonar-process/src/test/java/org/sonar/process/StopWatcherTest.java (renamed from server/sonar-process/src/test/java/org/sonar/process/AbstractStopWatcherTest.java) | 8 | ||||
-rw-r--r-- | server/sonar-process/src/test/java/org/sonar/process/test/StandardProcess.java | 6 |
4 files changed, 24 insertions, 54 deletions
diff --git a/server/sonar-process/src/main/java/org/sonar/process/ProcessEntryPoint.java b/server/sonar-process/src/main/java/org/sonar/process/ProcessEntryPoint.java index 056e9dc54da..3d2e87d7efa 100644 --- a/server/sonar-process/src/main/java/org/sonar/process/ProcessEntryPoint.java +++ b/server/sonar-process/src/main/java/org/sonar/process/ProcessEntryPoint.java @@ -48,32 +48,27 @@ public class ProcessEntryPoint { private final ProcessCommands commands; private final SystemExit exit; private final StopWatcher stopWatcher; - private final HardStopWatcher hardStopWatcher; + private final StopWatcher hardStopWatcher; // new Runnable() is important to avoid conflict of call to ProcessEntryPoint#stop() with Thread#stop() - private final Thread shutdownHook = new Thread(new Runnable() { - @Override - public void run() { - exit.setInShutdownHook(); - stop(); - } - }); + private final Runtime runtime; private volatile Monitored monitored; private volatile StopperThread stopperThread; private volatile HardStopperThread hardStopperThread; ProcessEntryPoint(Props props, SystemExit exit, ProcessCommands commands) { - this(props, getProcessNumber(props), getSharedDir(props), exit, commands); + this(props, getProcessNumber(props), getSharedDir(props), exit, commands, Runtime.getRuntime()); } - private ProcessEntryPoint(Props props, int processNumber, File sharedDir, SystemExit exit, ProcessCommands commands) { + private ProcessEntryPoint(Props props, int processNumber, File sharedDir, SystemExit exit, ProcessCommands commands, Runtime runtime) { this.props = props; this.processKey = props.nonNullValue(PROPERTY_PROCESS_KEY); this.processNumber = processNumber; this.sharedDir = sharedDir; this.exit = exit; this.commands = commands; - this.stopWatcher = new StopWatcher(commands, this); - this.hardStopWatcher = new HardStopWatcher(commands, this); + this.stopWatcher = createStopWatcher(commands, this); + this.hardStopWatcher = createHardStopWatcher(commands, this); + this.runtime = runtime; } public ProcessCommands getCommands() { @@ -118,7 +113,10 @@ public class ProcessEntryPoint { private void launch(Logger logger) throws InterruptedException { logger.info("Starting {}", getKey()); - Runtime.getRuntime().addShutdownHook(shutdownHook); + runtime.addShutdownHook(new Thread(() -> { + exit.setInShutdownHook(); + stop(); + })); stopWatcher.start(); hardStopWatcher.start(); @@ -225,16 +223,12 @@ public class ProcessEntryPoint { return lifecycle.isCurrentState(candidateState); } - Thread getShutdownHook() { - return shutdownHook; - } - public static ProcessEntryPoint createForArguments(String[] args) { Props props = ConfigurationUtils.loadPropsFromCommandLineArgs(args); File sharedDir = getSharedDir(props); int processNumber = getProcessNumber(props); ProcessCommands commands = DefaultProcessCommands.main(sharedDir, processNumber); - return new ProcessEntryPoint(props, processNumber, sharedDir, new SystemExit(), commands); + return new ProcessEntryPoint(props, processNumber, sharedDir, new SystemExit(), commands, Runtime.getRuntime()); } private static int getProcessNumber(Props props) { @@ -248,35 +242,15 @@ public class ProcessEntryPoint { /** * This watchdog is looking for hard stop to be requested via {@link ProcessCommands#askedForHardStop()}. */ - private static class HardStopWatcher extends AbstractStopWatcher { - - private HardStopWatcher(ProcessCommands commands, ProcessEntryPoint processEntryPoint) { - super( - "HardStop Watcher", - () -> { - LoggerFactory.getLogger(HardStopWatcher.class).info("Hard stopping process"); - processEntryPoint.hardStopAsync(); - }, - commands::askedForHardStop); - } - + private static StopWatcher createHardStopWatcher(ProcessCommands commands, ProcessEntryPoint processEntryPoint) { + return new StopWatcher("HardStop Watcher", processEntryPoint::hardStopAsync, commands::askedForHardStop); } /** * This watchdog is looking for graceful stop to be requested via {@link ProcessCommands#askedForStop()} ()}. */ - private static class StopWatcher extends AbstractStopWatcher { - - private StopWatcher(ProcessCommands commands, ProcessEntryPoint processEntryPoint) { - super( - "Stop Watcher", - () -> { - LoggerFactory.getLogger(StopWatcher.class).info("Stopping process"); - processEntryPoint.stopAsync(); - }, - commands::askedForStop); - } - + private static StopWatcher createStopWatcher(ProcessCommands commands, ProcessEntryPoint processEntryPoint) { + return new StopWatcher("Stop Watcher", processEntryPoint::stopAsync, commands::askedForStop); } /** diff --git a/server/sonar-process/src/main/java/org/sonar/process/AbstractStopWatcher.java b/server/sonar-process/src/main/java/org/sonar/process/StopWatcher.java index 50e4e73940a..3e1af3b7b25 100644 --- a/server/sonar-process/src/main/java/org/sonar/process/AbstractStopWatcher.java +++ b/server/sonar-process/src/main/java/org/sonar/process/StopWatcher.java @@ -22,18 +22,18 @@ package org.sonar.process; import com.google.common.annotations.VisibleForTesting; import java.util.function.BooleanSupplier; -abstract class AbstractStopWatcher extends Thread { +public class StopWatcher extends Thread { private final Runnable stopCommand; private final BooleanSupplier shouldStopTest; private final long delayMs; private volatile boolean watching = true; - public AbstractStopWatcher(String threadName, Runnable stopCommand, BooleanSupplier shouldStopTest) { + public StopWatcher(String threadName, Runnable stopCommand, BooleanSupplier shouldStopTest) { this(threadName, stopCommand, shouldStopTest, 500L); } @VisibleForTesting - AbstractStopWatcher(String threadName, Runnable stopCommand, BooleanSupplier shouldStopTest, long delayMs) { + StopWatcher(String threadName, Runnable stopCommand, BooleanSupplier shouldStopTest, long delayMs) { super(threadName); this.stopCommand = stopCommand; this.shouldStopTest = shouldStopTest; diff --git a/server/sonar-process/src/test/java/org/sonar/process/AbstractStopWatcherTest.java b/server/sonar-process/src/test/java/org/sonar/process/StopWatcherTest.java index 8fbc9b2cefd..79561fdf967 100644 --- a/server/sonar-process/src/test/java/org/sonar/process/AbstractStopWatcherTest.java +++ b/server/sonar-process/src/test/java/org/sonar/process/StopWatcherTest.java @@ -32,7 +32,7 @@ import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -public class AbstractStopWatcherTest { +public class StopWatcherTest { @Rule public TestRule safeguardTimeout = new DisableOnDebug(Timeout.seconds(60)); @@ -44,8 +44,7 @@ public class AbstractStopWatcherTest { when(commands.askedForHardStop()).thenReturn(false, true); Stoppable stoppable = mock(Stoppable.class); - AbstractStopWatcher underTest = new AbstractStopWatcher("TheThreadName", - stoppable::hardStopAsync, commands::askedForHardStop, 1L) {}; + StopWatcher underTest = new StopWatcher("TheThreadName", stoppable::hardStopAsync, commands::askedForHardStop, 1L); underTest.start(); while (underTest.isAlive()) { @@ -61,8 +60,7 @@ public class AbstractStopWatcherTest { when(commands.askedForHardStop()).thenReturn(false); Stoppable stoppable = mock(Stoppable.class); - AbstractStopWatcher underTest = new AbstractStopWatcher("TheThreadName", - stoppable::hardStopAsync, commands::askedForHardStop, 1L) {}; + StopWatcher underTest = new StopWatcher("TheThreadName", stoppable::hardStopAsync, commands::askedForHardStop, 1L); underTest.start(); underTest.interrupt(); diff --git a/server/sonar-process/src/test/java/org/sonar/process/test/StandardProcess.java b/server/sonar-process/src/test/java/org/sonar/process/test/StandardProcess.java index 3f449d635fe..e360cd03605 100644 --- a/server/sonar-process/src/test/java/org/sonar/process/test/StandardProcess.java +++ b/server/sonar-process/src/test/java/org/sonar/process/test/StandardProcess.java @@ -36,11 +36,9 @@ public class StandardProcess implements Monitored { @Override public void run() { try { - while (true) { - Thread.sleep(100L); - } + Thread.sleep(Long.MAX_VALUE); } catch (InterruptedException e) { - Thread.currentThread().interrupt(); + interrupt(); } } }; |