Package com.github.ddth.dlock.impl.redis
Class ClusteredRedisDLock
- java.lang.Object
-
- com.github.ddth.dlock.impl.AbstractDLock
-
- com.github.ddth.dlock.impl.redis.BaseRedisDLock
-
- com.github.ddth.dlock.impl.redis.ClusteredRedisDLock
-
- All Implemented Interfaces:
IDLock,AutoCloseable
public class ClusteredRedisDLock extends BaseRedisDLock
Clustered Redis implementation ofICache.- Since:
- 0.1.0
- Author:
- Thanh Ba Nguyen
-
-
Field Summary
Fields Modifier and Type Field Description static StringLOCK_PROP_REDIS_HOSTS_AND_PORTSTo override thesetRedisHostsAndPorts(String)setting.-
Fields inherited from class com.github.ddth.dlock.impl.redis.BaseRedisDLock
LOCK_PROP_REDIS_PASSWORD, myOwnRedis
-
Fields inherited from interface com.github.ddth.dlock.IDLock
DEFAULT_LOCK_DURATION_MS
-
-
Constructor Summary
Constructors Constructor Description ClusteredRedisDLock(String name)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected com.github.ddth.commons.redis.JedisConnectorbuildJedisConnector()protected redis.clients.jedis.JedisClustergetJedis()StringgetRedisHostsAndPorts()Redis' hosts and ports scheme (formathost1:port1,host2:port2,host3:port3).ClusteredRedisDLockinit()Init method.LockResultlock(int waitWeight, String clientId, long lockDurationMs)Acquire the lock forclientIdfor a duration oflockDurationMs.ClusteredRedisDLocksetRedisHostsAndPorts(String redisHostsAndPorts)Redis' hosts and ports scheme (formathost1:port1,host2:port2,host3:port3).LockResultunlock(String clientId)Release the lock.-
Methods inherited from class com.github.ddth.dlock.impl.redis.BaseRedisDLock
destroy, getJedisConnector, getRedisPassword, getScriptLock, getScriptUnlock, getZsetName, setJedisConnector, setRedisPassword, setScriptLock, setScriptUnlock, unlockResult, updateLockHolder, updateLockHolder
-
Methods inherited from class com.github.ddth.dlock.impl.AbstractDLock
close, getClientId, getLockProperties, getLockProperty, getName, getTimestampExpiry, lock, lock, lock, setClientId, setLockProperties, setName, setTimestampExpiry, toString, updateLockHolder
-
-
-
-
Field Detail
-
LOCK_PROP_REDIS_HOSTS_AND_PORTS
public static final String LOCK_PROP_REDIS_HOSTS_AND_PORTS
To override thesetRedisHostsAndPorts(String)setting.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ClusteredRedisDLock
public ClusteredRedisDLock(String name)
-
-
Method Detail
-
getRedisHostsAndPorts
public String getRedisHostsAndPorts()
Redis' hosts and ports scheme (formathost1:port1,host2:port2,host3:port3).- Returns:
-
setRedisHostsAndPorts
public ClusteredRedisDLock setRedisHostsAndPorts(String redisHostsAndPorts)
Redis' hosts and ports scheme (formathost1:port1,host2:port2,host3:port3).- Parameters:
redisHostsAndPorts-- Returns:
-
buildJedisConnector
protected com.github.ddth.commons.redis.JedisConnector buildJedisConnector()
- Specified by:
buildJedisConnectorin classBaseRedisDLock- Returns:
- Since:
- 0.1.1.2
-
init
public ClusteredRedisDLock init()
Init method.- Overrides:
initin classBaseRedisDLock- Returns:
-
getJedis
protected redis.clients.jedis.JedisCluster getJedis()
- Returns:
-
lock
public LockResult lock(int waitWeight, String clientId, long lockDurationMs)
Acquire the lock forclientIdfor a duration oflockDurationMs.Reentrant: lock can be acquired multiple times by the same
clientId. Lock's expiry will be extended accordingly.- Parameters:
waitWeight- "fairness":clientIdwith higherwaitWeightvalue might have higher chance to acquire the lock, negative value means "no fairness"clientId- within a namespace, only one client is allowed to hold the lock as a given time- Returns:
LockResult.SUCCESSFULif successful,LockResult.HOLD_BY_ANOTHER_CLIENTif lock is currently hold by another client
-
unlock
public LockResult unlock(String clientId)
Release the lock.- Parameters:
clientId- within a namespace, only one client is allowed to hold the lock as a given time- Returns:
LockResult.SUCCESSFULif successful,LockResult.HOLD_BY_ANOTHER_CLIENTif lock is currently hold by another client,LockResult.NOT_FOUNDif the lock is not currently hold by any client (not locked before, or already expired)
-
-