this.getTaskExecutor().execute(() -> { Process process = OSDelegatingFileTailingMessageProducer.this.nativeTailProcess; if (process == null) { Thread.currentThread().interrupt(); logger.error("Interrupted - stopping adapter", e); stop(); destroyProcess(); if (isRunning()) { if (logger.isInfoEnabled()) { logger.info("Restarting tail process in " + getMissingFileDelay() + " milliseconds"); getTaskScheduler() .schedule(this::runExec, new Date(System.currentTimeMillis() + getMissingFileDelay()));
@Override protected void doStart() { super.doStart(); destroyProcess(); this.command = "tail " + this.options + " " + this.getFile().getAbsolutePath(); this.getTaskExecutor().execute(this::runExec); }
/** * Exec the native tail process. */ private void runExec() { this.destroyProcess(); if (logger.isInfoEnabled()) { logger.info("Starting tail process"); } try { Process process = Runtime.getRuntime().exec(this.command); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); this.nativeTailProcess = process; this.startProcessMonitor(); if (this.enableStatusReader) { startStatusReader(); } this.stdOutReader = reader; this.getTaskExecutor().execute(this); } catch (IOException e) { throw new MessagingException("Failed to exec tail command: '" + this.command + "'", e); } }
/** * Reads lines from stdout and sends in a message to the output channel. */ @Override public void run() { String line; try { if (logger.isDebugEnabled()) { logger.debug("Reading stdout"); } while ((line = this.stdOutReader.readLine()) != null) { this.send(line); } } catch (IOException e) { if (logger.isDebugEnabled()) { logger.debug("Exception on tail reader", e); } try { this.stdOutReader.close(); } catch (IOException e1) { if (logger.isDebugEnabled()) { logger.debug("Exception while closing stdout", e); } } this.destroyProcess(); } }
@Test @TailAvailable public void canRecalculateCommandWhenFileOrOptionsChanged() throws IOException { File firstFile = File.createTempFile("first", ".txt"); String firstOptions = "-f options"; File secondFile = File.createTempFile("second", ".txt"); String secondOptions = "-f newoptions"; OSDelegatingFileTailingMessageProducer adapter = new OSDelegatingFileTailingMessageProducer(); adapter.setFile(firstFile); adapter.setOptions(firstOptions); adapter.setOutputChannel(new QueueChannel()); adapter.setTailAttemptsDelay(500); adapter.setBeanFactory(mock(BeanFactory.class)); adapter.afterPropertiesSet(); adapter.start(); assertEquals("tail " + firstOptions + " " + firstFile.getAbsolutePath(), adapter.getCommand()); adapter.stop(); adapter.setFile(secondFile); adapter.start(); assertEquals("tail " + firstOptions + " " + secondFile.getAbsolutePath(), adapter.getCommand()); adapter.stop(); adapter.setOptions(secondOptions); adapter.start(); assertEquals("tail " + secondOptions + " " + secondFile.getAbsolutePath(), adapter.getCommand()); adapter.stop(); }
public class MyApplicationListener implements ApplicationListener<ContextRefreshedEvent> { @Autowired @Qualifier("outputChannel") private SubscribableChannel outputChannel; @Override public void onApplicationEvent(ContextRefreshedEvent event) { OSDelegatingFileTailingMessageProducer tailer = new OSDelegatingFileTailingMessageProducer(); tailer.setOutputChannel(outputChannel); tailer.setFile(new File("/file/to/tail.txt")); tailer.setOptions("-f -n 0"); tailer.afterPropertiesSet(); tailer.start(); } }
FileTailingMessageProducerSupport adapter; if (this.delay == null && this.end == null && this.reopen == null) { adapter = new OSDelegatingFileTailingMessageProducer(); ((OSDelegatingFileTailingMessageProducer) adapter).setEnableStatusReader(this.enableStatusReader); if (this.nativeOptions != null) { ((OSDelegatingFileTailingMessageProducer) adapter).setOptions(this.nativeOptions);
@Test public void testDefault() { String fileName = TestUtils.getPropertyValue(defaultAdapter, "file", File.class).getAbsolutePath(); String normalizedName = getNormalizedPath(fileName); assertEquals("/tmp/baz", normalizedName); assertEquals("tail -F -n 0 " + fileName, TestUtils.getPropertyValue(defaultAdapter, "command")); assertSame(exec, TestUtils.getPropertyValue(defaultAdapter, "taskExecutor")); assertTrue(TestUtils.getPropertyValue(defaultAdapter, "autoStartup", Boolean.class)); assertTrue(TestUtils.getPropertyValue(defaultAdapter, "enableStatusReader", Boolean.class)); assertEquals(123, TestUtils.getPropertyValue(defaultAdapter, "phase")); assertSame(this.tailErrorChannel, TestUtils.getPropertyValue(defaultAdapter, "errorChannel")); this.defaultAdapter.stop(); this.defaultAdapter.setOptions("-F -n 6"); this.defaultAdapter.start(); assertEquals("tail -F -n 6 " + fileName, TestUtils.getPropertyValue(defaultAdapter, "command")); }
@Test @TailAvailable public void testOS() throws Exception { OSDelegatingFileTailingMessageProducer adapter = new OSDelegatingFileTailingMessageProducer(); adapter.setOptions(TAIL_OPTIONS_FOLLOW_NAME_ALL_LINES); testGuts(adapter, "stdOutReader"); }
@Override protected void doStop() { super.doStop(); destroyProcess(); }