/** * 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; }
public ControlServer() { this.controlFile = new File(UserConfig.getUserConfigDir(), CONTROL_FILE); this.controlFileTailer = new Tailer(controlFile, this, 1000, true); this.eventBus = LocalEventBus.getInstance(); }
@Test public void testLongFile() throws Exception { final long delay = 50; final File file = new File(getTestDirectory(), "testLongFile.txt"); createFile(file, 0); try (final Writer writer = new FileWriter(file, true)) { for (int i = 0; i < 100000; i++) { writer.write("LineLineLineLineLineLineLineLineLineLine\n"); } writer.write("SBTOURIST\n"); } final TestTailerListener listener = new TestTailerListener(); tailer = new Tailer(file, listener, delay, false); final long start = System.currentTimeMillis(); final Thread thread = new Thread(tailer); thread.start(); List<String> lines = listener.getLines(); while (lines.isEmpty() || !lines.get(lines.size() - 1).equals("SBTOURIST")) { lines = listener.getLines(); } System.out.println("Elapsed: " + (System.currentTimeMillis() - start)); listener.clear(); }
tailer = new Tailer(file, charsetUTF8, listener, delay, false, isWindows, 4096); final Thread thread = new Thread(tailer); thread.start();
@Test public void testBufferBreak() throws Exception { final long delay = 50; final File file = new File(getTestDirectory(), "testBufferBreak.txt"); createFile(file, 0); writeString(file, "SBTOURIST\n"); final TestTailerListener listener = new TestTailerListener(); tailer = new Tailer(file, listener, delay, false, 1); final Thread thread = new Thread(tailer); thread.start(); List<String> lines = listener.getLines(); while (lines.isEmpty() || !lines.get(lines.size() - 1).equals("SBTOURIST")) { lines = listener.getLines(); } listener.clear(); }
@Test public void testIO335() throws Exception { // test CR behaviour // Create & start the Tailer final long delayMillis = 50; final File file = new File(getTestDirectory(), "tailer-testio334.txt"); createFile(file, 0); final TestTailerListener listener = new TestTailerListener(); tailer = new Tailer(file, listener, delayMillis, false); final Thread thread = new Thread(tailer); thread.start(); // Write some lines to the file writeString(file, "CRLF\r\n", "LF\n", "CR\r", "CRCR\r\r", "trail"); final long testDelayMillis = delayMillis * 10; TestUtils.sleep(testDelayMillis); final List<String> lines = listener.getLines(); assertEquals("line count", 4, lines.size()); assertEquals("line 1", "CRLF", lines.get(0)); assertEquals("line 2", "LF", lines.get(1)); assertEquals("line 3", "CR", lines.get(2)); assertEquals("line 4", "CRCR\r", lines.get(3)); }
@Test public void testInterrupt() throws Exception { final File file = new File(getTestDirectory(), "nosuchfile"); assertFalse("nosuchfile should not exist", file.exists()); final TestTailerListener listener = new TestTailerListener(); // Use a long delay to try to make sure the test thread calls interrupt() while the tailer thread is sleeping. final int delay = 1000; final int idle = 50; // allow time for thread to work tailer = new Tailer(file, listener, delay, false, 4096); final Thread thread = new Thread(tailer); thread.setDaemon(true); thread.start(); TestUtils.sleep(idle); thread.interrupt(); TestUtils.sleep(delay + idle); assertNotNull("Missing InterruptedException", listener.exception); assertTrue("Unexpected Exception: " + listener.exception, listener.exception instanceof InterruptedException); 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 testTailerEndOfFileReached() throws Exception { // Create & start the Tailer final long delayMillis = 50; final long testDelayMillis = delayMillis * 10; final File file = new File(getTestDirectory(), "tailer-eof-test.txt"); createFile(file, 0); final TestTailerListener listener = new TestTailerListener(); final String osname = System.getProperty("os.name"); final boolean isWindows = osname.startsWith("Windows"); tailer = new Tailer(file, listener, delayMillis, false, isWindows); final Thread thread = new Thread(tailer); thread.start(); // write a few lines write(file, "line1", "line2", "line3"); TestUtils.sleep(testDelayMillis); // write a few lines write(file, "line4", "line5", "line6"); TestUtils.sleep(testDelayMillis); // write a few lines write(file, "line7", "line8", "line9"); TestUtils.sleep(testDelayMillis); // May be > 3 times due to underlying OS behaviour wrt streams assertTrue("end of file reached at least 3 times", listener.reachedEndOfFile >= 3); }
@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 testTailerEof() throws Exception { // Create & start the Tailer final long delay = 50; final File file = new File(getTestDirectory(), "tailer2-test.txt"); createFile(file, 0); final TestTailerListener listener = new TestTailerListener(); tailer = new Tailer(file, listener, delay, false); final Thread thread = new Thread(tailer); thread.start(); // Write some lines to the file writeString(file, "Line"); TestUtils.sleep(delay * 2); List<String> lines = listener.getLines(); assertEquals("1 line count", 0, lines.size()); writeString(file, " one\n"); TestUtils.sleep(delay * 2); lines = listener.getLines(); assertEquals("1 line count", 1, lines.size()); assertEquals("1 line 1", "Line one", lines.get(0)); listener.clear(); }
final String osname = System.getProperty("os.name"); final boolean isWindows = osname.startsWith("Windows"); tailer = new Tailer(file, listener, delayMillis, false, isWindows); final Thread thread = new Thread(tailer); thread.start();
@Override protected void doStart() { super.doStart(); Tailer theTailer = new Tailer(this.getFile(), this, this.pollingDelay, this.end, this.reopen); this.getTaskExecutor().execute(theTailer); this.tailer = theTailer; }
/** * Creates and starts a Tailer for the given file. * * @param file the file to follow. * @param listener the TailerListener to use. * @param delay 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. * @return The new tailer */ public static Tailer create(File file, TailerListener listener, long delay, boolean end) { Tailer tailer = new Tailer(file, listener, delay, end); Thread thread = new Thread(tailer); thread.setDaemon(true); thread.start(); return tailer; }
/** * Creates and starts a Tailer for the given 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. * @param end Set to true to tail from the end of the file, false to tail from the beginning of the file. * @param bufSize buffer size. * @return The new tailer */ public static Tailer create(File file, TailerListener listener, long delayMillis, boolean end, int bufSize) { Tailer tailer = new Tailer(file, listener, delayMillis, end, bufSize); Thread thread = new Thread(tailer); thread.setDaemon(true); thread.start(); return tailer; }
/** * Creates and starts a Tailer for the given 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. * @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(File file, TailerListener listener, long delayMillis, boolean end, boolean reOpen, int bufSize) { Tailer tailer = new Tailer(file, listener, delayMillis, end, reOpen, bufSize); Thread thread = new Thread(tailer); thread.setDaemon(true); thread.start(); return tailer; }
/** * 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; }
protected LogTailer(File toRead, long timeForFileToBeCreated) throws IOException { long requiredTime = System.currentTimeMillis() + timeForFileToBeCreated; while ((System.currentTimeMillis() < requiredTime) && !(toRead.exists() && toRead.canRead())) { try { Thread.sleep(10); } catch (InterruptedException e) { // ignore } } if (!toRead.exists() || !toRead.canRead()) { throw new IllegalStateException("Couldn't read "+toRead.getCanonicalPath()+" in the configured timeout"); } logger.debug("Initialising Tailer for "+toRead.getCanonicalPath()); tailer = new Tailer(toRead, this, DELAY, 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; }
@Autowired public TailService(@Value("${geoip2.cityfile}") String cityFile, @Value("${access.logs}") String accessLogs, SimpMessageSendingOperations messagingTemplate) { this.messagingTemplate = messagingTemplate; String databaseFile = cityFile; if (databaseFile != null) { Path database = Paths.get(databaseFile); if (Files.exists(database)) { try { this.reader = new DatabaseReader.Builder(database.toFile()).build(); } catch (IOException e) { LoggerFactory.getLogger(getClass()).error("GeoIPCityService init", e); } } } this.tailers = new ArrayList<>(); for (String logFile : accessLogs.split(",")) { Path p = Paths.get(logFile.trim()); this.tailers.add(new Tailer(p.toFile(), new ListenerAdapter())); } this.executor = Executors.newFixedThreadPool(this.tailers.size()); for (Tailer tailer : this.tailers) { this.executor.execute(tailer); } }