I have multiple processes monitoring the same directory for files to process. I want to be able to lock a file that is being processed so none of the other processes can start processing that file. I have seen methods that rename the file with '.' in front. Is this a good way to do it or is there a better way? Also when you rename do you create a new file?
I don't think there's anything special about renaming with a dot in front of the name, except that some platforms will hide that file from ordinary directory listings presented to the user. They will not be hidden from Java, I believe, even on those platforms.
However, if your programs "know" that a file starting with a dot is already being processed, then your idea ought to work, providing that renaming is "atomic", meaning no thread or process will see a part-renamed file.
Unfortunately, the Java API does not make any guarantees about how renaming (File.renameTo()) works. It might be atomic, or it might not.
In practice, I believe that renaming, within the same directory is atomic on Win/Lin/MacOSX. Therefore, your idea should work.
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Joined: Oct 30, 2001
... but a nicer architecture might be to have a single component responsible for scanning the directory. That component would then hand over responsibility for processing the files to one of a pool of processor components.
Joined: Sep 24, 2005
So should I do something like this
Or can I just use file.rename on the original file?
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com