Class TLockFile

java.lang.Object
org.cdlib.mrt.utility.TLockFile

public class TLockFile extends Object
Propriety locking class. May be replaced with normalized mechanism in java 7. Allows locking to nano second range but is not specified in dflat to a form that is independant of this java implementation.
Author:
dloy
  • Field Details

  • Constructor Details

  • Method Details

    • setDebug

      public void setDebug(boolean debug)
      turn on/off debugging level
      Parameters:
      debug - true=logging turned on, false=logging turned off
    • getTLockFile

      public static TLockFile getTLockFile(File base, String lockName) throws TException
      Factory method
      Parameters:
      base - base File directory to contain this lock
      lockName - name of lock being created
      Returns:
      TLockFile object for this lock
      Throws:
      TException
    • getTLockFile

      public static TLockFile getTLockFile(File base, String lockName, int staleTimeSeconds) throws TException
      Factory method
      Parameters:
      base - base File directory to contain this lock
      lockName - name of lock being created
      Returns:
      TLockFile object for this lock
      Throws:
      TException
    • getTLockFile

      public static TLockFile getTLockFile(File base, String lockName, int matchAttempts, int waitTimeSeconds) throws TException
      Factory method - this form allows multiple retry attempts on the lock
      Parameters:
      base - base File directory to contain this lock
      lockName - name of lock being created
      matchAttempts - number of retries attempted
      waitTimeSeconds - wait time in seconds between retries
      Returns:
      TLockFile object for this lock
      Throws:
      TException
    • getReplaceTLockFile

      public static TLockFile getReplaceTLockFile(File base, String lockName, int staleTimeSeconds, int replaceTimeSeconds, int matchAttempts, int waitTimeSeconds) throws TException
      Factory method - will replace lock if exceeds certain age if does not exceed age then it attempts to build lock
      Parameters:
      base - base File directory to contain this lock
      lockName - name of lock being created
      staleTimeSeconds - used to set lock staleTime value
      replaceTimeSeconds - if age of lock exceeds replace time then force replacement
      matchAttempts - number of retries attempted
      waitTimeSeconds - wait time in seconds between retries
      Returns:
      TLockFile object for this lock
      Throws:
      TException
    • getReplaceStaleTLockFile

      public static TLockFile getReplaceStaleTLockFile(File base, String lockName, int staleTimeSeconds) throws TException
      Factory method - will replace lock if exceeds stale age
      Parameters:
      base - base File directory to contain this lock
      lockName - name of lock being created
      staleTimeSeconds - used to set lock staleTime value
      Returns:
      TLockFile object for this lock
      Throws:
      TException
    • setLock

      protected void setLock() throws TException
       set the lock
       - attempt to write lock file
       - reread lock - if my lock then lock is active
       - otherwise lock is inactive
       
      Throws:
      TException
    • getLockFile

      protected File getLockFile(File base, String lockName) throws TException
      get requested lock file
      Parameters:
      base - base File directory to contain this lock
      lockName - name of lock being created
      Returns:
      lock file in this directory with this name
      Throws:
      TException
    • extractLockContent

      protected TLockFile.LockContent extractLockContent(File localExtractFile) throws TException
      Lock info extracted from file
      Parameters:
      localExtractFile - file containing lock information
      Returns:
      extracted parsed lock info
      Throws:
      TException
    • buildLockContent

      protected TLockFile.LockContent buildLockContent(Integer staleSeconds)
      Used to create a new LockContent object using a stale date
      Parameters:
      staleSeconds - seconds from current when lock is stale
      Returns:
      LockContent lock object
    • replaceLock

      public void replaceLock(int seconds) throws TException
      replace existing lock with this lock if existing lock exceeds the number of seconds provided. This is used to replace a stale lock probable created in a failed process
      Parameters:
      seconds - if age of lock exceeds this value then replace lock
      Throws:
      TException
    • getDiffCurrent

      protected int getDiffCurrent()
      Return the difference in seconds between current lock and an extracted lock
      Returns:
      second difference between locks
    • retryLock

      public void retryLock(int noMatchAttempts, int matchAttempts, int waitTimeSeconds) throws TException
       Retry setting lock -
       Issue several threads waiting on same lock. Odd change on lock if another
       thread has grabbed it as opposed to lock status not changing
       - if this lock is current then lock is active and return
       - attempt is made to build lock
       - see if written lock is my lock
       - if yes then lock is active
       - if no, see if lock was grabbed by some other process since my last attempt
       - if different process, then reissue lock
       
      Parameters:
      noMatchAttempts - number of attempts to set lock
      matchAttempts - number of attempts to match that lock to this lock
      waitTimeSeconds - number of seconds between match attemts
      Throws:
      TException
    • replaceStaleLock

      public void replaceStaleLock() throws TException
      replace existing lock with this lock if existing lock stale time exceeds current time
      Throws:
      TException - process exception
    • isActiveLock

      public boolean isActiveLock()
      status of lock
      Returns:
      true=active, false=not set
    • getLockFile

      public File getLockFile()
      get this lock file object
      Returns:
    • getLockContent

      public TLockFile.LockContent getLockContent()
      get lock info for this object
      Returns:
    • remove

      public boolean remove() throws TException
      delete lock
      Returns:
      true=lock deleted, false=unable to delete lock
      Throws:
      TException
    • log

      protected void log(String msg)
      debug logging
      Parameters:
      msg - log this message to sysout