Class LockFile


  • public class LockFile
    extends java.lang.Object
    Provides for the creation and management of a named lock file. Keep in mind that if a lock expires it may be removed without notice. Care should be taken to renew a lock file in a timely manner.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int nextInstanceId  
    • Constructor Summary

      Constructors 
      Constructor Description
      LockFile​(java.io.File file)
      Constructor.
      LockFile​(java.io.File dir, java.lang.String name)
      Constructor.
      LockFile​(java.io.File dir, java.lang.String name, java.lang.String lockType)
      Constructor.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean containsLock​(java.io.File dir)  
      boolean createLock()
      Create the lock file using the default timeout.
      boolean createLock​(int timeout, boolean hold)
      Create the lock file.
      void dispose()
      Cleanup lock resources and tasks.
      protected void finalize()
      Cleanup during garbage collection.
      java.lang.String getLockOwner()
      Return the name of the current lock owner or "<Unknown>" if not locked or could not be determined.
      boolean haveLock()
      Determine if lock file was successfully created by this instance.
      boolean haveLock​(boolean verify)
      Determine if lock is still in place.
      static boolean isLocked​(java.io.File file)  
      static boolean isLocked​(java.io.File dir, java.lang.String name)  
      void removeLock()
      Remove the lock file.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • nextInstanceId

        public static int nextInstanceId
    • Constructor Detail

      • LockFile

        public LockFile​(java.io.File dir,
                        java.lang.String name)
        Constructor.
        Parameters:
        dir - directory containing lock file
        name - unmangled name of entity which this lock is associated with.
      • LockFile

        public LockFile​(java.io.File dir,
                        java.lang.String name,
                        java.lang.String lockType)
        Constructor.
        Parameters:
        dir - directory containing lock file
        name - unmangled name of entity which this lock is associated with.
        lockType - unique lock identifier (may not contain a '.')
      • LockFile

        public LockFile​(java.io.File file)
        Constructor.
        Parameters:
        file - file whose lock state will be controlled with this lock file.
    • Method Detail

      • isLocked

        public static boolean isLocked​(java.io.File dir,
                                       java.lang.String name)
        Parameters:
        dir - directory containing lock file
        name - of entity which this lock is associated with.
        Returns:
        true if any lock exists within dir for the given entity name.
      • isLocked

        public static boolean isLocked​(java.io.File file)
        Parameters:
        file - file whose lock state is controlled with this lock file.
        Returns:
        true if any lock exists within dir for the given entity name.
      • containsLock

        public static boolean containsLock​(java.io.File dir)
      • haveLock

        public boolean haveLock()
        Determine if lock file was successfully created by this instance. This does not quarentee that the lock is still present if more than MAX_LOCK_LEASE_PERIOD has lapsed since lock was created.
        Returns:
        true if lock has been created, otherwise false.
      • haveLock

        public boolean haveLock​(boolean verify)
        Determine if lock is still in place. Verifying the lock may be necessary when slow processes are holding the lock without timely renewals.
        Returns:
        true if lock is still in place, otherwise false.
      • getLockOwner

        public java.lang.String getLockOwner()
        Return the name of the current lock owner or "<Unknown>" if not locked or could not be determined.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • removeLock

        public void removeLock()
        Remove the lock file. This method should be invoked when the corresponding transaction is complete.
      • createLock

        public boolean createLock()
        Create the lock file using the default timeout. Lock is guaranteed for MAX_LOCK_LEASE_PERIOD seconds.
        Returns:
        true if lock creation was successful.
      • createLock

        public boolean createLock​(int timeout,
                                  boolean hold)
        Create the lock file. If another lock file already exists, wait for it to expire within the specified timeout period. Method will block until either the lock is obtained or the timeout period lapses.
        Parameters:
        timeout - maximum time in milliseconds to wait for lock.
        hold - if true the lock will be held and maintained until removed, otherwise it is only guaranteed for MAX_LOCK_LEASE_PERIOD seconds.
        Returns:
        true if lock creation was successful.
      • dispose

        public void dispose()
        Cleanup lock resources and tasks. Invoking this method could prevent stale locks from being removed if createLock was invoked with a very short timeout. Use of dispose is optional - the associated wait task should stop by it self allowing the LockFile object to be finalized.
      • finalize

        protected void finalize()
        Cleanup during garbage collection.
        Overrides:
        finalize in class java.lang.Object