Package com.github.ddth.dlock.impl.redis
Class RedisDLock
- java.lang.Object
-
- com.github.ddth.dlock.impl.AbstractDLock
-
- com.github.ddth.dlock.impl.redis.BaseRedisDLock
-
- com.github.ddth.dlock.impl.redis.RedisDLock
-
- All Implemented Interfaces:
IDLock,AutoCloseable
public class RedisDLock extends BaseRedisDLock
- Since:
- 0.1.0
- Author:
- Thanh Ba Nguyen
-
-
Field Summary
Fields Modifier and Type Field Description static StringLOCK_PROP_REDIS_HOST_AND_PORTTo override thesetRedisHostAndPort(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 RedisDLock(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.JedisgetJedis()StringgetRedisHostAndPort()Redis' host and port scheme (formathost:port).RedisDLockinit()Init method.LockResultlock(int waitWeight, String clientId, long lockDurationMs)Acquire the lock forclientIdfor a duration oflockDurationMs.RedisDLocksetRedisHostAndPort(String redisHostAndPort)Redis' host and port scheme (formathost:port).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_HOST_AND_PORT
public static final String LOCK_PROP_REDIS_HOST_AND_PORT
To override thesetRedisHostAndPort(String)setting.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
RedisDLock
public RedisDLock(String name)
-
-
Method Detail
-
getRedisHostAndPort
public String getRedisHostAndPort()
Redis' host and port scheme (formathost:port).- Returns:
-
setRedisHostAndPort
public RedisDLock setRedisHostAndPort(String redisHostAndPort)
Redis' host and port scheme (formathost:port).- Parameters:
redisHostAndPort-- Returns:
-
buildJedisConnector
protected com.github.ddth.commons.redis.JedisConnector buildJedisConnector()
- Specified by:
buildJedisConnectorin classBaseRedisDLock- Returns:
- Since:
- 0.1.1.2
-
init
public RedisDLock init()
Init method.- Overrides:
initin classBaseRedisDLock- Returns:
-
getJedis
protected redis.clients.jedis.Jedis 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)
-
-