Class LockExecutors


  • public class LockExecutors
    extends Object
    Provides access to executors that guarantee exclusive use of a given lock for tasks. This class is to use when exclusive access to a resource is needed across the entire application.
    Since:
    3.5.0
    • Method Detail

      • setFactory

        public static void setFactory​(Function<String,​LockExecutor> factory)
        Changes the factory used to create a LockExecutor instance for a given lock name. Applications that are deployed on a cluster must provide a factory that is cluster compatible. Factory implementation may use forLock(String, Lock) method to obtain an executor for a given Lock.
        Parameters:
        factory - the factory to create lock executors, for a given lock name as function argument
      • get

        public static LockExecutor get​(String lockName)
        Returns an executor that guarantees exclusive access for the given lock name. The returned executor ensures two tasks using the same lock cannot run at the same time; they will run sequentially.

        The default implementation guarantees exclusive access to a lock within the Java Virtual Machine. It uses the forLock(String, Lock) method to create an executor.

        If an application is cluster compatible, it must provide a different implementation with setFactory(Function), so that exclusive access is guaranteed for the cluster. This involves mechanisms that are specific to such application.

        Parameters:
        lockName - the name of the lock to acquire by the execution.
        Returns:
        an executor for the given lock.
      • forLock

        public static LockExecutor forLock​(String lockName,
                                           Lock lock)
        Creates an executor for the given lock name and lock instance.

        The executor will wait on the lock for 50 seconds. This default waiting timeout was chosen because the standard HTTP request timeout is 1 minute. This way the task will never be longer than the HTTP request itself.

        Parameters:
        lockName - the name of the lock to acquire by the execution.
        lock - the lock instance to control the execution
        Returns:
        an executor for the given lock.
        See Also:
        forLock(String, Lock, long, TimeUnit)
      • forLock

        public static LockExecutor forLock​(String lockName,
                                           Lock lock,
                                           long waitingTime,
                                           TimeUnit waitingTimeUnit)
        Creates an executor for the given lock name, lock instance and waiting period.
        Parameters:
        lockName - the name of the lock to acquire by the execution.
        lock - the lock instance to control the execution
        waitingTime - the maximum time to wait for the lock
        waitingTimeUnit - the time unit of the waitingTime argument
        Returns:
        an executor for the given lock.