0

We have several Java processes that write to one text file from time to time. These are not threads of the same virtual machine, but separate java.exe processes that are running from command line. These processes write to the same log file. We used canWrite...

while (!Log.canWrite()) {
    System.out.println("File: " + LogPath + " is locked, waiting...");
    Thread.sleep(2000);
}

... but it seems don't work. We get the following error:

The process cannot access the file because it is being used by another process) at java.io.FileOutputStream.openAppend(Native Method).

The Question:

what are the best practices for organizing semaphors in sutiations like this? It would be great if the solution wasn't too resource consuming, thus hugely affecting the total performance.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Racoon
  • 951
  • 3
  • 14
  • 32
  • I would strongly recommend using different log files for different processes. Or are you using this file to communicate between the processes? – Wim Deblauwe Jan 10 '13 at 16:06
  • No, I'm using this file as an activity log for all processes. If I used different files it would be hard to merge them into one whole... – Racoon Jan 10 '13 at 16:14
  • You can use http://vigilog.sourceforge.net/ to merge multiple log files into one for viewing. This is a tool I created and no longer actively supported, but it should work normally. Maybe there are better tools out there currently. – Wim Deblauwe Jan 11 '13 at 14:59

1 Answers1

3

You can synchronize access to the file using FileChannel.lock(). You can obtain a FileChannel from FileOutputStream.

EDIT But in your case it will be solving a wring problem. What you need is a centralized logging server. Refer to this question for more details: Centralised Java Logging

Community
  • 1
  • 1
Denis Tulskiy
  • 19,012
  • 6
  • 50
  • 68
  • Centralised Java Logging looks too serious to use it here. Briefly why we need semaphors: we run four java-based Fitnesse autotests simultaneously and track results in one text file. We could write to 4 different files but it would be hard to merge them. Here, Centralised Java Logging with SoccerAppender looks too complex solution... – Racoon Jan 10 '13 at 17:44
  • @Racoon: messing with file locks is almost guaranteed to cause you lots of headache. Are these processes continually running or do they all finish and produce 4 log files? If they finish and you need to analyse logs, then I'd go with merging files, it's much easier. – Denis Tulskiy Jan 10 '13 at 17:47
  • currently, they are writing in the same file. By merging I mean merging chronologically. It would be hard to merge records from 4 different files in chronological order. It's very strange Java doesn't have built-in functionality for writing to the same file without lots of headache. I thought it was a typical thing needed to many developers... – Racoon Jan 11 '13 at 10:09
  • @Racoon: don't you have timestamps in those log files? – Denis Tulskiy Jan 11 '13 at 10:38
  • I can write timestamps in those files myself. Would you suggest writing to seperate files and then merging? It's strange nobody seriously recommends using Java built-in facilities. – Racoon Jan 11 '13 at 14:14