简体   繁体   中英

File Tailing not working using TailerListenerAdapter of Apache Commons io

I want to tail file contents using Java.I tried using Tailer and TailerListenerAdapter of Apache commons io. I have included storm-core-1.1.1.jar in the classpath for the required dependencies.Program compiles and runs; But the 'handle' method of TailerListenerAdapter is not called at all and the execution gets stuck inside the main method.Following is the code:

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();

       }
}

If the execution stays in the main method, then it at least means that it did not crash. You can get further insight what is happening by implementing other methods of TailerListener interface in your show ShowLinesListener . There are methods to handle file being not present, file rotationg, generic exceptions etc.

You shouldn't call "tailer.run()" directly. Instead do:

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM