Class RedissonLock
- java.lang.Object
-
- org.redisson.RedissonObject
-
- org.redisson.RedissonLock
-
- All Implemented Interfaces:
Lock
,RExpirable
,RExpirableAsync
,RLock
,RLockAsync
,RObject
,RObjectAsync
- Direct Known Subclasses:
RedissonFairLock
,RedissonReadLock
,RedissonTransactionalLock
,RedissonWriteLock
public class RedissonLock extends RedissonObject implements RLock
Distributed implementation ofLock
Implements reentrant lock.
Lock will be removed automatically if client disconnects.Implements a non-fair locking so doesn't guarantees an acquire order.
- Author:
- Nikita Koksharov
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
RedissonLock.ExpirationEntry
-
Field Summary
Fields Modifier and Type Field Description protected long
internalLockLeaseTime
protected static LockPubSub
PUBSUB
-
Fields inherited from class org.redisson.RedissonObject
codec
-
-
Constructor Summary
Constructors Constructor Description RedissonLock(CommandAsyncExecutor commandExecutor, String name)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected RFuture<Void>
acquireFailedAsync(long threadId)
boolean
clearExpire()
Clear an expire timeout or expire date for object.RFuture<Boolean>
clearExpireAsync()
Clear an expire timeout or expire date for object in async mode.RFuture<Boolean>
deleteAsync()
Delete object in async modeboolean
expire(long timeToLive, TimeUnit timeUnit)
Set a timeout for object.RFuture<Boolean>
expireAsync(long timeToLive, TimeUnit timeUnit)
Set a timeout for object in async mode.boolean
expireAt(long timestamp)
Set an expire date for object.boolean
expireAt(Date timestamp)
Set an expire date for object.RFuture<Boolean>
expireAtAsync(long timestamp)
Set an expire date for object in async mode.RFuture<Boolean>
expireAtAsync(Date timestamp)
Set an expire date for object in async mode.boolean
forceUnlock()
Unlocks lock independently of stateRFuture<Boolean>
forceUnlockAsync()
Unlocks lock independently of stateprotected RedissonLockEntry
getEntry(long threadId)
protected String
getEntryName()
int
getHoldCount()
Number of holds on this lock by the current threadRFuture<Integer>
getHoldCountAsync()
Number of holds on this lock by the current threadprotected String
getLockName(long threadId)
RFuture<Boolean>
isExistsAsync()
Check object existence in async mode.boolean
isHeldByCurrentThread()
Checks if this lock is held by the current threadboolean
isLocked()
Checks if this lock locked by any threadvoid
lock()
void
lock(long leaseTime, TimeUnit unit)
Acquires the lock.RFuture<Void>
lockAsync()
RFuture<Void>
lockAsync(long currentThreadId)
RFuture<Void>
lockAsync(long leaseTime, TimeUnit unit)
Acquires the lock.RFuture<Void>
lockAsync(long leaseTime, TimeUnit unit, long currentThreadId)
void
lockInterruptibly()
void
lockInterruptibly(long leaseTime, TimeUnit unit)
Acquires the lock.Condition
newCondition()
long
remainTimeToLive()
Remaining time to live of Redisson object that has a timeoutRFuture<Long>
remainTimeToLiveAsync()
Remaining time to live of Redisson object that has a timeoutprotected RFuture<Boolean>
renewExpirationAsync(long threadId)
protected RFuture<RedissonLockEntry>
subscribe(long threadId)
boolean
tryLock()
boolean
tryLock(long waitTime, long leaseTime, TimeUnit unit)
Returnstrue
as soon as the lock is acquired.boolean
tryLock(long waitTime, TimeUnit unit)
RFuture<Boolean>
tryLockAsync()
RFuture<Boolean>
tryLockAsync(long threadId)
RFuture<Boolean>
tryLockAsync(long waitTime, long leaseTime, TimeUnit unit)
RFuture<Boolean>
tryLockAsync(long waitTime, long leaseTime, TimeUnit unit, long currentThreadId)
RFuture<Boolean>
tryLockAsync(long waitTime, TimeUnit unit)
void
unlock()
RFuture<Void>
unlockAsync()
RFuture<Void>
unlockAsync(long threadId)
protected RFuture<Boolean>
unlockInnerAsync(long threadId)
protected void
unsubscribe(RFuture<RedissonLockEntry> future, long threadId)
-
Methods inherited from class org.redisson.RedissonObject
await, copy, copyAsync, delete, dump, dumpAsync, encode, encode, encode, encodeMapKey, encodeMapKeys, encodeMapValue, encodeMapValues, get, getCodec, getName, getName, isExists, migrate, migrateAsync, move, moveAsync, prefixName, rename, renameAsync, renamenx, renamenxAsync, restore, restore, restoreAndReplace, restoreAndReplace, restoreAndReplaceAsync, restoreAndReplaceAsync, restoreAsync, restoreAsync, suffixName, toSeconds, touch, touchAsync, unlink, unlinkAsync
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.redisson.api.RExpirable
clearExpire, expire, expireAt, expireAt, remainTimeToLive
-
Methods inherited from interface org.redisson.api.RExpirableAsync
clearExpireAsync, expireAsync, expireAtAsync, expireAtAsync, remainTimeToLiveAsync
-
Methods inherited from interface org.redisson.api.RObject
copy, delete, dump, getCodec, getName, isExists, migrate, move, rename, renamenx, restore, restore, restoreAndReplace, restoreAndReplace, touch, unlink
-
Methods inherited from interface org.redisson.api.RObjectAsync
copyAsync, dumpAsync, migrateAsync, moveAsync, renameAsync, renamenxAsync, restoreAndReplaceAsync, restoreAndReplaceAsync, restoreAsync, restoreAsync, touchAsync, unlinkAsync
-
-
-
-
Field Detail
-
internalLockLeaseTime
protected long internalLockLeaseTime
-
PUBSUB
protected static final LockPubSub PUBSUB
-
-
Constructor Detail
-
RedissonLock
public RedissonLock(CommandAsyncExecutor commandExecutor, String name)
-
-
Method Detail
-
getEntryName
protected String getEntryName()
-
getLockName
protected String getLockName(long threadId)
-
lock
public void lock(long leaseTime, TimeUnit unit)
Description copied from interface:RLock
Acquires the lock.If the lock is not available then the current thread becomes disabled for thread scheduling purposes and lies dormant until the lock has been acquired. If the lock is acquired, it is held until
unlock
is invoked, or until leaseTime milliseconds have passed since the lock was granted - whichever comes first.- Specified by:
lock
in interfaceRLock
- Parameters:
leaseTime
- the maximum time to hold the lock after granting it, before automatically releasing it if it hasn't already been released by invokingunlock
. If leaseTime is -1, hold the lock until explicitly unlocked.unit
- the time unit of theleaseTime
argument
-
lockInterruptibly
public void lockInterruptibly() throws InterruptedException
- Specified by:
lockInterruptibly
in interfaceLock
- Throws:
InterruptedException
-
lockInterruptibly
public void lockInterruptibly(long leaseTime, TimeUnit unit) throws InterruptedException
Description copied from interface:RLock
Acquires the lock.If the lock is not available then the current thread becomes disabled for thread scheduling purposes and lies dormant until the lock has been acquired. If the lock is acquired, it is held until
unlock
is invoked, or until leaseTime have passed since the lock was granted - whichever comes first.- Specified by:
lockInterruptibly
in interfaceRLock
- Parameters:
leaseTime
- the maximum time to hold the lock after granting it, before automatically releasing it if it hasn't already been released by invokingunlock
. If leaseTime is -1, hold the lock until explicitly unlocked.unit
- the time unit of theleaseTime
argument- Throws:
InterruptedException
- - if the thread is interrupted before or during this method.
-
tryLock
public boolean tryLock(long waitTime, long leaseTime, TimeUnit unit) throws InterruptedException
Description copied from interface:RLock
Returnstrue
as soon as the lock is acquired. If the lock is currently held by another thread in this or any other process in the distributed system this method keeps trying to acquire the lock for up towaitTime
before giving up and returningfalse
. If the lock is acquired, it is held untilunlock
is invoked, or untilleaseTime
have passed since the lock was granted - whichever comes first.- Specified by:
tryLock
in interfaceRLock
- Parameters:
waitTime
- the maximum time to aquire the lockleaseTime
- lease timeunit
- time unit- Returns:
true
if lock has been successfully acquired- Throws:
InterruptedException
- - if the thread is interrupted before or during this method.
-
getEntry
protected RedissonLockEntry getEntry(long threadId)
-
subscribe
protected RFuture<RedissonLockEntry> subscribe(long threadId)
-
unsubscribe
protected void unsubscribe(RFuture<RedissonLockEntry> future, long threadId)
-
tryLock
public boolean tryLock(long waitTime, TimeUnit unit) throws InterruptedException
- Specified by:
tryLock
in interfaceLock
- Throws:
InterruptedException
-
newCondition
public Condition newCondition()
- Specified by:
newCondition
in interfaceLock
-
forceUnlock
public boolean forceUnlock()
Description copied from interface:RLock
Unlocks lock independently of state- Specified by:
forceUnlock
in interfaceRLock
- Returns:
true
if unlocked otherwisefalse
-
forceUnlockAsync
public RFuture<Boolean> forceUnlockAsync()
Description copied from interface:RLockAsync
Unlocks lock independently of state- Specified by:
forceUnlockAsync
in interfaceRLockAsync
- Returns:
true
if unlocked otherwisefalse
-
isLocked
public boolean isLocked()
Description copied from interface:RLock
Checks if this lock locked by any thread
-
isExistsAsync
public RFuture<Boolean> isExistsAsync()
Description copied from interface:RObjectAsync
Check object existence in async mode.- Specified by:
isExistsAsync
in interfaceRObjectAsync
- Overrides:
isExistsAsync
in classRedissonObject
- Returns:
true
if object exists andfalse
otherwise
-
isHeldByCurrentThread
public boolean isHeldByCurrentThread()
Description copied from interface:RLock
Checks if this lock is held by the current thread- Specified by:
isHeldByCurrentThread
in interfaceRLock
- Returns:
true
if held by current thread otherwisefalse
-
getHoldCountAsync
public RFuture<Integer> getHoldCountAsync()
Description copied from interface:RLockAsync
Number of holds on this lock by the current thread- Specified by:
getHoldCountAsync
in interfaceRLockAsync
- Returns:
- holds or
0
if this lock is not held by current thread
-
getHoldCount
public int getHoldCount()
Description copied from interface:RLock
Number of holds on this lock by the current thread- Specified by:
getHoldCount
in interfaceRLock
- Returns:
- holds or
0
if this lock is not held by current thread
-
deleteAsync
public RFuture<Boolean> deleteAsync()
Description copied from interface:RObjectAsync
Delete object in async mode- Specified by:
deleteAsync
in interfaceRObjectAsync
- Overrides:
deleteAsync
in classRedissonObject
- Returns:
true
if object was deletedfalse
if not
-
unlockAsync
public RFuture<Void> unlockAsync()
- Specified by:
unlockAsync
in interfaceRLockAsync
-
unlockAsync
public RFuture<Void> unlockAsync(long threadId)
- Specified by:
unlockAsync
in interfaceRLockAsync
-
lockAsync
public RFuture<Void> lockAsync()
- Specified by:
lockAsync
in interfaceRLockAsync
-
lockAsync
public RFuture<Void> lockAsync(long leaseTime, TimeUnit unit)
Description copied from interface:RLockAsync
Acquires the lock.If the lock is not available then the current thread becomes disabled for thread scheduling purposes and lies dormant until the lock has been acquired. If the lock is acquired, it is held until
unlock
is invoked, or until leaseTime milliseconds have passed since the lock was granted - whichever comes first.- Specified by:
lockAsync
in interfaceRLockAsync
- Parameters:
leaseTime
- the maximum time to hold the lock after granting it, before automatically releasing it if it hasn't already been released by invokingunlock
. If leaseTime is -1, hold the lock until explicitly unlocked.unit
- the time unit of theleaseTime
argument- Returns:
- void
-
lockAsync
public RFuture<Void> lockAsync(long currentThreadId)
- Specified by:
lockAsync
in interfaceRLockAsync
-
lockAsync
public RFuture<Void> lockAsync(long leaseTime, TimeUnit unit, long currentThreadId)
- Specified by:
lockAsync
in interfaceRLockAsync
-
tryLockAsync
public RFuture<Boolean> tryLockAsync()
- Specified by:
tryLockAsync
in interfaceRLockAsync
-
tryLockAsync
public RFuture<Boolean> tryLockAsync(long threadId)
- Specified by:
tryLockAsync
in interfaceRLockAsync
-
tryLockAsync
public RFuture<Boolean> tryLockAsync(long waitTime, TimeUnit unit)
- Specified by:
tryLockAsync
in interfaceRLockAsync
-
tryLockAsync
public RFuture<Boolean> tryLockAsync(long waitTime, long leaseTime, TimeUnit unit)
- Specified by:
tryLockAsync
in interfaceRLockAsync
-
tryLockAsync
public RFuture<Boolean> tryLockAsync(long waitTime, long leaseTime, TimeUnit unit, long currentThreadId)
- Specified by:
tryLockAsync
in interfaceRLockAsync
-
expire
public boolean expire(long timeToLive, TimeUnit timeUnit)
Description copied from interface:RExpirable
Set a timeout for object. After the timeout has expired, the key will automatically be deleted.- Specified by:
expire
in interfaceRExpirable
- Parameters:
timeToLive
- - timeout before object will be deletedtimeUnit
- - timeout time unit- Returns:
true
if the timeout was set andfalse
if not
-
expireAsync
public RFuture<Boolean> expireAsync(long timeToLive, TimeUnit timeUnit)
Description copied from interface:RExpirableAsync
Set a timeout for object in async mode. After the timeout has expired, the key will automatically be deleted.- Specified by:
expireAsync
in interfaceRExpirableAsync
- Parameters:
timeToLive
- - timeout before object will be deletedtimeUnit
- - timeout time unit- Returns:
true
if the timeout was set andfalse
if not
-
expireAt
public boolean expireAt(long timestamp)
Description copied from interface:RExpirable
Set an expire date for object. When expire date comes the key will automatically be deleted.- Specified by:
expireAt
in interfaceRExpirable
- Parameters:
timestamp
- - expire date in milliseconds (Unix timestamp)- Returns:
true
if the timeout was set andfalse
if not
-
expireAtAsync
public RFuture<Boolean> expireAtAsync(long timestamp)
Description copied from interface:RExpirableAsync
Set an expire date for object in async mode. When expire date comes the key will automatically be deleted.- Specified by:
expireAtAsync
in interfaceRExpirableAsync
- Parameters:
timestamp
- - expire date in milliseconds (Unix timestamp)- Returns:
true
if the timeout was set andfalse
if not
-
expireAt
public boolean expireAt(Date timestamp)
Description copied from interface:RExpirable
Set an expire date for object. When expire date comes the key will automatically be deleted.- Specified by:
expireAt
in interfaceRExpirable
- Parameters:
timestamp
- - expire date- Returns:
true
if the timeout was set andfalse
if not
-
expireAtAsync
public RFuture<Boolean> expireAtAsync(Date timestamp)
Description copied from interface:RExpirableAsync
Set an expire date for object in async mode. When expire date comes the key will automatically be deleted.- Specified by:
expireAtAsync
in interfaceRExpirableAsync
- Parameters:
timestamp
- - expire date- Returns:
true
if the timeout was set andfalse
if not
-
clearExpire
public boolean clearExpire()
Description copied from interface:RExpirable
Clear an expire timeout or expire date for object.- Specified by:
clearExpire
in interfaceRExpirable
- Returns:
true
if timeout was removedfalse
if object does not exist or does not have an associated timeout
-
clearExpireAsync
public RFuture<Boolean> clearExpireAsync()
Description copied from interface:RExpirableAsync
Clear an expire timeout or expire date for object in async mode. Object will not be deleted.- Specified by:
clearExpireAsync
in interfaceRExpirableAsync
- Returns:
true
if the timeout was cleared andfalse
if not
-
remainTimeToLive
public long remainTimeToLive()
Description copied from interface:RExpirable
Remaining time to live of Redisson object that has a timeout- Specified by:
remainTimeToLive
in interfaceRExpirable
- Returns:
- time in milliseconds -2 if the key does not exist. -1 if the key exists but has no associated expire.
-
remainTimeToLiveAsync
public RFuture<Long> remainTimeToLiveAsync()
Description copied from interface:RExpirableAsync
Remaining time to live of Redisson object that has a timeout- Specified by:
remainTimeToLiveAsync
in interfaceRExpirableAsync
- Returns:
- time in milliseconds -2 if the key does not exist. -1 if the key exists but has no associated expire.
-
-