2

I have a Java server that appends certain information from multiple clients to a single file. So I am creating a FileWriter with append=true, and a PrintWriter with autoFlush=true. However, when I monitor (E.g., using the tail command) the file, the content is not immediately written out, usually there is 10-30 seconds delay. The server is running on Linux with JDK 1.7 64b. Why does autoflush not work here? How to make the data immediately available in the file?

//Create appendable FileWriter, and autoflushable PrintWriter
PrintWriter pw = new PrintWriter(new FileWriter(file, true), true);

... ...

if (save2File) {
    //println(String) supposed to flush the content
    pw.println(getEventOutput(event));
    // adding flush still not working!
    pw.flush();
}

Here is the code to reproduce the issue on. On Linux there is a noticeable delay (~1-2s) after the app output the line, and before the line is viewable in another terminal (using tail -f). On windows, it seems the output is pretty fast (faster than I am able to refresh the file in Notepad++)

public static void main(String[] args) throws IOException,
        InterruptedException {
    long time = 5000;
    File file = new File("xyz.test");
    PrintWriter pw = new PrintWriter(new FileWriter(file, true), true);

    for (int i = 0; i < 10; ++i) {
        pw.println("this is line " + i);
        System.out.println("output line " + i);
        // Thread.sleep(time);
        System.in.read();
    }
    System.in.read();
}

Update 2012-12-04:

I think I find the root cause of the problem. It's actually the async /nfs mount on Linux. The directory the server write file to is mounted as async nfs

...   ...   nfs   vers=3,async,rw,rsize=32768,wsize=32768 0 0

After I switch the file to /tmp, the file appending is immediate now.

fivelements
  • 1,487
  • 2
  • 19
  • 32
  • Perhaps the delay is due to this call `getEventOutput`? – nullpotent Nov 24 '12 at 22:46
  • are you using close() at the end, maybe file is still open by the another thread and you are still using system resources for it? – HRgiger Nov 24 '12 at 22:49
  • GetEventOutput has no delay. In fact, the client has received confirmation on the event. And that's why I know the file should have the content. – fivelements Nov 25 '12 at 04:56
  • Since there is one file to write to, and it is run at the server, I keep the printwriter open, until server shut down. In the test environment, I only have one client, thus have single thread to trigger the event. Also, I only use println(), which should be thread safe. – fivelements Nov 25 '12 at 05:02
  • 2
    Are you aware that `tail` itself does poll (i.e. it only checks ever so often if new data is available)? **That** might very well be the source of the delay that you see. – Joachim Sauer Nov 26 '12 at 14:32
  • I understand tail is using poll. But on the same linux box, I assume it should not poll longer than 10s. This is what happens when I run the code in the server (file flushed 10-30s later). – fivelements Nov 26 '12 at 15:35

0 Answers0