@Test public void forward_stream_to_log() { InputStream stream = IOUtils.toInputStream("one\nsecond log\nthird log\n"); Logger logger = mock(Logger.class); StreamGobbler gobbler = new StreamGobbler(stream, "WEB", logger); verifyZeroInteractions(logger); gobbler.start(); StreamGobbler.waitUntilFinish(gobbler); verify(logger).info("one"); verify(logger).info("second log"); verify(logger).info("third log"); verifyNoMoreInteractions(logger); } }
public void stopForcibly() { eventWatcher.interrupt(); stopWatcher.interrupt(); if (process != null) { process.destroyForcibly(); waitForDown(); process.closeStreams(); } if (stdOutGobbler != null) { StreamGobbler.waitUntilFinish(stdOutGobbler); stdOutGobbler.interrupt(); } if (stdErrGobbler != null) { StreamGobbler.waitUntilFinish(stdErrGobbler); stdErrGobbler.interrupt(); } lifecycle.tryToMoveTo(Lifecycle.State.STOPPED); }
static void waitUntilFinish(@Nullable StreamGobbler gobbler) { if (gobbler != null) { try { gobbler.join(); } catch (InterruptedException ignored) { // consider as finished, restore the interrupted flag Thread.currentThread().interrupt(); } } } }
public boolean start(Supplier<ProcessMonitor> commandLauncher) { if (!lifecycle.tryToMoveTo(Lifecycle.State.STARTING)) { // has already been started return false; } try { this.process = commandLauncher.get(); } catch (RuntimeException e) { LOG.error("Fail to launch process [{}]", processId.getKey(), e); lifecycle.tryToMoveTo(Lifecycle.State.STOPPED); throw e; } this.stdOutGobbler = new StreamGobbler(process.getInputStream(), processId.getKey()); this.stdOutGobbler.start(); this.stdErrGobbler = new StreamGobbler(process.getErrorStream(), processId.getKey()); this.stdErrGobbler.start(); this.stopWatcher.start(); this.eventWatcher.start(); // Could be improved by checking the status "up" in shared memory. // Not a problem so far as this state is not used by listeners. lifecycle.tryToMoveTo(Lifecycle.State.STARTED); return true; }
public boolean start(Supplier<ProcessMonitor> commandLauncher) { if (!lifecycle.tryToMoveTo(Lifecycle.State.STARTING)) { // has already been started return false; } try { this.process = commandLauncher.get(); } catch (RuntimeException e) { LOG.error("Fail to launch process [{}]", processId.getKey(), e); lifecycle.tryToMoveTo(Lifecycle.State.STOPPED); throw e; } this.stdOutGobbler = new StreamGobbler(process.getInputStream(), processId.getKey()); this.stdOutGobbler.start(); this.stdErrGobbler = new StreamGobbler(process.getErrorStream(), processId.getKey()); this.stdErrGobbler.start(); this.stopWatcher.start(); this.eventWatcher.start(); // Could be improved by checking the status "up" in shared memory. // Not a problem so far as this state is not used by listeners. lifecycle.tryToMoveTo(Lifecycle.State.STARTED); return true; }
public void stopForcibly() { eventWatcher.interrupt(); stopWatcher.interrupt(); if (process != null) { process.destroyForcibly(); waitForDown(); process.closeStreams(); } if (stdOutGobbler != null) { StreamGobbler.waitUntilFinish(stdOutGobbler); stdOutGobbler.interrupt(); } if (stdErrGobbler != null) { StreamGobbler.waitUntilFinish(stdErrGobbler); stdErrGobbler.interrupt(); } lifecycle.tryToMoveTo(Lifecycle.State.STOPPED); }
static void waitUntilFinish(@Nullable StreamGobbler gobbler) { if (gobbler != null) { try { gobbler.join(); } catch (InterruptedException ignored) { // consider as finished, restore the interrupted flag Thread.currentThread().interrupt(); } } } }