Class RedisLockRegistry
java.lang.Object
org.springframework.integration.redis.util.RedisLockRegistry
- All Implemented Interfaces:
- DisposableBean,- ExpirableLockRegistry,- LockRegistry,- RenewableLockRegistry
public final class RedisLockRegistry
extends Object
implements ExpirableLockRegistry, DisposableBean, RenewableLockRegistry
Implementation of 
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.
 Conditions are not supported.
- Since:
- 4.0
- Author:
- Gary Russell, Konstantin Yakimov, Artem Bilan, Vedran Pavic, Unseok Kim, Anton Gabov, Christian Tzolov, Eddie Cho, Myeonghyeon Lee, Roman Zabaluev, Alex Peelman, Youbin Wu
- 
Nested Class SummaryNested ClassesModifier and TypeClassDescriptionstatic enumThe mode in which this registry is going to work with locks.
- 
Constructor SummaryConstructorsConstructorDescriptionRedisLockRegistry(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.
- 
Method SummaryModifier and TypeMethodDescriptionvoiddestroy()voidexpireUnusedOlderThan(long age) Remove locks last acquired more than 'age' ago that are not currently locked.Obtain the lock associated with the parameter object.voidRenew the time to live of the lock is associated with the parameter object.voidsetCacheCapacity(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 a @link Duration} to sleep between obtainLock attempts.voidsetRedisLockType(RedisLockRegistry.RedisLockType redisLockType) SetRedisLockRegistry.RedisLockTypemode to work in.voidsetRenewalTaskScheduler(TaskScheduler renewalTaskScheduler) Set theTaskSchedulerto use for the renewal task.Methods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.integration.support.locks.LockRegistryexecuteLocked, executeLocked, executeLocked, executeLocked
- 
Constructor Details- 
RedisLockRegistryConstructs a lock registry with the default (60 second) lock expiration.- Parameters:
- connectionFactory- The connection factory.
- registryKey- The key prefix for locks.
 
- 
RedisLockRegistrypublic RedisLockRegistry(RedisConnectionFactory connectionFactory, String registryKey, long expireAfter) Constructs a lock registry with the supplied lock expiration.- Parameters:
- connectionFactory- The connection factory.
- registryKey- The key prefix for locks.
- expireAfter- The expiration in milliseconds.
 
 
- 
- 
Method Details- 
setExecutor
- 
setRenewalTaskSchedulerDescription copied from interface:RenewableLockRegistrySet theTaskSchedulerto use for the renewal task. When renewalTaskScheduler is set, it will be used to periodically renew the lock to ensure that the lock does not expire while the thread is working.- Specified by:
- setRenewalTaskSchedulerin interface- RenewableLockRegistry
- Parameters:
- renewalTaskScheduler- renew task scheduler
 
- 
setCacheCapacitypublic void setCacheCapacity(int cacheCapacity) Set the capacity of cached locks.- Parameters:
- cacheCapacity- The capacity of cached lock, (default 100_000).
- Since:
- 5.5.6
 
- 
setIdleBetweenTries
- 
setRedisLockTypeSetRedisLockRegistry.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- the- RedisLockRegistry.RedisLockTypeto work in.
- Since:
- 5.5.13
 
- 
obtainDescription copied from interface:LockRegistryObtain the lock associated with the parameter object.- Specified by:
- obtainin interface- LockRegistry
- Parameters:
- lockKey- The object with which the lock is associated.
- Returns:
- The associated lock.
 
- 
expireUnusedOlderThanpublic void expireUnusedOlderThan(long age) Description copied from interface:ExpirableLockRegistryRemove locks last acquired more than 'age' ago that are not currently locked.- Specified by:
- expireUnusedOlderThanin interface- ExpirableLockRegistry
- Parameters:
- age- the time since the lock was last obtained.
 
- 
destroypublic void destroy()- Specified by:
- destroyin interface- DisposableBean
 
- 
renewLockDescription copied from interface:RenewableLockRegistryRenew the time to live of the lock is associated with the parameter object. The lock must be held by the current thread- Specified by:
- renewLockin interface- RenewableLockRegistry
- Parameters:
- lockKey- The object with which the lock is associated.
 
 
-