Class FileLockUtil

java.lang.Object
jp.ecuacion.lib.core.util.FileLockUtil

public class FileLockUtil extends Object
Provides file lock utility methods.

The method lock obtains the lock of the lock file, and release 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 lockFile is 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 by getLockFileVersion method.
      When the lock is released, the lock file is updated by writing the timestamp to it.

      Parameters:
      lockFile - lockFile
      version - the version, may be null when you don't have to validate the version from the optimistic exclusive control.
      Returns:
      The Pair tuple which have FileChannel and FileLock.
      Throws:
      IOException - IOException
    • release

      public static void release(org.apache.commons.lang3.tuple.Pair<FileChannel,FileLock> channelAndLock) throws IOException
      Releases the lock.

      The timestamp string is written to the lock file right before the lock is released.

      Parameters:
      channelAndLock - the return object of the method lock.
      Throws:
      IOException - IOException
    • getLockFileVersion

      public static String getLockFileVersion(File lockFile) throws IOException
      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