java.lang.Object
jp.ecuacion.lib.core.util.FileLockUtil
Provides file lock utility methods.
The method lock obtains the lock of the lock file,
and release releases the lock.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringgetLockFileVersion(File lockFile) Obtains the last update timestamp string from the lock file.static org.apache.commons.lang3.tuple.Pair<FileChannel, FileLock> Obtains the lock of the designated file.static voidrelease(org.apache.commons.lang3.tuple.Pair<FileChannel, FileLock> channelAndLock) Releases the lock.
-
Constructor Details
-
FileLockUtil
public FileLockUtil()
-
-
Method Details
-
lock
public static org.apache.commons.lang3.tuple.Pair<FileChannel,FileLock> lock(File lockFile, String version) throws IOException Obtains the lock of the designated file.The argument
lockFileis the file only for locks, not the business use files.This method also supports the optimistic exclusive control.
The version is obtained from the timestamp string of the file bygetLockFileVersionmethod.
When the lock is released, the lock file is updated by writing the timestamp to it.- Parameters:
lockFile- lockFileversion- the version, may benullwhen you don't have to validate the version from the optimistic exclusive control.- Returns:
- The
Pairtuple which haveFileChannelandFileLock. - Throws:
IOException- IOException
-
release
public static void release(org.apache.commons.lang3.tuple.Pair<FileChannel, FileLock> channelAndLock) throws IOExceptionReleases the lock.The timestamp string is written to the lock file right before the lock is released.
- Parameters:
channelAndLock- the return object of the methodlock.- Throws:
IOException- IOException
-
getLockFileVersion
Obtains the last update timestamp string from the lock file.This is used to get the version for optimistic exclusive control.
If the file does not exist, this method creates it.
- Returns:
- The timestamp string in yyyy-mm-dd-hh-mi-ss.SSS format. To ignore the time offset, the time is always treated as UTC.
- Throws:
IOException
-