Class RedisLockRegistry

java.lang.Object
org.springframework.integration.redis.util.RedisLockRegistry
All Implemented Interfaces:
org.springframework.beans.factory.DisposableBean, org.springframework.integration.support.locks.ExpirableLockRegistry, org.springframework.integration.support.locks.LockRegistry

public final class RedisLockRegistry
extends java.lang.Object
implements org.springframework.integration.support.locks.ExpirableLockRegistry, org.springframework.beans.factory.DisposableBean
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
  • Nested Class Summary

    Nested Classes 
    Modifier and Type Class Description
    static class  RedisLockRegistry.RedisLockType
    The mode in which this registry is going to work with locks.
  • Constructor Summary

    Constructors 
    Constructor Description
    RedisLockRegistry​(org.springframework.data.redis.connection.RedisConnectionFactory connectionFactory, java.lang.String registryKey)
    Constructs a lock registry with the default (60 second) lock expiration.
    RedisLockRegistry​(org.springframework.data.redis.connection.RedisConnectionFactory connectionFactory, java.lang.String registryKey, long expireAfter)
    Constructs a lock registry with the supplied lock expiration.
  • Method Summary

    Modifier and Type Method Description
    void destroy()  
    void expireUnusedOlderThan​(long age)  
    java.util.concurrent.locks.Lock obtain​(java.lang.Object lockKey)  
    void setCacheCapacity​(int cacheCapacity)
    Set the capacity of cached locks.
    void setExecutor​(java.util.concurrent.Executor executor)
    Set the Executor, where is not provided then a default of cached thread pool Executor will be used.
    void setRedisLockType​(RedisLockRegistry.RedisLockType redisLockType)
    Set RedisLockRegistry.RedisLockType mode to work in.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • RedisLockRegistry

      public RedisLockRegistry​(org.springframework.data.redis.connection.RedisConnectionFactory connectionFactory, java.lang.String registryKey)
      Constructs 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, java.lang.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

      public void setExecutor​(java.util.concurrent.Executor executor)
      Set the Executor, where is not provided then a default of cached thread pool Executor will be used.
      Parameters:
      executor - the executor service
      Since:
      5.0.5
    • setCacheCapacity

      public void setCacheCapacity​(int cacheCapacity)
      Set the capacity of cached locks.
      Parameters:
      cacheCapacity - The capacity of cached lock, (default 100_000).
      Since:
      5.5.6
    • setRedisLockType

      public void setRedisLockType​(RedisLockRegistry.RedisLockType redisLockType)
      Set RedisLockRegistry.RedisLockType mode to work in. By default, the RedisLockRegistry.RedisLockType.SPIN_LOCK is used - works in all the environment. The RedisLockRegistry.RedisLockType.PUB_SUB_LOCK is 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.RedisLockType to work in.
      Since:
      5.5.13
    • obtain

      public java.util.concurrent.locks.Lock obtain​(java.lang.Object lockKey)
      Specified by:
      obtain in interface org.springframework.integration.support.locks.LockRegistry
    • expireUnusedOlderThan

      public void expireUnusedOlderThan​(long age)
      Specified by:
      expireUnusedOlderThan in interface org.springframework.integration.support.locks.ExpirableLockRegistry
    • destroy

      public void destroy()
      Specified by:
      destroy in interface org.springframework.beans.factory.DisposableBean