I am building a Java application to process files on a local FS (NTFS, but a solution that would allow an easy extension to Linux filesystems in future would be nice). The problem is that a single file cannot be processed more than once in case of two instances (processes) of my application running simultaneously.
Seems like I need to lock each file before processing it somehow. The application does not use a database and using one just for the purpose of locking does not seem convenient.
So I was thinking about appending a postfix to the filename. The question is, how (if it’s possible) to ask if a filename ends with a postfix and then append the postfix atomically.
Other tricks to achieve what I need are also appreciated.
You can perhaps take an advantage of the fact that file rename operation is atomic on most file systems. Your logic could be like this: