Skip to content

Commit

Permalink
Add the ability to point own local LockRegistry.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kiakimov authored and garyrussell committed Dec 23, 2014
1 parent c56ee44 commit ad16ac5
Showing 1 changed file with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
* {@link Condition}s are not supported.
*
* @author Gary Russell
* @author Konstantin Yakimov
* @since 4.0
*
*/
Expand All @@ -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();

Expand All @@ -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.
*/
Expand All @@ -119,21 +121,37 @@ public RedisLockRegistry(RedisConnectionFactory connectionFactory, String regist
}

/**
* 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<String, RedisLockRegistry.RedisLock>();
this.redisTemplate.setConnectionFactory(connectionFactory);
this.redisTemplate.setKeySerializer(new StringRedisSerializer());
this.redisTemplate.setValueSerializer(new LockSerializer());
this.redisTemplate.afterPropertiesSet();
this.registryKey = registryKey;
this.expireAfter = expireAfter;
this.localRegistry = localRegistry;
}

@Override
Expand Down

0 comments on commit ad16ac5

Please sign in to comment.