【问题标题】:File Tailing not working using TailerListenerAdapter of Apache Commons io文件拖尾无法使用 Apache Commons io 的 TailerListenerAdapter
【发布时间】:2017-09-29 09:02:02
【问题描述】:

我想使用 Java 跟踪文件内容。我尝试使用 Apache commons io 的 Tailer 和 TailerListenerAdapter。我在所需依赖项的类路径中包含了storm-core-1.1.1.jar。程序编译并运行;但是 TailerListenerAdapter 的 'handle' 方法根本没有被调用,并且执行卡在 main 方法中。以下是代码:

import org.apache.storm.shade.org.apache.commons.io.input.TailerListenerAdapter;
import org.apache.storm.shade.org.apache.commons.io.input.Tailer;
import org.apache.storm.shade.org.apache.commons.io.input.TailerListener;

import java.io.File;
public class LogTailTest {

    /**
     * TailerListener implementation.
     */
    static public class ShowLinesListener extends TailerListenerAdapter {
        @Override
        public void handle(String line) {
            System.out.println(line);
            System.out.println("inside handle");
        }
    }

    public static void main(String args[]) {


        TailerListener listener  = new ShowLinesListener();
        File file = new File("C:/LogFiles/Radius-log");
        System.out.println("inside main");
        Tailer tailer = Tailer.create(file, listener);
        tailer.run();

       }
}

【问题讨论】:

    标签: java multithreading apache-commons-io apache-tailer


    【解决方案1】:

    如果执行停留在 main 方法中,那么至少意味着它没有崩溃。 您可以通过在您的节目ShowLinesListener 中实现TailerListener 接口的其他方法来进一步了解正在发生的事情。有一些方法可以处理文件不存在、文件轮换、通用异常等。

    【讨论】:

      【解决方案2】:

      您不应该直接调用“tailer.run()”。而是这样做:

      TailerListener listener  = new ShowLinesListener();
      File file = new File("C:/LogFiles/Radius-log");
      System.out.println("inside main");
      Tailer tailer = Tailer.create(file, listener);
      Thread thread = new Thread(tailer);
      thread.setDaemon(true); // optional
      thread.start();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-25
        • 1970-01-01
        • 2013-01-04
        • 2019-05-06
        • 1970-01-01
        • 2011-08-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多