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
public abstract class AbstractLockExecutor<T> extends Object implements LockExecutor
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
Constructors Modifier Constructor Description 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.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.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract Optional<T>
acquireLock()
Attempts to obtain a lock.<V> V
execute(Callable<V> command)
Executes the given task and returns its computed result, with the guarantee no other task for the same lock can run concurrently.protected abstract long
getLockAge(T lock)
Returns the lock age.protected abstract String
getLockName()
Returns the lock name to be displayed in logs.protected abstract Optional<T>
queryLock()
Queries an existing lock.protected abstract void
releaseLock(T lock)
Releases the lock.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.sodius.oslc.server.concurrent.lock.LockExecutor
execute
-
-
-
-
Constructor Detail
-
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 Detail
-
execute
public <V> V execute(Callable<V> command) throws Exception
Description copied from interface:LockExecutor
Executes 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:
execute
in 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 aCompletionException
if thetask
is eitherinterrupted
or the timeout to acquire the lock expires.
-
queryLock
protected abstract Optional<T> queryLock()
Queries an existing lock. Returns empty if the lock is not found or an exception happens.- Returns:
- an optional lock
-
getLockName
protected abstract String getLockName()
Returns the lock name to be displayed in logs.- Returns:
- lock name
-
getLockAge
protected abstract long getLockAge(T lock)
Returns the lock age.- Parameters:
lock
- the lock- Returns:
- lock age in milliseconds
-
releaseLock
protected abstract void releaseLock(T lock) throws LockException
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
protected abstract Optional<T> acquireLock() throws LockException
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
-
-