Package org.redisson
Class RedissonSemaphore
- java.lang.Object
-
- org.redisson.RedissonObject
-
- org.redisson.RedissonSemaphore
-
- All Implemented Interfaces:
RExpirable
,RExpirableAsync
,RObject
,RObjectAsync
,RSemaphore
,RSemaphoreAsync
- Direct Known Subclasses:
RedissonQueueSemaphore
public class RedissonSemaphore extends RedissonObject implements RSemaphore
Distributed and concurrent implementation ofSemaphore
.Works in non-fair mode. Therefore order of acquiring is unpredictable.
- Author:
- Nikita Koksharov
-
-
Field Summary
-
Fields inherited from class org.redisson.RedissonObject
codec, name
-
-
Constructor Summary
Constructors Constructor Description RedissonSemaphore(CommandAsyncExecutor commandExecutor, String name)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
acquire()
Acquires a permit.void
acquire(int permits)
Acquires defined amount ofpermits
.RFuture<Void>
acquireAsync()
Acquires a permit.RFuture<Void>
acquireAsync(int permits)
Acquires defined amount ofpermits
.void
addPermits(int permits)
Increases or decreases the number of available permits by defined value.RFuture<Void>
addPermitsAsync(int permits)
Increases or decreases the number of available permits by defined value.int
availablePermits()
Returns amount of available permits.RFuture<Integer>
availablePermitsAsync()
Returns amount of available permits.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.protected RFuture<Boolean>
clearExpireAsync(String... keys)
int
drainPermits()
Acquires and returns all permits that are immediately available.RFuture<Integer>
drainPermitsAsync()
Acquires and returns all permits that are immediately available.boolean
expire(long timeToLive, TimeUnit timeUnit)
Set a timeout for object.boolean
expire(Instant instant)
Set an expire date for object.RFuture<Boolean>
expireAsync(long timeToLive, TimeUnit timeUnit)
Set a timeout for object in async mode.protected RFuture<Boolean>
expireAsync(long timeToLive, TimeUnit timeUnit, String... keys)
RFuture<Boolean>
expireAsync(Instant instant)
Set an expire date for object.boolean
expireAt(long timestamp)
UseRExpirable.expire(Instant)
insteadboolean
expireAt(Date timestamp)
UseRExpirable.expire(Instant)
insteadRFuture<Boolean>
expireAtAsync(long timestamp)
UseRExpirableAsync.expireAsync(Instant)
insteadprotected RFuture<Boolean>
expireAtAsync(long timestamp, String... keys)
RFuture<Boolean>
expireAtAsync(Date timestamp)
UseRExpirableAsync.expireAsync(Instant)
insteadstatic String
getChannelName(String name)
void
release()
Releases a permit.void
release(int permits)
Releases defined amount ofpermits
.RFuture<Void>
releaseAsync()
Releases a permit.RFuture<Void>
releaseAsync(int permits)
Releases defined amount ofpermits
.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 timeoutboolean
tryAcquire()
Tries to acquire currently available permit.boolean
tryAcquire(int permits)
Tries to acquire defined amount of currently availablepermits
.boolean
tryAcquire(int permits, long waitTime, TimeUnit unit)
Tries to acquire defined amount of currently availablepermits
.boolean
tryAcquire(long time, TimeUnit unit)
Tries to acquire currently available permit.RFuture<Boolean>
tryAcquireAsync()
Acquires a permit.RFuture<Boolean>
tryAcquireAsync(int permits)
Tries to acquire defined amount of currently availablepermits
.RFuture<Boolean>
tryAcquireAsync(int permits, long waitTime, TimeUnit unit)
Tries to acquire defined amount of currently availablepermits
.RFuture<Boolean>
tryAcquireAsync(long waitTime, TimeUnit unit)
Tries to acquire currently available permit.boolean
trySetPermits(int permits)
Tries to set number of permits.RFuture<Boolean>
trySetPermitsAsync(int permits)
Tries to set number of permits.-
Methods inherited from class org.redisson.RedissonObject
addListener, addListener, addListenerAsync, addListenerAsync, copy, copyAsync, delete, deleteAsync, deleteAsync, dump, dumpAsync, encode, encode, encode, encode, encodeMapKey, encodeMapKeys, encodeMapValue, encodeMapValues, get, getCodec, getIdleTime, getIdleTimeAsync, getLockByMapKey, getLockByValue, getName, getRawName, getRawName, isExists, isExistsAsync, migrate, migrateAsync, move, moveAsync, prefixName, removeListener, removeListenerAsync, removeListenersAsync, rename, renameAsync, renamenx, renamenxAsync, restore, restore, restoreAndReplace, restoreAndReplace, restoreAndReplaceAsync, restoreAndReplaceAsync, restoreAsync, restoreAsync, setName, sizeInMemory, sizeInMemoryAsync, sizeInMemoryAsync, sizeInMemoryAsync, suffixName, toSeconds, toStream, 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, expire, expireAt, expireAt, remainTimeToLive
-
Methods inherited from interface org.redisson.api.RExpirableAsync
clearExpireAsync, expireAsync, expireAsync, expireAtAsync, expireAtAsync, remainTimeToLiveAsync
-
Methods inherited from interface org.redisson.api.RObject
addListener, copy, delete, dump, getCodec, getIdleTime, getName, isExists, migrate, move, removeListener, rename, renamenx, restore, restore, restoreAndReplace, restoreAndReplace, sizeInMemory, touch, unlink
-
Methods inherited from interface org.redisson.api.RObjectAsync
addListenerAsync, copyAsync, deleteAsync, dumpAsync, getIdleTimeAsync, isExistsAsync, migrateAsync, moveAsync, removeListenerAsync, renameAsync, renamenxAsync, restoreAndReplaceAsync, restoreAndReplaceAsync, restoreAsync, restoreAsync, sizeInMemoryAsync, touchAsync, unlinkAsync
-
-
-
-
Constructor Detail
-
RedissonSemaphore
public RedissonSemaphore(CommandAsyncExecutor commandExecutor, String name)
-
-
Method Detail
-
acquire
public void acquire() throws InterruptedException
Description copied from interface:RSemaphore
Acquires a permit. Waits if necessary until a permit became available.- Specified by:
acquire
in interfaceRSemaphore
- Throws:
InterruptedException
- if the current thread was interrupted
-
acquire
public void acquire(int permits) throws InterruptedException
Description copied from interface:RSemaphore
Acquires defined amount ofpermits
. Waits if necessary until all permits became available.- Specified by:
acquire
in interfaceRSemaphore
- Parameters:
permits
- the number of permits to acquire- Throws:
InterruptedException
- if the current thread is interrupted
-
acquireAsync
public RFuture<Void> acquireAsync()
Description copied from interface:RSemaphoreAsync
Acquires a permit. Waits if necessary until a permit became available.- Specified by:
acquireAsync
in interfaceRSemaphoreAsync
- Returns:
- void
-
acquireAsync
public RFuture<Void> acquireAsync(int permits)
Description copied from interface:RSemaphoreAsync
Acquires defined amount ofpermits
. Waits if necessary until all permits became available.- Specified by:
acquireAsync
in interfaceRSemaphoreAsync
- Parameters:
permits
- the number of permits to acquire- Returns:
- void
-
tryAcquire
public boolean tryAcquire()
Description copied from interface:RSemaphore
Tries to acquire currently available permit.- Specified by:
tryAcquire
in interfaceRSemaphore
- Returns:
true
if a permit was acquired andfalse
otherwise
-
tryAcquire
public boolean tryAcquire(int permits)
Description copied from interface:RSemaphore
Tries to acquire defined amount of currently availablepermits
.- Specified by:
tryAcquire
in interfaceRSemaphore
- Parameters:
permits
- the number of permits to acquire- Returns:
true
if permits were acquired andfalse
otherwise
-
tryAcquireAsync
public RFuture<Boolean> tryAcquireAsync()
Description copied from interface:RSemaphoreAsync
Acquires a permit. Waits if necessary until a permit became available.- Specified by:
tryAcquireAsync
in interfaceRSemaphoreAsync
- Returns:
true
if a permit was acquired andfalse
otherwise
-
tryAcquireAsync
public RFuture<Boolean> tryAcquireAsync(int permits)
Description copied from interface:RSemaphoreAsync
Tries to acquire defined amount of currently availablepermits
.- Specified by:
tryAcquireAsync
in interfaceRSemaphoreAsync
- Parameters:
permits
- the number of permits to acquire- Returns:
true
if permits were acquired andfalse
otherwise
-
tryAcquireAsync
public RFuture<Boolean> tryAcquireAsync(long waitTime, TimeUnit unit)
Description copied from interface:RSemaphoreAsync
Tries to acquire currently available permit. Waits up to definedwaitTime
if necessary until a permit became available.- Specified by:
tryAcquireAsync
in interfaceRSemaphoreAsync
- Parameters:
waitTime
- the maximum time to waitunit
- the time unit- Returns:
true
if a permit was acquired andfalse
otherwise
-
tryAcquire
public boolean tryAcquire(int permits, long waitTime, TimeUnit unit) throws InterruptedException
Description copied from interface:RSemaphore
Tries to acquire defined amount of currently availablepermits
. Waits up to definedwaitTime
if necessary until all permits became available.- Specified by:
tryAcquire
in interfaceRSemaphore
- Parameters:
permits
- amount of permitswaitTime
- the maximum time to waitunit
- the time unit- Returns:
true
if permits were acquired andfalse
otherwise- Throws:
InterruptedException
- if the current thread was interrupted
-
tryAcquireAsync
public RFuture<Boolean> tryAcquireAsync(int permits, long waitTime, TimeUnit unit)
Description copied from interface:RSemaphoreAsync
Tries to acquire defined amount of currently availablepermits
. Waits up to definedwaitTime
if necessary until all permits became available.- Specified by:
tryAcquireAsync
in interfaceRSemaphoreAsync
- Parameters:
permits
- amount of permitswaitTime
- the maximum time to waitunit
- the time unit- Returns:
true
if permits were acquired andfalse
otherwise
-
tryAcquire
public boolean tryAcquire(long time, TimeUnit unit) throws InterruptedException
Description copied from interface:RSemaphore
Tries to acquire currently available permit. Waits up to definedwaitTime
if necessary until a permit became available.- Specified by:
tryAcquire
in interfaceRSemaphore
- Parameters:
time
- the maximum time to waitunit
- the time unit- Returns:
true
if a permit was acquired andfalse
otherwise- Throws:
InterruptedException
- if the current thread was interrupted
-
release
public void release()
Description copied from interface:RSemaphore
Releases a permit. Increases the number of available permits.- Specified by:
release
in interfaceRSemaphore
-
release
public void release(int permits)
Description copied from interface:RSemaphore
Releases defined amount ofpermits
. Increases the number of available permits bypermits
amount.- Specified by:
release
in interfaceRSemaphore
- Parameters:
permits
- amount of permits
-
releaseAsync
public RFuture<Void> releaseAsync()
Description copied from interface:RSemaphoreAsync
Releases a permit.- Specified by:
releaseAsync
in interfaceRSemaphoreAsync
- Returns:
- void
-
releaseAsync
public RFuture<Void> releaseAsync(int permits)
Description copied from interface:RSemaphoreAsync
Releases defined amount ofpermits
.- Specified by:
releaseAsync
in interfaceRSemaphoreAsync
- Parameters:
permits
- amount- Returns:
- void
-
drainPermits
public int drainPermits()
Description copied from interface:RSemaphore
Acquires and returns all permits that are immediately available.- Specified by:
drainPermits
in interfaceRSemaphore
- Returns:
- number of permits
-
drainPermitsAsync
public RFuture<Integer> drainPermitsAsync()
Description copied from interface:RSemaphoreAsync
Acquires and returns all permits that are immediately available.- Specified by:
drainPermitsAsync
in interfaceRSemaphoreAsync
- Returns:
- number of permits
-
availablePermits
public int availablePermits()
Description copied from interface:RSemaphore
Returns amount of available permits.- Specified by:
availablePermits
in interfaceRSemaphore
- Returns:
- number of permits
-
availablePermitsAsync
public RFuture<Integer> availablePermitsAsync()
Description copied from interface:RSemaphoreAsync
Returns amount of available permits.- Specified by:
availablePermitsAsync
in interfaceRSemaphoreAsync
- Returns:
- number of permits
-
trySetPermits
public boolean trySetPermits(int permits)
Description copied from interface:RSemaphore
Tries to set number of permits.- Specified by:
trySetPermits
in interfaceRSemaphore
- Parameters:
permits
- - number of permits- Returns:
true
if permits has been set successfully, otherwisefalse
if permits were already set.
-
trySetPermitsAsync
public RFuture<Boolean> trySetPermitsAsync(int permits)
Description copied from interface:RSemaphoreAsync
Tries to set number of permits.- Specified by:
trySetPermitsAsync
in interfaceRSemaphoreAsync
- Parameters:
permits
- - number of permits- Returns:
true
if permits has been set successfully, otherwisefalse
.
-
addPermits
public void addPermits(int permits)
Description copied from interface:RSemaphore
Increases or decreases the number of available permits by defined value.- Specified by:
addPermits
in interfaceRSemaphore
- Parameters:
permits
- amount of permits to add/remove
-
addPermitsAsync
public RFuture<Void> addPermitsAsync(int permits)
Description copied from interface:RSemaphoreAsync
Increases or decreases the number of available permits by defined value.- Specified by:
addPermitsAsync
in interfaceRSemaphoreAsync
- Parameters:
permits
- amount of permits to add/remove
-
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
UseRExpirable.expire(Instant)
instead- 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
UseRExpirableAsync.expireAsync(Instant)
instead- Specified by:
expireAtAsync
in interfaceRExpirableAsync
- Parameters:
timestamp
- - expire date in milliseconds (Unix timestamp)- Returns:
true
if the timeout was set andfalse
if not
-
expire
public boolean expire(Instant instant)
Description copied from interface:RExpirable
Set an expire date for object. When expire date comes the key will automatically be deleted.- Specified by:
expire
in interfaceRExpirable
- Parameters:
instant
- - expire date- Returns:
true
if the timeout was set andfalse
if not
-
expireAsync
public RFuture<Boolean> expireAsync(Instant instant)
Description copied from interface:RExpirableAsync
Set an expire date for object. When expire date comes the key will automatically be deleted.- Specified by:
expireAsync
in interfaceRExpirableAsync
- Parameters:
instant
- - expire date- Returns:
true
if the timeout was set andfalse
if not
-
expireAt
public boolean expireAt(Date timestamp)
Description copied from interface:RExpirable
UseRExpirable.expire(Instant)
instead- 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
UseRExpirableAsync.expireAsync(Instant)
instead- 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.
-
expireAsync
protected RFuture<Boolean> expireAsync(long timeToLive, TimeUnit timeUnit, String... keys)
-
-