From ad16ac51e9a79355cf3e5049bcd189218cecdee4 Mon Sep 17 00:00:00 2001 From: Konstantin Yakimov Date: Tue, 23 Dec 2014 16:14:32 +0300 Subject: [PATCH] Add the ability to point own local LockRegistry. JIRA: https://jira.spring.io/browse/INT-3587 There is no ability to use own local LockRegistry to avoid overlapping of produced ReentrantLock objects, it can happens if used unique keys have same masked value for DefaultLockRegistry. Add a constructor to allow the configuration of a lock registry with a differnt number of locks than the default. --- .../redis/util/RedisLockRegistry.java | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/spring-integration-redis/src/main/java/org/springframework/integration/redis/util/RedisLockRegistry.java b/spring-integration-redis/src/main/java/org/springframework/integration/redis/util/RedisLockRegistry.java index 571b2cc2ca..1996e7665f 100644 --- a/spring-integration-redis/src/main/java/org/springframework/integration/redis/util/RedisLockRegistry.java +++ b/spring-integration-redis/src/main/java/org/springframework/integration/redis/util/RedisLockRegistry.java @@ -75,6 +75,7 @@ import org.springframework.util.Assert; * {@link Condition}s are not supported. * * @author Gary Russell + * @author Konstantin Yakimov * @since 4.0 * */ @@ -94,7 +95,7 @@ public final class RedisLockRegistry implements LockRegistry { private final long expireAfter; - private final LockRegistry localRegistry = new DefaultLockRegistry(); + private final LockRegistry localRegistry; private final LockSerializer lockSerializer = new LockSerializer(); @@ -110,7 +111,8 @@ public final class RedisLockRegistry implements LockRegistry { } /** - * Constructs a lock registry with the default (60 second) lock expiration. + * Constructs a lock registry with the default (60 second) lock expiration and a default + * local {@link DefaultLockRegistry}. * @param connectionFactory The connection factory. * @param registryKey The key prefix for locks. */ @@ -119,14 +121,29 @@ public final class RedisLockRegistry implements LockRegistry { } /** - * Constructs a lock registry with the supplied lock expiration. + * Constructs a lock registry with the supplied lock expiration and a default + * local {@link DefaultLockRegistry}. * @param connectionFactory The connection factory. * @param registryKey The key prefix for locks. * @param expireAfter The expiration in milliseconds. */ public RedisLockRegistry(RedisConnectionFactory connectionFactory, String registryKey, long expireAfter) { + this(connectionFactory, registryKey, expireAfter, new DefaultLockRegistry()); + } + + /** + * Constructs a lock registry with the supplied lock expiration and a custom local {@link LockRegistry}. + * @param connectionFactory The connection factory. + * @param registryKey The key prefix for locks. + * @param expireAfter The expiration in milliseconds. + * @param localRegistry The local registry used to reduce wait time, + * {@link DefaultLockRegistry} is used by default. + */ + public RedisLockRegistry(RedisConnectionFactory connectionFactory, String registryKey, + long expireAfter, LockRegistry localRegistry) { Assert.notNull(connectionFactory, "'connectionFactory' cannot be null"); Assert.notNull(registryKey, "'registryKey' cannot be null"); + Assert.notNull(localRegistry, "'localRegistry' cannot be null"); this.redisTemplate = new RedisTemplate(); this.redisTemplate.setConnectionFactory(connectionFactory); this.redisTemplate.setKeySerializer(new StringRedisSerializer()); @@ -134,6 +151,7 @@ public final class RedisLockRegistry implements LockRegistry { this.redisTemplate.afterPropertiesSet(); this.registryKey = registryKey; this.expireAfter = expireAfter; + this.localRegistry = localRegistry; } @Override