@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.
| Modifier and Type | Class and Description |
|---|---|
static class |
DefaultLockManager.DefaultLockManagerBuilder |
| Constructor and Description |
|---|
DefaultLockManager(LockRepository repository,
TimeService timeUtils,
long lockAcquiredForMillis,
long lockQuitTryingAfterMillis,
long lockTryFrequencyMillis,
long lockRefreshMarginMillis)
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.
|
static DefaultLockManager.DefaultLockManagerBuilder |
builder() |
void |
close()
Required when lock managing is done.
|
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
|
long |
getLockTryFrequency() |
long |
getMillisUntilRefreshRequired() |
String |
getOwner() |
protected void |
initialize()
idempotent operation that
- Starts the acquisition timer
- Initializes and run the lock daemon.
|
boolean |
isLockHeld() |
void |
releaseLockDefault()
Release the default lock when is acquired by the current LockChecker.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitgetDefaultKeypublic DefaultLockManager(LockRepository repository, TimeService timeUtils, long lockAcquiredForMillis, long lockQuitTryingAfterMillis, long lockTryFrequencyMillis, long lockRefreshMarginMillis)
repository - lock repositorytimeUtils - time utils servicepublic static DefaultLockManager.DefaultLockManagerBuilder builder()
public 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 long getLockTryFrequency()
getLockTryFrequency in interface LockManagerpublic String getOwner()
getOwner in interface LockManagerpublic boolean isLockHeld()
isLockHeld in interface LockManagerpublic long getMillisUntilRefreshRequired()
getMillisUntilRefreshRequired in interface LockManagerprotected void initialize()
Copyright © 2022. All rights reserved.