Class 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.
    • 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 retries
        lockAcquisitionMaxRetries - the max number of lock acquisition retries
        lockReleaseMaxRetries - the max number of lock release retries
        lockMaxAge - 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 interface LockExecutor
        Type Parameters:
        V - the result type of method call
        Parameters:
        command - the runnable task
        Returns:
        the computed result
        Throws:
        Exception - if unable to compute a result. The exception is a CompletionException if the task is either interrupted 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