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 of Semaphore.

Works in non-fair mode. Therefore order of acquiring is unpredictable.

Author:
Nikita Koksharov
  • Constructor Details

  • Method Details

    • getChannelName

      public static String getChannelName(String name)
    • 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 interface RSemaphore
      Throws:
      InterruptedException - if the current thread was interrupted
    • acquire

      public void acquire(int permits) throws InterruptedException
      Description copied from interface: RSemaphore
      Acquires defined amount of permits. Waits if necessary until all permits became available.
      Specified by:
      acquire in interface RSemaphore
      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 interface RSemaphoreAsync
      Returns:
      void
    • acquireAsync

      public RFuture<Void> acquireAsync(int permits)
      Description copied from interface: RSemaphoreAsync
      Acquires defined amount of permits. Waits if necessary until all permits became available.
      Specified by:
      acquireAsync in interface RSemaphoreAsync
      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 interface RSemaphore
      Returns:
      true if a permit was acquired and false otherwise
    • tryAcquire

      public boolean tryAcquire(int permits)
      Description copied from interface: RSemaphore
      Tries to acquire defined amount of currently available permits.
      Specified by:
      tryAcquire in interface RSemaphore
      Parameters:
      permits - the number of permits to acquire
      Returns:
      true if permits were acquired and false 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 interface RSemaphoreAsync
      Returns:
      true if a permit was acquired and false otherwise
    • tryAcquireAsync

      public RFuture<Boolean> tryAcquireAsync(int permits)
      Description copied from interface: RSemaphoreAsync
      Tries to acquire defined amount of currently available permits.
      Specified by:
      tryAcquireAsync in interface RSemaphoreAsync
      Parameters:
      permits - the number of permits to acquire
      Returns:
      true if permits were acquired and false otherwise
    • tryAcquireAsync

      public RFuture<Boolean> tryAcquireAsync(long waitTime, TimeUnit unit)
      Description copied from interface: RSemaphoreAsync
      Tries to acquire currently available permit. Waits up to defined waitTime if necessary until a permit became available.
      Specified by:
      tryAcquireAsync in interface RSemaphoreAsync
      Parameters:
      waitTime - the maximum time to wait
      unit - the time unit
      Returns:
      true if a permit was acquired and false 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 available permits. Waits up to defined waitTime if necessary until all permits became available.
      Specified by:
      tryAcquire in interface RSemaphore
      Parameters:
      permits - amount of permits
      waitTime - the maximum time to wait
      unit - the time unit
      Returns:
      true if permits were acquired and false 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 available permits. Waits up to defined waitTime if necessary until all permits became available.
      Specified by:
      tryAcquireAsync in interface RSemaphoreAsync
      Parameters:
      permits - amount of permits
      waitTime - the maximum time to wait
      unit - the time unit
      Returns:
      true if permits were acquired and false 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 defined waitTime if necessary until a permit became available.
      Specified by:
      tryAcquire in interface RSemaphore
      Parameters:
      time - the maximum time to wait
      unit - the time unit
      Returns:
      true if a permit was acquired and false 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 interface RSemaphore
    • release

      public void release(int permits)
      Description copied from interface: RSemaphore
      Releases defined amount of permits. Increases the number of available permits by permits amount.
      Specified by:
      release in interface RSemaphore
      Parameters:
      permits - amount of permits
    • releaseAsync

      public RFuture<Void> releaseAsync()
      Description copied from interface: RSemaphoreAsync
      Releases a permit.
      Specified by:
      releaseAsync in interface RSemaphoreAsync
      Returns:
      void
    • releaseAsync

      public RFuture<Void> releaseAsync(int permits)
      Description copied from interface: RSemaphoreAsync
      Releases defined amount of permits.
      Specified by:
      releaseAsync in interface RSemaphoreAsync
      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 interface RSemaphore
      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 interface RSemaphoreAsync
      Returns:
      number of permits
    • availablePermits

      public int availablePermits()
      Description copied from interface: RSemaphore
      Returns amount of available permits.
      Specified by:
      availablePermits in interface RSemaphore
      Returns:
      number of permits
    • availablePermitsAsync

      public RFuture<Integer> availablePermitsAsync()
      Description copied from interface: RSemaphoreAsync
      Returns amount of available permits.
      Specified by:
      availablePermitsAsync in interface RSemaphoreAsync
      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 interface RSemaphore
      Parameters:
      permits - - number of permits
      Returns:
      true if permits has been set successfully, otherwise false 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 interface RSemaphoreAsync
      Parameters:
      permits - - number of permits
      Returns:
      true if permits has been set successfully, otherwise false.
    • 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 interface RSemaphore
      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 interface RSemaphoreAsync
      Parameters:
      permits - amount of permits to add/remove
    • expire

      public boolean expire(long timeToLive, TimeUnit timeUnit)
      Description copied from interface: RExpirable
      Specified by:
      expire in interface RExpirable
      Parameters:
      timeToLive - - timeout before object will be deleted
      timeUnit - - timeout time unit
      Returns:
      true if the timeout was set and false if not
    • expireAsync

      public RFuture<Boolean> expireAsync(long timeToLive, TimeUnit timeUnit)
      Description copied from interface: RExpirableAsync
      Specified by:
      expireAsync in interface RExpirableAsync
      Parameters:
      timeToLive - - timeout before object will be deleted
      timeUnit - - timeout time unit
      Returns:
      true if the timeout was set and false if not
    • expireAt

      public boolean expireAt(long timestamp)
      Description copied from interface: RExpirable
      Specified by:
      expireAt in interface RExpirable
      Parameters:
      timestamp - - expire date in milliseconds (Unix timestamp)
      Returns:
      true if the timeout was set and false if not
    • expireAtAsync

      public RFuture<Boolean> expireAtAsync(long timestamp)
      Description copied from interface: RExpirableAsync
      Specified by:
      expireAtAsync in interface RExpirableAsync
      Parameters:
      timestamp - - expire date in milliseconds (Unix timestamp)
      Returns:
      true if the timeout was set and false if not
    • expire

      public boolean expire(Instant instant)
      Description copied from interface: RExpirable
      Sets an expiration date for this object. When expire date comes the key will automatically be deleted.
      Specified by:
      expire in interface RExpirable
      Parameters:
      instant - expire date
      Returns:
      true if the timeout was set and false if not
    • expireIfSet

      public boolean expireIfSet(Instant time)
      Description copied from interface: RExpirable
      Sets an expiration date for this object only if it has been already set. When expire date comes the object will automatically be deleted.

      Requires Redis 7.0.0 and higher.

      Specified by:
      expireIfSet in interface RExpirable
      Parameters:
      time - expire date
      Returns:
      true if the timeout was set and false if not
    • expireIfSetAsync

      public RFuture<Boolean> expireIfSetAsync(Instant time)
      Description copied from interface: RExpirableAsync
      Sets an expiration date for this object only if it has been already set. When expire date comes the object will automatically be deleted.

      Requires Redis 7.0.0 and higher.

      Specified by:
      expireIfSetAsync in interface RExpirableAsync
      Parameters:
      time - expire date
      Returns:
      true if the timeout was set and false if not
    • expireIfNotSet

      public boolean expireIfNotSet(Instant time)
      Description copied from interface: RExpirable
      Sets an expiration date for this object only if it hasn't been set before. When expire date comes the object will automatically be deleted.

      Requires Redis 7.0.0 and higher.

      Specified by:
      expireIfNotSet in interface RExpirable
      Parameters:
      time - expire date
      Returns:
      true if the timeout was set and false if not
    • expireIfNotSetAsync

      public RFuture<Boolean> expireIfNotSetAsync(Instant time)
      Description copied from interface: RExpirableAsync
      Sets an expiration date for this object only if it hasn't been set before. When expire date comes the object will automatically be deleted.

      Requires Redis 7.0.0 and higher.

      Specified by:
      expireIfNotSetAsync in interface RExpirableAsync
      Parameters:
      time - expire date
      Returns:
      true if the timeout was set and false if not
    • expireIfGreater

      public boolean expireIfGreater(Instant time)
      Description copied from interface: RExpirable
      Sets an expiration date for this object only if it's greater than expiration date set before. When expire date comes the object will automatically be deleted.

      Requires Redis 7.0.0 and higher.

      Specified by:
      expireIfGreater in interface RExpirable
      Parameters:
      time - expire date
      Returns:
      true if the timeout was set and false if not
    • expireIfGreaterAsync

      public RFuture<Boolean> expireIfGreaterAsync(Instant time)
      Description copied from interface: RExpirableAsync
      Sets an expiration date for this object only if it's greater than expiration date set before. When expire date comes the object will automatically be deleted.

      Requires Redis 7.0.0 and higher.

      Specified by:
      expireIfGreaterAsync in interface RExpirableAsync
      Parameters:
      time - expire date
      Returns:
      true if the timeout was set and false if not
    • expireIfLess

      public boolean expireIfLess(Instant time)
      Description copied from interface: RExpirable
      Sets an expiration date for this object only if it's less than expiration date set before. When expire date comes the object will automatically be deleted.

      Requires Redis 7.0.0 and higher.

      Specified by:
      expireIfLess in interface RExpirable
      Parameters:
      time - expire date
      Returns:
      true if the timeout was set and false if not
    • expireIfLessAsync

      public RFuture<Boolean> expireIfLessAsync(Instant time)
      Description copied from interface: RExpirableAsync
      Sets an expiration date for this object only if it's less than expiration date set before. When expire date comes the object will automatically be deleted.

      Requires Redis 7.0.0 and higher.

      Specified by:
      expireIfLessAsync in interface RExpirableAsync
      Parameters:
      time - expire date
      Returns:
      true if the timeout was set and false 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 interface RExpirableAsync
      Parameters:
      instant - - expire date
      Returns:
      true if the timeout was set and false if not
    • expire

      public boolean expire(Duration duration)
      Description copied from interface: RExpirable
      Sets a timeout for this object. After the timeout has expired, the key will automatically be deleted.
      Specified by:
      expire in interface RExpirable
      Parameters:
      duration - timeout before object will be deleted
      Returns:
      true if the timeout was set and false if not
    • expireAsync

      public RFuture<Boolean> expireAsync(Duration duration)
      Description copied from interface: RExpirableAsync
      Set a timeout for object. After the timeout has expired, the key will automatically be deleted.
      Specified by:
      expireAsync in interface RExpirableAsync
      Parameters:
      duration - timeout before object will be deleted
      Returns:
      true if the timeout was set and false if not
    • expireAt

      public boolean expireAt(Date timestamp)
      Description copied from interface: RExpirable
      Specified by:
      expireAt in interface RExpirable
      Parameters:
      timestamp - - expire date
      Returns:
      true if the timeout was set and false if not
    • expireAtAsync

      public RFuture<Boolean> expireAtAsync(Date timestamp)
      Description copied from interface: RExpirableAsync
      Specified by:
      expireAtAsync in interface RExpirableAsync
      Parameters:
      timestamp - - expire date
      Returns:
      true if the timeout was set and false if not
    • expireIfSet

      public boolean expireIfSet(Duration duration)
      Description copied from interface: RExpirable
      Sets a timeout for this object only if it has been already set. After the timeout has expired, the key will automatically be deleted.

      Requires Redis 7.0.0 and higher.

      Specified by:
      expireIfSet in interface RExpirable
      Parameters:
      duration - timeout before object will be deleted
      Returns:
      true if the timeout was set and false if not
    • expireIfSetAsync

      public RFuture<Boolean> expireIfSetAsync(Duration duration)
      Description copied from interface: RExpirableAsync
      Sets a timeout for this object only if it has been already set. After the timeout has expired, the key will automatically be deleted.

      Requires Redis 7.0.0 and higher.

      Specified by:
      expireIfSetAsync in interface RExpirableAsync
      Parameters:
      duration - timeout before object will be deleted
      Returns:
      true if the timeout was set and false if not
    • expireIfNotSet

      public boolean expireIfNotSet(Duration duration)
      Description copied from interface: RExpirable
      Sets a timeout for this object only if it hasn't been set before. After the timeout has expired, the key will automatically be deleted.

      Requires Redis 7.0.0 and higher.

      Specified by:
      expireIfNotSet in interface RExpirable
      Parameters:
      duration - timeout before object will be deleted
      Returns:
      true if the timeout was set and false if not
    • expireIfNotSetAsync

      public RFuture<Boolean> expireIfNotSetAsync(Duration duration)
      Description copied from interface: RExpirableAsync
      Sets a timeout for this object only if it hasn't been set before. After the timeout has expired, the key will automatically be deleted.

      Requires Redis 7.0.0 and higher.

      Specified by:
      expireIfNotSetAsync in interface RExpirableAsync
      Parameters:
      duration - timeout before object will be deleted
      Returns:
      true if the timeout was set and false if not
    • expireIfGreater

      public boolean expireIfGreater(Duration duration)
      Description copied from interface: RExpirable
      Sets a timeout for this object only if it's greater than timeout set before. After the timeout has expired, the key will automatically be deleted.

      Requires Redis 7.0.0 and higher.

      Specified by:
      expireIfGreater in interface RExpirable
      Parameters:
      duration - timeout before object will be deleted
      Returns:
      true if the timeout was set and false if not
    • expireIfGreaterAsync

      public RFuture<Boolean> expireIfGreaterAsync(Duration duration)
      Description copied from interface: RExpirableAsync
      Sets a timeout for this object only if it's greater than timeout set before. After the timeout has expired, the key will automatically be deleted.

      Requires Redis 7.0.0 and higher.

      Specified by:
      expireIfGreaterAsync in interface RExpirableAsync
      Parameters:
      duration - timeout before object will be deleted
      Returns:
      true if the timeout was set and false if not
    • expireIfLess

      public boolean expireIfLess(Duration duration)
      Description copied from interface: RExpirable
      Sets a timeout for this object only if it's less than timeout set before. After the timeout has expired, the key will automatically be deleted.

      Requires Redis 7.0.0 and higher.

      Specified by:
      expireIfLess in interface RExpirable
      Parameters:
      duration - timeout before object will be deleted
      Returns:
      true if the timeout was set and false if not
    • expireIfLessAsync

      public RFuture<Boolean> expireIfLessAsync(Duration duration)
      Description copied from interface: RExpirableAsync
      Sets a timeout for this object only if it's less than timeout set before. After the timeout has expired, the key will automatically be deleted.

      Requires Redis 7.0.0 and higher.

      Specified by:
      expireIfLessAsync in interface RExpirableAsync
      Parameters:
      duration - timeout before object will be deleted
      Returns:
      true if the timeout was set and false if not
    • clearExpire

      public boolean clearExpire()
      Description copied from interface: RExpirable
      Clear an expire timeout or expire date for object.
      Specified by:
      clearExpire in interface RExpirable
      Returns:
      true if timeout was removed false 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 interface RExpirableAsync
      Returns:
      true if the timeout was cleared and false 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 interface RExpirable
      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 interface RExpirableAsync
      Returns:
      time in milliseconds -2 if the key does not exist. -1 if the key exists but has no associated expire.
    • getExpireTime

      public long getExpireTime()
      Description copied from interface: RExpirable
      Expiration time of Redisson object that has a timeout

      Requires Redis 7.0.0 and higher.

      Specified by:
      getExpireTime in interface RExpirable
      Returns:
      expiration time
    • getExpireTimeAsync

      public RFuture<Long> getExpireTimeAsync()
      Description copied from interface: RExpirableAsync
      Expiration time of Redisson object that has a timeout

      Requires Redis 7.0.0 and higher.

      Specified by:
      getExpireTimeAsync in interface RExpirableAsync
      Returns:
      expiration time
    • expireAsync

      protected RFuture<Boolean> expireAsync(long timeToLive, TimeUnit timeUnit, String param, String... keys)
    • expireAtAsync

      protected RFuture<Boolean> expireAtAsync(long timestamp, String param, String... keys)
    • clearExpireAsync

      protected RFuture<Boolean> clearExpireAsync(String... keys)