@NotThreadSafe public class DefaultLockManager extends Object implements LockManager
This class is responsible of managing the lock at high level. It provides 3 main methods which are for acquiring, ensuring(doesn't acquires the lock, just refresh the expiration time if required) and releasing the lock.
Implementation note: This class is not thread safe. If in future development thread safety is needed, please consider using volatile for expiresAt field and synchronized mechanism.
| Constructor and Description |
|---|
DefaultLockManager(LockRepository repository,
TimeService timeUtils)
Constructor takes some bean injections
|
| Modifier and Type | Method and Description |
|---|---|
void |
acquireLockDefault()
Tries to acquire the default lock regardless who is the current owner.
|
void |
clean() |
void |
close()
release the default lock.
|
void |
ensureLockDefault()
Tries to refresh the default lock when the current LockChecker has the lock or , when the lock
is expired, is the last owner
|
int |
getLockMaxTries() |
String |
getOwner() |
boolean |
isLockHeld() |
void |
releaseLockDefault()
Release the default lock when is acquired by the current LockChecker.
|
DefaultLockManager |
setLockAcquiredForMillis(long lockAcquiredForMillis)
Indicates the number of milliseconds the lock will be acquired for
|
DefaultLockManager |
setLockMaxTries(int lockMaxTries)
Updates the maximum number of tries to acquire the lock, if the flag 'waitForLog' is set
|
DefaultLockManager |
setLockMaxWaitMillis(long lockMaxWaitMillis)
If the flag 'waitForLog' is set, indicates the maximum time it will wait for the lock in each try.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitgetDefaultKeypublic DefaultLockManager(LockRepository repository, TimeService timeUtils)
repository - lock repositorytimeUtils - time utils servicepublic void acquireLockDefault()
throws LockCheckException
Tries to acquire the default lock regardless who is the current owner.
If the lock is already acquired by the current LockChecker or is expired, will be updated
In case the lock is acquired by another LockChecker, it will wait until the current lock is expired and will try to acquire it again. This will be repeated as many times as (maxTries - 1)
acquireLockDefault in interface LockManagerLockCheckException - if the lock cannot be acquiredpublic void ensureLockDefault()
throws LockCheckException
Tries to refresh the default lock when the current LockChecker has the lock or , when the lock is expired, is the last owner
Notice that it does not try to acquire when is acquired by another LockChecker
ensureLockDefault in interface LockManagerLockCheckException - if, in case of needed, the lock cannot be refreshedpublic void releaseLockDefault()
Release the default lock when is acquired by the current LockChecker.
When the lock is not acquired by the current LockChecker, it won't make any change. Does not throw any exception neither.
Idempotent operation.
releaseLockDefault in interface LockManagerpublic void close()
close in interface LockManagerclose in interface Closeableclose in interface AutoCloseablepublic DefaultLockManager setLockMaxWaitMillis(long lockMaxWaitMillis)
If the flag 'waitForLog' is set, indicates the maximum time it will wait for the lock in each try.
Default 3 minutes
setLockMaxWaitMillis in interface LockManagerlockMaxWaitMillis - max waiting time for lock. Must be greater than 0public int getLockMaxTries()
getLockMaxTries in interface LockManagerpublic DefaultLockManager setLockMaxTries(int lockMaxTries)
Updates the maximum number of tries to acquire the lock, if the flag 'waitForLog' is set
Default 1
setLockMaxTries in interface LockManagerlockMaxTries - number of triespublic DefaultLockManager setLockAcquiredForMillis(long lockAcquiredForMillis)
Indicates the number of milliseconds the lock will be acquired for
Default 3 minutes
setLockAcquiredForMillis in interface LockManagerlockAcquiredForMillis - milliseconds the lock will be acquired forpublic String getOwner()
getOwner in interface LockManagerpublic boolean isLockHeld()
isLockHeld in interface LockManagerpublic void clean()
clean in interface LockManagerCopyright © 2021. All rights reserved.