3

I have implement a tailerListener in my program but when I start it, it never stop.

Here is my program:

public class PostClient{

private static File file = new File("../file.txt");

public static void main(String [] args){    

//      TAILER LISTENER
    TailerListenerAdapter listener = new MyTailerListener();
    Tailer tailer = new Tailer(file, listener, 500);

    Executor executor = new Executor() {
          public void execute(Runnable command) {
              command.run();
           }
    };

    System.out.println("Execution of the tailer");
    executor.execute(tailer);
    System.out.println("Stop tailer");
    tailer.stop(); 

with my class MyTailerListener

import org.apache.commons.io.input.TailerListenerAdapter;

public class MyTailerListener extends TailerListenerAdapter {
    public void handle(String line) {
        System.out.println(line);
    }
}

At the beginning, I managed to go to the tailer.stop (so my program stopped, great) but after write some other lines and touch to several things, it did not work any more.

Strange thing, when I replace my class MyTailerListener by:

public void handle(String line) {
    final String logEntryPattern = "(\\w+\\s+\\d+\\s+\\d{2}:\\d{2}:\\d{2})\\s+(\\S+)\\s+(\\S+):\\s+(.+)";
    final Pattern p = Pattern.compile(logEntryPattern);
    final Matcher matcher = p.matcher(line);

System.out.println("Total groups: " + matcher.groupCount());
System.out.println("Date&Time: " + matcher.group(1));
System.out.println("Hostname: " + matcher.group(2));
System.out.println("Program Name: " + matcher.group(3));
System.out.println("Log: " + matcher.group(4));
}

I have just pick it out from an answer, but it does not concern my file. Then my program stop…

I think it is because of it can’t find the matcher.group(1). When I remove all the sysout but the first, my program does not stop.

Chènevis
  • 513
  • 1
  • 9
  • 22

1 Answers1

3

The way you implemented the Executor, the tailer gets run in the same thread. What you probably want to do is create a new thread to execute MyTailerListener in. Try eg.

Thread tailerThread=new Thread(tailer);
tailerThread.start();
System.out.println("Stop tailer");
tailerThread.stop();

Note however, that use of Thread.stop() is generally discouraged because it does not allow threads to terminate cleanly. Also, in this particular example the thread might be terminated before it does any work at all. You probably don't want that. Another quick hack would be to wait a second before stopping the tailer (eg. Thread.sleep(1000);), but you should really define when (under which conditions) the program should be stopped and implement that cleanly instead.

emu
  • 399
  • 2
  • 6
  • Thank. I think I don’t want to use thread because I do not really understand them. I saw on this page https://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/input/Tailer.html that I could use "executor" instead of the threads ; but maybe it is also a thread. – Chènevis Jul 21 '15 at 07:41
  • Tailer implements the Runnable interface and that indicates that it's meant to be run in a Thread of its own. Also, the command "tail -f" (which Tailer mimics according to that javadoc) never terminates on its own. The user needs to kill that process (typically Ctrl-C on a Unix shell) to end it. So it make perfect sense, that your program would behave in the same way. Maybe you should clarify what your program is actually supposed to do. – emu Jul 21 '15 at 09:43
  • Ok, I have change the purpose of my program. Now it watch the file flow and gave it to a server. So the way it don’t stop is useful for me. Thank you for your help. – Chènevis Jul 21 '15 at 14:13