线路完成之前BufferedReader readLine [英] BufferedReader readLine before the line is complete

查看:204
本文介绍了线路完成之前BufferedReader readLine的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个进程(实际上是一个Web服务器),它写入日志文件作为来自客户端的POST。



然后我有一个单独的进程读取循环中的(tail -f)日志文件:

  BufferedReader reader = new BufferedReader(new FileReader(logFilePath)); 
String line = null;

while(true)
{
if((line = reader.readLine())!= null)
{
if(firstLine == null)firstLine = line;
processLine(line);
继续;
}

尝试
{
Thread.sleep(sleepTime);
long ts = System.currentTimeMillis();
} catch(InterruptedException e){
Thread.currentThread()。interrupt();
休息;
}
}

99%的情况下这种情况非常有效,我通过BufferedReader.readLine读入的行不是一个完整的行。我想这是因为作者(网络服务器)尚未完成将该行的所有字节刷新到文件中。



有任何建议要解决或解决这个问题吗?非常感谢!

解决方案

使用过几乎同时需要访问一个文件的多个进程(通常是一个进程权限)接下来),我发现(和我曾经合作过的其他开发人员)一个预告片文件可以用来指示一个进程何时完成与另一个进程需要访问的文件进行交互。 / p>

我不确定您的网络服务器写入此日志的频率以及您的Java进程需要多快阅读它,但是如果有时间可能会在每个进程之间失效访问该文件,可以写出一个预告片文件,指示您的网络服务器何时写入该文件。您的Java进程可以首先检查预告片文件是否存在,然后在找到它,删除然后从日志中读取。



否则,您可能需要使用OS命令来找出你的网络服务器是否正在写入日志,而只是在没有的情况下运行日志阅读器。 http://blog.bensmann.com/executing-operating-system-commands 有一些关于用Java执行OS命令的好信息。



底线是否需要建立某种固定的差距,这样这些进程不会读/写同时到日志。


I have a process (actually a webserver) that writes to a log file as the POST coming in from clients.

Then I have a separate process that reads the log file like (tail -f) in a loop:

            BufferedReader reader = new BufferedReader(new FileReader(logFilePath));
            String line = null;

            while (true)
            {
                    if ( (line = reader.readLine()) != null )
                    {
                            if (firstLine == null) firstLine = line;
                            processLine(line);
                            continue;
                    }

                    try
                    {
                            Thread.sleep(sleepTime);
                            long ts = System.currentTimeMillis();
                    } catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                            break;
                    }
            }

This works very well 99% of the time but occasionally, the line I read in by BufferedReader.readLine isn't a complete line. I suppose that's because the writer (the webserver) hasn't finished flushing all the bytes of the line to the file yet.

Any suggestions to fix or work around this? Thanks a lot!

解决方案

Having worked with multiple processes that need to access one file at about the same time (often one process right after another), I've found (along with other developers I've worked with) that a "trailer" file is good to use to indicate when one process is done interacting with the file that needs to be accessed by another process.

I'm not sure how often your webserver is writing to this log and also how quickly your Java process needs to read it, but if there's time that can lapse between each process's access to the file, a trailer file can be written out that indicates when your webserver is done writing to the file. Your Java process can first check if the trailer file exists and then upon finding it, delete and then read from the log.

Otherwise, you might need to use OS commands to find out if your webserver is writing to the log and only run the log reader when it's not. http://blog.bensmann.com/executing-operating-system-commands has some good info on executing OS commands in Java.

Bottom line is there needs to be a solid gap of some sort established such that these processes aren't reading/writing to the log at the same time.

这篇关于线路完成之前BufferedReader readLine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆