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 voidacquire()Acquires a permit.voidacquire(int permits)Acquires defined amount ofpermits.RFuture<Void>acquireAsync()Acquires a permit.RFuture<Void>acquireAsync(int permits)Acquires defined amount ofpermits.voidaddPermits(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.intavailablePermits()Returns amount of available permits.RFuture<Integer>availablePermitsAsync()Returns amount of available permits.booleanclearExpire()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)intdrainPermits()Acquires and returns all permits that are immediately available.RFuture<Integer>drainPermitsAsync()Acquires and returns all permits that are immediately available.booleanexpire(long timeToLive, TimeUnit timeUnit)Set a timeout for object.booleanexpire(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.booleanexpireAt(long timestamp)UseRExpirable.expire(Instant)insteadbooleanexpireAt(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 StringgetChannelName(String name)voidrelease()Releases a permit.voidrelease(int permits)Releases defined amount ofpermits.RFuture<Void>releaseAsync()Releases a permit.RFuture<Void>releaseAsync(int permits)Releases defined amount ofpermits.longremainTimeToLive()Remaining time to live of Redisson object that has a timeoutRFuture<Long>remainTimeToLiveAsync()Remaining time to live of Redisson object that has a timeoutbooleantryAcquire()Tries to acquire currently available permit.booleantryAcquire(int permits)Tries to acquire defined amount of currently availablepermits.booleantryAcquire(int permits, long waitTime, TimeUnit unit)Tries to acquire defined amount of currently availablepermits.booleantryAcquire(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.booleantrySetPermits(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 InterruptedExceptionDescription copied from interface:RSemaphoreAcquires a permit. Waits if necessary until a permit became available.- Specified by:
acquirein interfaceRSemaphore- Throws:
InterruptedException- if the current thread was interrupted
-
acquire
public void acquire(int permits) throws InterruptedExceptionDescription copied from interface:RSemaphoreAcquires defined amount ofpermits. Waits if necessary until all permits became available.- Specified by:
acquirein 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:RSemaphoreAsyncAcquires a permit. Waits if necessary until a permit became available.- Specified by:
acquireAsyncin interfaceRSemaphoreAsync- Returns:
- void
-
acquireAsync
public RFuture<Void> acquireAsync(int permits)
Description copied from interface:RSemaphoreAsyncAcquires defined amount ofpermits. Waits if necessary until all permits became available.- Specified by:
acquireAsyncin interfaceRSemaphoreAsync- Parameters:
permits- the number of permits to acquire- Returns:
- void
-
tryAcquire
public boolean tryAcquire()
Description copied from interface:RSemaphoreTries to acquire currently available permit.- Specified by:
tryAcquirein interfaceRSemaphore- Returns:
trueif a permit was acquired andfalseotherwise
-
tryAcquire
public boolean tryAcquire(int permits)
Description copied from interface:RSemaphoreTries to acquire defined amount of currently availablepermits.- Specified by:
tryAcquirein interfaceRSemaphore- Parameters:
permits- the number of permits to acquire- Returns:
trueif permits were acquired andfalseotherwise
-
tryAcquireAsync
public RFuture<Boolean> tryAcquireAsync()
Description copied from interface:RSemaphoreAsyncAcquires a permit. Waits if necessary until a permit became available.- Specified by:
tryAcquireAsyncin interfaceRSemaphoreAsync- Returns:
trueif a permit was acquired andfalseotherwise
-
tryAcquireAsync
public RFuture<Boolean> tryAcquireAsync(int permits)
Description copied from interface:RSemaphoreAsyncTries to acquire defined amount of currently availablepermits.- Specified by:
tryAcquireAsyncin interfaceRSemaphoreAsync- Parameters:
permits- the number of permits to acquire- Returns:
trueif permits were acquired andfalseotherwise
-
tryAcquireAsync
public RFuture<Boolean> tryAcquireAsync(long waitTime, TimeUnit unit)
Description copied from interface:RSemaphoreAsyncTries to acquire currently available permit. Waits up to definedwaitTimeif necessary until a permit became available.- Specified by:
tryAcquireAsyncin interfaceRSemaphoreAsync- Parameters:
waitTime- the maximum time to waitunit- the time unit- Returns:
trueif a permit was acquired andfalseotherwise
-
tryAcquire
public boolean tryAcquire(int permits, long waitTime, TimeUnit unit) throws InterruptedExceptionDescription copied from interface:RSemaphoreTries to acquire defined amount of currently availablepermits. Waits up to definedwaitTimeif necessary until all permits became available.- Specified by:
tryAcquirein interfaceRSemaphore- Parameters:
permits- amount of permitswaitTime- the maximum time to waitunit- the time unit- Returns:
trueif permits were acquired andfalseotherwise- Throws:
InterruptedException- if the current thread was interrupted
-
tryAcquireAsync
public RFuture<Boolean> tryAcquireAsync(int permits, long waitTime, TimeUnit unit)
Description copied from interface:RSemaphoreAsyncTries to acquire defined amount of currently availablepermits. Waits up to definedwaitTimeif necessary until all permits became available.- Specified by:
tryAcquireAsyncin interfaceRSemaphoreAsync- Parameters:
permits- amount of permitswaitTime- the maximum time to waitunit- the time unit- Returns:
trueif permits were acquired andfalseotherwise
-
tryAcquire
public boolean tryAcquire(long time, TimeUnit unit) throws InterruptedExceptionDescription copied from interface:RSemaphoreTries to acquire currently available permit. Waits up to definedwaitTimeif necessary until a permit became available.- Specified by:
tryAcquirein interfaceRSemaphore- Parameters:
time- the maximum time to waitunit- the time unit- Returns:
trueif a permit was acquired andfalseotherwise- Throws:
InterruptedException- if the current thread was interrupted
-
release
public void release()
Description copied from interface:RSemaphoreReleases a permit. Increases the number of available permits.- Specified by:
releasein interfaceRSemaphore
-
release
public void release(int permits)
Description copied from interface:RSemaphoreReleases defined amount ofpermits. Increases the number of available permits bypermitsamount.- Specified by:
releasein interfaceRSemaphore- Parameters:
permits- amount of permits
-
releaseAsync
public RFuture<Void> releaseAsync()
Description copied from interface:RSemaphoreAsyncReleases a permit.- Specified by:
releaseAsyncin interfaceRSemaphoreAsync- Returns:
- void
-
releaseAsync
public RFuture<Void> releaseAsync(int permits)
Description copied from interface:RSemaphoreAsyncReleases defined amount ofpermits.- Specified by:
releaseAsyncin interfaceRSemaphoreAsync- Parameters:
permits- amount- Returns:
- void
-
drainPermits
public int drainPermits()
Description copied from interface:RSemaphoreAcquires and returns all permits that are immediately available.- Specified by:
drainPermitsin interfaceRSemaphore- Returns:
- number of permits
-
drainPermitsAsync
public RFuture<Integer> drainPermitsAsync()
Description copied from interface:RSemaphoreAsyncAcquires and returns all permits that are immediately available.- Specified by:
drainPermitsAsyncin interfaceRSemaphoreAsync- Returns:
- number of permits
-
availablePermits
public int availablePermits()
Description copied from interface:RSemaphoreReturns amount of available permits.- Specified by:
availablePermitsin interfaceRSemaphore- Returns:
- number of permits
-
availablePermitsAsync
public RFuture<Integer> availablePermitsAsync()
Description copied from interface:RSemaphoreAsyncReturns amount of available permits.- Specified by:
availablePermitsAsyncin interfaceRSemaphoreAsync- Returns:
- number of permits
-
trySetPermits
public boolean trySetPermits(int permits)
Description copied from interface:RSemaphoreTries to set number of permits.- Specified by:
trySetPermitsin interfaceRSemaphore- Parameters:
permits- - number of permits- Returns:
trueif permits has been set successfully, otherwisefalseif permits were already set.
-
trySetPermitsAsync
public RFuture<Boolean> trySetPermitsAsync(int permits)
Description copied from interface:RSemaphoreAsyncTries to set number of permits.- Specified by:
trySetPermitsAsyncin interfaceRSemaphoreAsync- Parameters:
permits- - number of permits- Returns:
trueif permits has been set successfully, otherwisefalse.
-
addPermits
public void addPermits(int permits)
Description copied from interface:RSemaphoreIncreases or decreases the number of available permits by defined value.- Specified by:
addPermitsin interfaceRSemaphore- Parameters:
permits- amount of permits to add/remove
-
addPermitsAsync
public RFuture<Void> addPermitsAsync(int permits)
Description copied from interface:RSemaphoreAsyncIncreases or decreases the number of available permits by defined value.- Specified by:
addPermitsAsyncin interfaceRSemaphoreAsync- Parameters:
permits- amount of permits to add/remove
-
expire
public boolean expire(long timeToLive, TimeUnit timeUnit)Description copied from interface:RExpirableSet a timeout for object. After the timeout has expired, the key will automatically be deleted.- Specified by:
expirein interfaceRExpirable- Parameters:
timeToLive- - timeout before object will be deletedtimeUnit- - timeout time unit- Returns:
trueif the timeout was set andfalseif not
-
expireAsync
public RFuture<Boolean> expireAsync(long timeToLive, TimeUnit timeUnit)
Description copied from interface:RExpirableAsyncSet a timeout for object in async mode. After the timeout has expired, the key will automatically be deleted.- Specified by:
expireAsyncin interfaceRExpirableAsync- Parameters:
timeToLive- - timeout before object will be deletedtimeUnit- - timeout time unit- Returns:
trueif the timeout was set andfalseif not
-
expireAt
public boolean expireAt(long timestamp)
Description copied from interface:RExpirableUseRExpirable.expire(Instant)instead- Specified by:
expireAtin interfaceRExpirable- Parameters:
timestamp- - expire date in milliseconds (Unix timestamp)- Returns:
trueif the timeout was set andfalseif not
-
expireAtAsync
public RFuture<Boolean> expireAtAsync(long timestamp)
Description copied from interface:RExpirableAsyncUseRExpirableAsync.expireAsync(Instant)instead- Specified by:
expireAtAsyncin interfaceRExpirableAsync- Parameters:
timestamp- - expire date in milliseconds (Unix timestamp)- Returns:
trueif the timeout was set andfalseif not
-
expire
public boolean expire(Instant instant)
Description copied from interface:RExpirableSet an expire date for object. When expire date comes the key will automatically be deleted.- Specified by:
expirein interfaceRExpirable- Parameters:
instant- - expire date- Returns:
trueif the timeout was set andfalseif not
-
expireAsync
public RFuture<Boolean> expireAsync(Instant instant)
Description copied from interface:RExpirableAsyncSet an expire date for object. When expire date comes the key will automatically be deleted.- Specified by:
expireAsyncin interfaceRExpirableAsync- Parameters:
instant- - expire date- Returns:
trueif the timeout was set andfalseif not
-
expireAt
public boolean expireAt(Date timestamp)
Description copied from interface:RExpirableUseRExpirable.expire(Instant)instead- Specified by:
expireAtin interfaceRExpirable- Parameters:
timestamp- - expire date- Returns:
trueif the timeout was set andfalseif not
-
expireAtAsync
public RFuture<Boolean> expireAtAsync(Date timestamp)
Description copied from interface:RExpirableAsyncUseRExpirableAsync.expireAsync(Instant)instead- Specified by:
expireAtAsyncin interfaceRExpirableAsync- Parameters:
timestamp- - expire date- Returns:
trueif the timeout was set andfalseif not
-
clearExpire
public boolean clearExpire()
Description copied from interface:RExpirableClear an expire timeout or expire date for object.- Specified by:
clearExpirein interfaceRExpirable- Returns:
trueif timeout was removedfalseif object does not exist or does not have an associated timeout
-
clearExpireAsync
public RFuture<Boolean> clearExpireAsync()
Description copied from interface:RExpirableAsyncClear an expire timeout or expire date for object in async mode. Object will not be deleted.- Specified by:
clearExpireAsyncin interfaceRExpirableAsync- Returns:
trueif the timeout was cleared andfalseif not
-
remainTimeToLive
public long remainTimeToLive()
Description copied from interface:RExpirableRemaining time to live of Redisson object that has a timeout- Specified by:
remainTimeToLivein 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:RExpirableAsyncRemaining time to live of Redisson object that has a timeout- Specified by:
remainTimeToLiveAsyncin 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)
-
-