本文介绍了在java taillistener中,如何处理更多日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java taillistener监视我的日志文件.每当更新日志文件时,它将打印日志消息.当启动一个或两个日志文件时,它可以正常工作.但是当尝试监视更多文件时(例如10个文件) ),即使日志在日志文件中更新,控制台上也没有消息显示.我的代码如下.

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;

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?

推荐答案

我认为问题是您使用拖尾的方式错误.

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

您正在尝试通过执行程序服务的线程池使用拖尾程序.但是,拖尾具有以下特性:在外部调用Tailer.stop()之前,它不会从run()方法中退出.而且在您的代码中,这不会发生.

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.

更糟糕的是,您正在使用ScheduledThreadPoolExecutor,并告诉它每20秒启动一个新的Tailer线程!

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

因此,将发生的事情是,计划的前N个Tailer运行将各自抓住执行者服务的一个线程...并永远挂在其上.当所有线程都在使用中时,执行器将等待其中一个线程完成……而这不会发生.

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.

解决方案是在每个专用的线程中运行每个Tailer实例.您不应该尝试使用有限线程池中的线程,因为您将耗尽该池.而且出于相同的原因,您不应该尝试使用执行程序服务.

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.

如果使用专用线程无效,那么我就没主意了.您需要自己看一下Tailer代码和/或在调试器下运行您的应用程序,以便可以看到Tailer线程的实际作用.

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.

这篇关于在java taillistener中,如何处理更多日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 21:15