Class RedisLockRegistry
- All Implemented Interfaces:
org.springframework.beans.factory.DisposableBean, org.springframework.integration.support.locks.ExpirableLockRegistry<org.springframework.integration.support.locks.DistributedLock>, org.springframework.integration.support.locks.LockRegistry<org.springframework.integration.support.locks.DistributedLock>, org.springframework.integration.support.locks.RenewableLockRegistry<org.springframework.integration.support.locks.DistributedLock>
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') is 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.
Conditions are not supported.
The lock instances are cached by their key for future reuse.
If all the locks in the cache are acquired,
the obtain(Object) method throws CannotAcquireLockException.
Otherwise, the old, unused locks are evicted from the cache.
An attempt to lock such an orphaned RedisLockRegistry.RedisLock throws another CannotAcquireLockException
to avoid double locking from different threads on the same key.
- Since:
- 4.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumThe mode in which this registry is going to work with locks. -
Constructor Summary
ConstructorsConstructorDescriptionRedisLockRegistry(org.springframework.data.redis.connection.RedisConnectionFactory connectionFactory, String registryKey) Create a lock registry with the default (60 second) lock expiration.RedisLockRegistry(org.springframework.data.redis.connection.RedisConnectionFactory connectionFactory, String registryKey, long expireAfter) Create a lock registry with the supplied lock expiration.RedisLockRegistry(org.springframework.data.redis.connection.RedisConnectionFactory connectionFactory, String registryKey, Duration expireAfter) Create a lock registry with the supplied lock expiration. -
Method Summary
Modifier and TypeMethodDescriptionvoiddestroy()voidexpireUnusedOlderThan(long age) org.springframework.integration.support.locks.DistributedLockvoidvoidvoidsetCacheCapacity(int cacheCapacity) Set the capacity of cached locks.voidsetExecutor(Executor executor) Set theExecutor, where is not provided then a default of cached thread pool Executor will be used.voidsetIdleBetweenTries(Duration idleBetweenTries) Specify aDurationto sleep between obtainLock attempts.voidsetRedisLockType(RedisLockRegistry.RedisLockType redisLockType) SetRedisLockRegistry.RedisLockTypemode to work in.voidsetRenewalTaskScheduler(org.springframework.scheduling.TaskScheduler renewalTaskScheduler) toString()Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.springframework.integration.support.locks.LockRegistry
executeLocked, executeLocked, executeLocked, executeLocked
-
Constructor Details
-
RedisLockRegistry
public RedisLockRegistry(org.springframework.data.redis.connection.RedisConnectionFactory connectionFactory, String registryKey) Create a lock registry with the default (60 second) lock expiration.- Parameters:
connectionFactory- The connection factory.registryKey- The key prefix for locks.
-
RedisLockRegistry
public RedisLockRegistry(org.springframework.data.redis.connection.RedisConnectionFactory connectionFactory, String registryKey, long expireAfter) Create a lock registry with the supplied lock expiration.- Parameters:
connectionFactory- The connection factory.registryKey- The key prefix for locks.expireAfter- The expiration in milliseconds.
-
RedisLockRegistry
public RedisLockRegistry(org.springframework.data.redis.connection.RedisConnectionFactory connectionFactory, String registryKey, Duration expireAfter) Create a lock registry with the supplied lock expiration.- Parameters:
connectionFactory- The connection factory.registryKey- The key prefix for locks.expireAfter- The expiration inDuration.- Since:
- 7.0
-
-
Method Details
-
setExecutor
-
setRenewalTaskScheduler
public void setRenewalTaskScheduler(org.springframework.scheduling.TaskScheduler renewalTaskScheduler) - Specified by:
setRenewalTaskSchedulerin interfaceorg.springframework.integration.support.locks.RenewableLockRegistry<org.springframework.integration.support.locks.DistributedLock>
-
setCacheCapacity
public void setCacheCapacity(int cacheCapacity) Set the capacity of cached locks. If the number of currently cached locks exceeds the new capacity, this method tries to evict unused locks.- Parameters:
cacheCapacity- The capacity of cached lock, (default 100_000).- Since:
- 5.5.6
- See Also:
-
setIdleBetweenTries
-
setRedisLockType
SetRedisLockRegistry.RedisLockTypemode to work in. By default, theRedisLockRegistry.RedisLockType.SPIN_LOCKis used - works in all the environment. TheRedisLockRegistry.RedisLockType.PUB_SUB_LOCKis a preferred mode when not in Master/Replica connections - less network chatter. Set the type of unlockType, Select the lock method.- Parameters:
redisLockType- theRedisLockRegistry.RedisLockTypeto work in.- Since:
- 5.5.13
-
obtain
- Specified by:
obtainin interfaceorg.springframework.integration.support.locks.LockRegistry<org.springframework.integration.support.locks.DistributedLock>
-
expireUnusedOlderThan
public void expireUnusedOlderThan(long age) - Specified by:
expireUnusedOlderThanin interfaceorg.springframework.integration.support.locks.ExpirableLockRegistry<org.springframework.integration.support.locks.DistributedLock>
-
destroy
public void destroy()- Specified by:
destroyin interfaceorg.springframework.beans.factory.DisposableBean
-
renewLock
- Specified by:
renewLockin interfaceorg.springframework.integration.support.locks.RenewableLockRegistry<org.springframework.integration.support.locks.DistributedLock>
-
renewLock
-
toString
-