Package com.github.ddth.dlock.impl
Class AbstractDLock
- java.lang.Object
-
- com.github.ddth.dlock.impl.AbstractDLock
-
- All Implemented Interfaces:
IDLock,AutoCloseable
- Direct Known Subclasses:
BaseRedisDLock,InmemDLock
public abstract class AbstractDLock extends Object implements IDLock, AutoCloseable
Abstract implementation ofIDLock.- Since:
- 0.1.0
- Author:
- Thanh Ba Nguyen
-
-
Field Summary
-
Fields inherited from interface com.github.ddth.dlock.IDLock
DEFAULT_LOCK_DURATION_MS
-
-
Constructor Summary
Constructors Constructor Description AbstractDLock(String name)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()voiddestroy()Cleanup method.protected StringgetClientId()Get client-id who is currently holding the lock.protected PropertiesgetLockProperties()Get lock's custom properties.protected StringgetLockProperty(String key)Get lock's custom property.StringgetName()Get lock's name(space).protected longgetTimestampExpiry()Get timestamp (in milliseconds) when the lock-acquisition expires.AbstractDLockinit()Init method.LockResultlock(int waitWeight, String clientId)Acquire the lock forclientIdwith default duration.LockResultlock(String clientId)Acquire the lock forclientIdwith default duration.LockResultlock(String clientId, long lockDurationMs)Acquire the lock forclientIdfor a duration oflockDurationMs.protected AbstractDLocksetClientId(String clientId)Get client-id who is currently holding the lock.AbstractDLocksetLockProperties(Properties lockProps)Lock's custom properties.AbstractDLocksetName(String name)protected AbstractDLocksetTimestampExpiry(long timestampExpiry)Get timestamp (in milliseconds) when the lock-acquisition expires.StringtoString()protected voidupdateLockHolder(String clientId, long lockDurationMs)Update current lock's holder info.
-
-
-
Constructor Detail
-
AbstractDLock
public AbstractDLock(String name)
-
-
Method Detail
-
getName
public String getName()
Get lock's name(space).
-
setName
public AbstractDLock setName(String name)
-
updateLockHolder
protected void updateLockHolder(String clientId, long lockDurationMs)
Update current lock's holder info.- Parameters:
clientId-lockDurationMs-- Since:
- 0.1.1
-
getClientId
protected String getClientId()
Get client-id who is currently holding the lock.Note: value returned from this method is "estimated".
- Returns:
- Since:
- 0.1.1
-
setClientId
protected AbstractDLock setClientId(String clientId)
Get client-id who is currently holding the lock.- Parameters:
clientId-- Returns:
- Since:
- 0.1.1
-
getTimestampExpiry
protected long getTimestampExpiry()
Get timestamp (in milliseconds) when the lock-acquisition expires.Note: value returned from this method is "estimated".
- Returns:
- Since:
- 0.1.1
-
setTimestampExpiry
protected AbstractDLock setTimestampExpiry(long timestampExpiry)
Get timestamp (in milliseconds) when the lock-acquisition expires.- Parameters:
timestampExpiry-- Returns:
- Since:
- 0.1.1
-
setLockProperties
public AbstractDLock setLockProperties(Properties lockProps)
Lock's custom properties.- Parameters:
lockProps-- Returns:
-
getLockProperties
protected Properties getLockProperties()
Get lock's custom properties.- Returns:
-
getLockProperty
protected String getLockProperty(String key)
Get lock's custom property.- Parameters:
key-- Returns:
-
init
public AbstractDLock init()
Init method.- Returns:
-
destroy
public void destroy()
Cleanup method.
-
close
public void close()
- Specified by:
closein interfaceAutoCloseable
-
lock
public LockResult lock(String clientId)
Acquire the lock forclientIdwith default duration.Reentrant: lock can be acquired multiple times by the same
clientId. Lock's expiry will be extended accordingly.- Specified by:
lockin interfaceIDLock- Parameters:
clientId- within a namespace, only one client is allowed to hold lock as a given time- Returns:
LockResult.SUCCESSFULif successful,LockResult.HOLD_BY_ANOTHER_CLIENTif lock is currently hold by another client
-
lock
public LockResult lock(int waitWeight, String clientId)
Acquire the lock forclientIdwith default duration.Reentrant: lock can be acquired multiple times by the same
clientId. Lock's expiry will be extended accordingly.- Specified by:
lockin interfaceIDLock- 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- Since:
- 0.1.2
-
lock
public LockResult lock(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.- Specified by:
lockin interfaceIDLock- 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- Since:
- 0.1.2
-
-