public final class RedisLockRegistry extends Object implements ExpirableLockRegistry, DisposableBean
ExpirableLockRegistry
providing a distributed lock using Redis.
Locks are stored under the key registryKey:lockKey
. Locks expire after
(default 60) seconds. Threads unlocking an
expired lock will get an IllegalStateException
. This should be
considered as a critical error because it is possible the protected
resources were compromised.
Locks are reentrant.
However, locks are scoped by the registry; a lock from a different registry with the same key (even if the registry uses the same 'registryKey') are different locks, and the second cannot be acquired by the same thread while the first is locked.
Note: This is not intended for low latency applications. It is intended for resource locking across multiple JVMs.
Condition
s are not supported.
Constructor and Description |
---|
RedisLockRegistry(RedisConnectionFactory connectionFactory,
String registryKey)
Constructs a lock registry with the default (60 second) lock expiration.
|
RedisLockRegistry(RedisConnectionFactory connectionFactory,
String registryKey,
long expireAfter)
Constructs a lock registry with the supplied lock expiration.
|
Modifier and Type | Method and Description |
---|---|
void |
destroy() |
void |
expireUnusedOlderThan(long age)
Remove locks last acquired more than 'age' ago that are not currently locked.
|
Lock |
obtain(Object lockKey)
Obtains the lock associated with the parameter object.
|
void |
setExecutor(Executor executor)
Set the
Executor , where is not provided then a default of
cached thread pool Executor will be used. |
public RedisLockRegistry(RedisConnectionFactory connectionFactory, String registryKey)
connectionFactory
- The connection factory.registryKey
- The key prefix for locks.public RedisLockRegistry(RedisConnectionFactory connectionFactory, String registryKey, long expireAfter)
connectionFactory
- The connection factory.registryKey
- The key prefix for locks.expireAfter
- The expiration in milliseconds.public void setExecutor(Executor executor)
Executor
, where is not provided then a default of
cached thread pool Executor will be used.executor
- the executor servicepublic Lock obtain(Object lockKey)
LockRegistry
obtain
in interface LockRegistry
lockKey
- The object with which the lock is associated.public void expireUnusedOlderThan(long age)
ExpirableLockRegistry
expireUnusedOlderThan
in interface ExpirableLockRegistry
age
- the time since the lock was last obtained.public void destroy()
destroy
in interface DisposableBean