How to best lock files in a Java cluster

I have a server cluster running on JBoss I need to update the file in a safe way Specifically, I need

>Lock file a – if it is already locked, it is blocked in a safe way so that if the JVM suddenly dies, there will be no dangling lock The 30 second timeout is OK. > Read file A. > change content > write file to temporary name a.tmp > delete original file A. > rename a.tmp to correct name A. > unlock file a

When I look at Java nio. Filelock, it seems to be associated with an InputStream I really just need to lock an abstract name I don't need to lock part of the file If this is the best choice, I can create a lock file (separate from the data file) for this However, the main problem with my problem is that I need to acquire the lock before reading, and then release the lock after updating the file Please note that the way I update files is to ensure that there are no partially written files on the file system I need to write the whole file, and then rename it after writing to ensure that any file has a complete set of contents. If the process dies during writing, a temporary file that can be easily cleaned up will be left later

java. nio. Is filelock really used for this purpose? Or should I see something else?

Solution

This is what I finally did For each file named "XXX", I lock the file with a zero length named "xxx#lock"

>I use RandomAccessFile to lock it for update. > When this locked file is locked, I manipulate the actual file with problems: reading the original file, streaming to the temporary file, deleting the original file, renaming the temporary file, etc. > I unlocked the lock file

Lock code:

File lockFile = new File(target.getParent(),target.getName() + "#LOCK");
    lockAccessFile = new RandomAccessFile(lockFile,"rw");
    FileChannel lockChannel = lockAccessFile.getChannel();
    lock = lockChannel.lock();

Unlock code:

if (lock != null) {
        lock.release();
        lock = null;
    }
    if (lockAccessFile != null) {
        lockAccessFile.close();
        lockAccessFile = null;
    }

I wrap it in a class that enables locking and unlocking with reading or writing to the original file As you can see, lock and lockaccessfile are member variables

I never write anything in the lock file, but they exist in the folder Since I have a small number of files to manage in this way (six of them), I just leave the lock file there because it is an ugly but harmless overhead

This works across multi - homed clusters in my code because my code is the only thing that manipulates these files The biggest problem is that if some other code starts operating on files without knowing this Convention, it will cause problems I found no evidence that there is a standard way to deal with this problem, which will be enforced by the operating system and supported by Java If you know, please let me know

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>