@After public void tearDown() throws Exception { if (tailer != null) { tailer.stop(); } }
@Override public void handle(String command) { try { ControlCommand controlCommand = ControlCommand.valueOf(command.trim().toUpperCase()); switch (controlCommand) { case SHUTDOWN: logger.log(Level.INFO, "Control file: Received shutdown command. Shutting down."); eventBus.post(controlCommand); controlFileTailer.stop(); break; case RELOAD: logger.log(Level.INFO, "Control file: Received reload command. Reloading config ..."); eventBus.post(controlCommand); break; default: throw new RuntimeException("This command should have been handled."); } } catch (Exception e) { logger.log(Level.WARNING, "Control file: Ignoring unknown command: " + command, e); } }
listener.handle(e); stop();
@Test public void testHandleShutdown() { ctrlServer.handle("SHUTDOWN"); verifyZeroInteractions(ctrlFile); verify(eventBus).post(ControlCommand.SHUTDOWN); verifyNoMoreInteractions(eventBus); verify(ctrlFileTailer).stop(); verifyNoMoreInteractions(ctrlFileTailer); }
@Test public void testStopWithNoFileUsingExecutor() throws Exception { final File file = new File(getTestDirectory(),"nosuchfile"); assertFalse("nosuchfile should not exist", file.exists()); final TestTailerListener listener = new TestTailerListener(); final int delay = 100; final int idle = 50; // allow time for thread to work tailer = new Tailer(file, listener, delay, false); final Executor exec = new ScheduledThreadPoolExecutor(1); exec.execute(tailer); TestUtils.sleep(idle); tailer.stop(); TestUtils.sleep(delay+idle); assertNull("Should not generate Exception", listener.exception); assertEquals("Expected init to be called", 1 , listener.initialised); assertTrue("fileNotFound should be called", listener.notFound > 0); assertEquals("fileRotated should be not be called", 0 , listener.rotated); assertEquals("end of file never reached", 0, listener.reachedEndOfFile); }
@Test public void testStopWithNoFile() throws Exception { final File file = new File(getTestDirectory(),"nosuchfile"); assertFalse("nosuchfile should not exist", file.exists()); final TestTailerListener listener = new TestTailerListener(); final int delay = 100; final int idle = 50; // allow time for thread to work tailer = Tailer.create(file, listener, delay, false); TestUtils.sleep(idle); tailer.stop(); TestUtils.sleep(delay+idle); assertNull("Should not generate Exception", listener.exception); assertEquals("Expected init to be called", 1 , listener.initialised); assertTrue("fileNotFound should be called", listener.notFound > 0); assertEquals("fileRotated should be not be called", 0 , listener.rotated); assertEquals("end of file never reached", 0, listener.reachedEndOfFile); }
@Override protected void doStop() { super.doStop(); this.tailer.stop(); }
/** * Allows the tailer to complete its current loop and return. */ @Override public void stop() { super.stop(); this.run = false; }
@SuppressWarnings("NullableProblems") @Override public void onFailure(Throwable t) { tailer.stop(); } }
/** * Stop and cleanup itself!. * */ public void stop() { LOG.info("Stop Tailer"); if (this.tailer != null) { this.tailer.stop(); } this.tailer = null; }
@Override public void onSuccess(@Nullable Object result) { tailer.stop(); }
@PreDestroy public void preDestroy() { if (this.tailers != null) { for (Tailer tailer : this.tailers) { tailer.stop(); } } if (this.executor != null) { this.executor.shutdown(); } }
/** * Stops the tailer with an exception * @param e The exception to send to listener */ private void stop(final Exception e) { listener.handle(e); stop(); }
@Override protected void onLastClientDisconnect() { logger().info("Stopping to tail {}", settings.get(SETTINGS_PATH)); tailer.stop(); tailerRunning = false; }
public class LogTailTest { /** * TailerListener implementation. */ static public class ShowLinesListener extends TailerListenerAdapter { @Override public void handle(String line) { System.out.println(line); } } public static void main(String args[]) { TailerListener listener = new ShowLinesListener(); File file = new File("./test.log"); Tailer tailer = new Tailer(file, listener, 1000); tailer.run(); try { Thread.sleep(100000); } catch(InterruptedException ex) { Thread.currentThread().interrupt(); } tailer.stop(); }
@Override public void stream(OutputStream output) throws IOException { this.servletOutput = output; Tailer tailer = null; try { if (newTailAllowed()) { tailer = Tailer.create(logFile.getFile(), this, TAIL_DELAY_MILLIS, true); appendLine("Tailing " + logFile.getName()); keepRunningWhileOpen(); } } finally { IOUtils.closeQuietly(output); if (tailer != null) { tailer.stop(); log.info("Stopped tail on " + logFile.getName()); runningTails.decrementAndGet(); } } }
listener.handle(e); stop();
addTailers(stderrListeners, stderrFile, future); boolean foundListeningLine = Uninterruptibles.awaitUninterruptibly(listeningLatch, replayManagerConfig.serverReadinessTimeoutMillis, TimeUnit.MILLISECONDS); listeningWatch.stop(); if (!foundListeningLine) { throw new ServerFailedToStartException("timed out while waiting for server to start", stdoutFile, stderrFile);
tailer.stop();