1

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

       }
}

2 Answers2

0

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.

Adam Kotwasinski
  • 4,377
  • 3
  • 17
  • 40
0

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();
Steve F
  • 33
  • 4