Package org.cdlib.mrt.utility
Class TLockFile
java.lang.Object
org.cdlib.mrt.utility.TLockFile
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
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected boolean
protected TLockFile.LockContent
protected boolean
protected TLockFile.LockContent
protected File
protected File
protected String
protected static final String
protected static final String
protected static final int
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected TLockFile.LockContent
buildLockContent
(Integer staleSeconds) Used to create a new LockContent object using a stale dateprotected TLockFile.LockContent
extractLockContent
(File localExtractFile) Lock info extracted from fileprotected int
Return the difference in seconds between current lock and an extracted lockget lock info for this objectget this lock file objectprotected File
getLockFile
(File base, String lockName) get requested lock filestatic TLockFile
getReplaceStaleTLockFile
(File base, String lockName, int staleTimeSeconds) Factory method - will replace lock if exceeds stale agestatic TLockFile
getReplaceTLockFile
(File base, String lockName, int staleTimeSeconds, int replaceTimeSeconds, int matchAttempts, int waitTimeSeconds) Factory method - will replace lock if exceeds certain age if does not exceed age then it attempts to build lockstatic TLockFile
getTLockFile
(File base, String lockName) Factory methodstatic TLockFile
getTLockFile
(File base, String lockName, int staleTimeSeconds) Factory methodstatic TLockFile
getTLockFile
(File base, String lockName, int matchAttempts, int waitTimeSeconds) Factory method - this form allows multiple retry attempts on the lockboolean
status of lockprotected void
debug loggingboolean
remove()
delete lockvoid
replaceLock
(int seconds) replace existing lock with this lock if existing lock exceeds the number of seconds provided.void
replace existing lock with this lock if existing lock stale time exceeds current timevoid
retryLock
(int noMatchAttempts, int matchAttempts, int waitTimeSeconds) Retry setting lock - Issue several threads waiting on same lock.void
setDebug
(boolean debug) turn on/off debugging levelprotected void
setLock()
set the lock - attempt to write lock file - reread lock - if my lock then lock is active - otherwise lock is inactive
-
Field Details
-
NAME
- See Also:
-
MESSAGE
- See Also:
-
NOMATCH_ATTEMPTS
protected static final int NOMATCH_ATTEMPTS- See Also:
-
lockFile
-
activeLock
protected boolean activeLock -
lockContent
-
currentLock
-
m_base
-
m_lockName
-
debug
protected boolean debug
-
-
Constructor Details
-
TLockFile
Constructor- Parameters:
base
- base File directory to contain this locklockName
- name of lock being created- Throws:
TException
-
TLockFile
- Throws:
TException
-
-
Method Details
-
setDebug
public void setDebug(boolean debug) turn on/off debugging level- Parameters:
debug
- true=logging turned on, false=logging turned off
-
getTLockFile
Factory method- Parameters:
base
- base File directory to contain this locklockName
- 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 locklockName
- 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 locklockName
- name of lock being createdmatchAttempts
- number of retries attemptedwaitTimeSeconds
- 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 locklockName
- name of lock being createdstaleTimeSeconds
- used to set lock staleTime valuereplaceTimeSeconds
- if age of lock exceeds replace time then force replacementmatchAttempts
- number of retries attemptedwaitTimeSeconds
- 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 locklockName
- name of lock being createdstaleTimeSeconds
- used to set lock staleTime value- Returns:
- TLockFile object for this lock
- Throws:
TException
-
setLock
set the lock - attempt to write lock file - reread lock - if my lock then lock is active - otherwise lock is inactive
- Throws:
TException
-
getLockFile
get requested lock file- Parameters:
base
- base File directory to contain this locklockName
- name of lock being created- Returns:
- lock file in this directory with this name
- Throws:
TException
-
extractLockContent
Lock info extracted from file- Parameters:
localExtractFile
- file containing lock information- Returns:
- extracted parsed lock info
- Throws:
TException
-
buildLockContent
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
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 lockmatchAttempts
- number of attempts to match that lock to this lockwaitTimeSeconds
- number of seconds between match attemts- Throws:
TException
-
replaceStaleLock
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
get this lock file object- Returns:
-
getLockContent
get lock info for this object- Returns:
-
remove
delete lock- Returns:
- true=lock deleted, false=unable to delete lock
- Throws:
TException
-
log
debug logging- Parameters:
msg
- log this message to sysout
-