in java taillistener, how to handle more log files

混江龙づ霸主 提交于 2020-01-15 09:15:51

问题


I am using java taillistener to monitor my log files.Whenever log files are updated,it will print the log message.when motoring one or two log files,it working fine.But when trying to monitoring more file(say 10 files),there is no messages displayed in console even logs are updated in log file.My code is given below.

ScheduledThreadPoolExecutor logMonitorThreadPoolExec;

if (listOfFiles[i].isFile()) 
{
 files = listOfFiles[i].getName();
 File pcounter_log = new File(files);                                
 Tailer logMessages = new Tailer(pcounter_log, new FileListener(files,element.getLogPattern()),
                                        5000, true);
 logMonitorThreadPoolExec.scheduleWithFixedDelay(logMessages, 5, 20,
                        TimeUnit.SECONDS);

}

public class FileListener extends TailerListenerAdapter {

 private final String fileName;

 public FileListener(String fileName, ArrayList<String> pattern) {
    this.fileName = fileName;
 }
  public void handle(String line) {

    System.out.println(fileName+"<---->"+line); 
    }
}

Can u please help me to handle this?


回答1:


I think that the problem is that you are using the Tailer the wrong way.

You are trying to use the Tailer using the thread pool of an executor service. But a Tailer has the property that it won't exit from it's run() method until something externally calls Tailer.stop(). And in your code, that's not going to happen.

Worse still, you are using a ScheduledThreadPoolExecutor, and telling it to start a new Tailer thread every 20 seconds!

So what will happen is that the first N Tailer runs scheduled will each grab one of the executor service's threads ... and hang onto it forever. When all threads are in use, the Executor will wait for one of the threads to finish ... and that won't happen.

The solution is to run each Tailer instance in its own dedicated Thread. You shouldn't try to use a Thread from a finite thread pool because you'll exhaust the pool. And you shouldn't try to use an executor service, for basically the same reason.


If using dedicated threads doesn't work, I'm out of ideas. You'll need to take a look at the Tailer code yourself and/or run your application under a debugger so that you can see what the Tailer threads are actually doing.



来源:https://stackoverflow.com/questions/6039923/in-java-taillistener-how-to-handle-more-log-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!