Class LockExecutors
- java.lang.Object
-
- com.sodius.oslc.server.concurrent.lock.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 Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static LockExecutor
forLock(String lockName, Lock lock)
Creates an executor for the given lock name and lock instance.static LockExecutor
forLock(String lockName, Lock lock, long waitingTime, TimeUnit waitingTimeUnit)
Creates an executor for the given lock name, lock instance and waiting period.static LockExecutor
get(String lockName)
Returns an executor that guarantees exclusive access for the given lock name.static void
setFactory(Function<String,LockExecutor> factory)
Changes the factory used to create aLockExecutor
instance for a given lock name.
-
-
-
Method Detail
-
setFactory
public static void setFactory(Function<String,LockExecutor> factory)
Changes the factory used to create aLockExecutor
instance for a given lock name. Applications that are deployed on a cluster must provide a factory that is cluster compatible. Factory implementation may useforLock(String, Lock)
method to obtain an executor for a givenLock
.- 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 executionwaitingTime
- the maximum time to wait for the lockwaitingTimeUnit
- the time unit of thewaitingTime
argument- Returns:
- an executor for the given lock.
-
-