@After public void tearDown() throws Exception { if (tailer != null) { tailer.stop(); } }
/** * Creates and starts a Tailer for the given file, starting at the beginning of the file * * @param file the file to follow. * @param listener the TailerListener to use. * @param delayMillis the delay between checks of the file for new content in milliseconds. * @return The new tailer */ public static Tailer create(final File file, final TailerListener listener, final long delayMillis) { return create(file, listener, delayMillis, false); }
/** * Creates and starts a Tailer for the given file. * * @param file the file to follow. * @param charset the character set to use for reading the file * @param listener the TailerListener to use. * @param delayMillis the delay between checks of the file for new content in milliseconds. * @param end Set to true to tail from the end of the file, false to tail from the beginning of the file. * @param reOpen whether to close/reopen the file between chunks * @param bufSize buffer size. * @return The new tailer */ public static Tailer create(final File file, final Charset charset, final TailerListener listener, final long delayMillis, final boolean end, final boolean reOpen ,final int bufSize) { final Tailer tailer = new Tailer(file, charset, listener, delayMillis, end, reOpen, bufSize); final Thread thread = new Thread(tailer); thread.setDaemon(true); thread.start(); return tailer; }
long position = 0; // position within the file while (getRun() && reader == null) { try { reader = new RandomAccessFile(file, RAF_MODE); while (getRun()) { final boolean newer = FileUtils.isFileNewer(file, last); // IO-279, must be done first readLines(save); } catch (final IOException ioe) { listener.handle(ioe); position = readLines(reader); last = file.lastModified(); } else if (newer) { position = readLines(reader); last = file.lastModified(); if (getRun() && reOpen) { reader = new RandomAccessFile(file, RAF_MODE); reader.seek(position); listener.handle(e); stop();
@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); }
@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); }
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(); }
public void enterLoop() throws IOException, ServiceAlreadyStartedException { File userAppDir = UserConfig.getUserConfigDir(); userAppDir.mkdirs(); controlFile.delete(); controlFile.createNewFile(); controlFile.deleteOnExit(); logger.log(Level.INFO, "Monitoring control file for commands at " + controlFile + " ..."); logger.log(Level.INFO, " (Note: This is a blocking operation. The 'main' thread is now blocked until '" + ControlCommand.SHUTDOWN + "' is received.)"); controlFileTailer.run(); // This blocks! }
int num; boolean seenCR = false; while (getRun() && ((num = reader.read(inbuf)) != EOF)) { for (int i = 0; i < num; i++) { final byte ch = inbuf[i];
@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(); } } }
while (getRun() && reader == null) { try { reader = new RandomAccessFile(file, RAF_MODE); while (getRun()) { final boolean newer = FileUtils.isFileNewer(file, last); // IO-279, must be done first readLines(save); } catch (IOException ioe) { listener.handle(ioe); position = readLines(reader); last = file.lastModified(); } else if (newer) { position = readLines(reader); last = file.lastModified(); if (getRun() && reOpen) { reader = new RandomAccessFile(file, RAF_MODE); reader.seek(position); stop(e); } catch (final Exception e) { stop(e); } finally { IOUtils.closeQuietly(reader);
@Test public void testEnterLoop() throws IOException, ServiceAlreadyStartedException { // invoke method ctrlServer.enterLoop(); // verify interactions verify(ctrlFile).delete(); verify(ctrlFile).createNewFile(); verify(ctrlFile).deleteOnExit(); verify(ctrlFileTailer).run(); }
int num; boolean seenCR = false; while (getRun() && ((num = reader.read(inbuf)) != EOF)) { for (int i = 0; i < num; i++) { final byte ch = inbuf[i];
/** * Creates and starts a Tailer for the given file, starting at the beginning of the file * with the default delay of 1.0s * * @param file the file to follow. * @param listener the TailerListener to use. * @return The new tailer */ public static Tailer create(final File file, final TailerListener listener) { return create(file, listener, DEFAULT_DELAY_MILLIS, false); }
final Tailer listeningWatch = Tailer.create(stdoutFile, new TailerListenerAdapter(){ @Override public void handle(String line) { 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);
@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); } }
public ControlServer() { this.controlFile = new File(UserConfig.getUserConfigDir(), CONTROL_FILE); this.controlFileTailer = new Tailer(controlFile, this, 1000, true); this.eventBus = LocalEventBus.getInstance(); }
long position = 0; // position within the file while (getRun() && reader == null) { try { reader = new RandomAccessFile(file, RAF_MODE); while (getRun()) { final boolean newer = FileUtils.isFileNewer(file, last); // IO-279, must be done first readLines(save); } catch (final IOException ioe) { listener.handle(ioe); position = readLines(reader); last = file.lastModified(); } else if (newer) { position = readLines(reader); last = file.lastModified(); if (getRun() && reOpen) { reader = new RandomAccessFile(file, RAF_MODE); reader.seek(position); listener.handle(e); stop();