Class AbstractLockExecutor<T>
java.lang.Object
com.sodius.oslc.server.concurrent.lock.AbstractLockExecutor<T>
- Type Parameters:
T- the lock object type
- All Implemented Interfaces:
LockExecutor,Executor
An executor that uses a singleton lock object to ensure runnable instances are executed sequentially.
This singleton object may be persisted in a database to be accessible across multiple JVMs.
For an alternative lock executor with file lock, see FileLockExecutor.
The executor uses a lock cache in case the runnable instances make subsequent calls to it, so the children commands dispose of the already acquired lock. It has optional parameters:
- lockOperationInterval: time between lock operations retries in milliseconds
- lockAcquisitionMaxRetries: max number of retries for lock acquisition
- lockReleaseMaxRetries: max number of retries for lock release
- lockMaxAge: maximum lock age in seconds before considering the lock as dead
- Since:
- 1.12.0
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedBuilds a Lock executor with a fixed interval of 100 milliseconds between retries, unlimited number of lock acquisition and release retries, and undefined lock max age.protectedAbstractLockExecutor(int lockOperationInterval, int lockAcquisitionMaxRetries, int lockReleaseMaxRetries, int lockMaxAge) Builds a Lock executor with a fixed interval between retries, fixed numbers of retries, and a lock max age. -
Method Summary
Modifier and TypeMethodDescriptionAttempts to obtain a lock.<V> VExecutes the given task and returns its computed result, with the guarantee no other task for the same lock can run concurrently.protected abstract longgetLockAge(T lock) Returns the lock age.protected abstract StringReturns the lock name to be displayed in logs.Queries an existing lock.protected abstract voidreleaseLock(T lock) Releases the lock.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.sodius.oslc.server.concurrent.lock.LockExecutor
execute
-
Constructor Details
-
AbstractLockExecutor
protected AbstractLockExecutor()Builds a Lock executor with a fixed interval of 100 milliseconds between retries, unlimited number of lock acquisition and release retries, and undefined lock max age. -
AbstractLockExecutor
protected AbstractLockExecutor(int lockOperationInterval, int lockAcquisitionMaxRetries, int lockReleaseMaxRetries, int lockMaxAge) Builds a Lock executor with a fixed interval between retries, fixed numbers of retries, and a lock max age.- Parameters:
lockOperationInterval- interval in milliseconds between lock operation retrieslockAcquisitionMaxRetries- the max number of lock acquisition retrieslockReleaseMaxRetries- the max number of lock release retrieslockMaxAge- the lock maximum age in seconds
-
-
Method Details
-
execute
Description copied from interface:LockExecutorExecutes the given task and returns its computed result, with the guarantee no other task for the same lock can run concurrently.The task is expected to be very lightweight and short-running, so that it does't block for too long other concurrent requests.
- Specified by:
executein interfaceLockExecutor- Type Parameters:
V- the result type of methodcall- Parameters:
command- the runnable task- Returns:
- the computed result
- Throws:
Exception- if unable to compute a result. The exception is aCompletionExceptionif thetaskis eitherinterruptedor the timeout to acquire the lock expires.
-
queryLock
Queries an existing lock. Returns empty if the lock is not found or an exception happens.- Returns:
- an optional lock
-
getLockName
Returns the lock name to be displayed in logs.- Returns:
- lock name
-
getLockAge
Returns the lock age.- Parameters:
lock- the lock- Returns:
- lock age in milliseconds
-
releaseLock
Releases the lock. If a not handled exception occurs, it must be thrown as a wrapped lock exception.- Parameters:
lock- the lock to release- Throws:
LockException- if something fails during the lock release
-
acquireLock
Attempts to obtain a lock. If none is available, returns empty. If a not handled exception occurs, it must be thrown as a wrapped lock exception.- Returns:
- an optional lock
- Throws:
LockException- if something fails during the lock acquisition
-