Package org.redisson

Class RedissonSemaphore

    • Method Detail

      • getChannelName

        public static String getChannelName​(String name)
      • acquire

        public void acquire()
                     throws InterruptedException
        Description copied from interface: RSemaphore
        Acquires a permit from this semaphore, blocking until one is available, or the thread is interrupted.

        Acquires a permit, if one is available and returns immediately, reducing the number of available permits by one.

        If no permit is available then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of two things happens:

        • Some other thread invokes the RSemaphore.release() method for this semaphore and the current thread is next to be assigned a permit; or
        • Some other thread interrupts the current thread.
        Specified by:
        acquire in interface RSemaphore
        Throws:
        InterruptedException - if the current thread is interrupted
      • acquire

        public void acquire​(int permits)
                     throws InterruptedException
        Description copied from interface: RSemaphore
        Acquires the given number of permits from this semaphore, blocking until all are available, or the thread is interrupted.

        Acquires the given number of permits, if they are available, and returns immediately, reducing the number of available permits by the given amount.

        If insufficient permits are available then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of two things happens:

        • Some other thread invokes one of the release methods for this semaphore, the current thread is next to be assigned permits and the number of available permits satisfies this request; or
        • Some other thread interrupts the current thread.
        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 from this semaphore.

        Acquires a permit, if one is available and returns immediately, reducing the number of available permits by one.

        Specified by:
        acquireAsync in interface RSemaphoreAsync
        Returns:
        void
      • acquireAsync

        public RFuture<Void> acquireAsync​(int permits)
        Description copied from interface: RSemaphoreAsync
        Acquires the given number of permits, if they are available, and returns immediately, reducing the number of available permits by the given amount.
        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
        Acquires a permit only if one is available at the time of invocation.

        Acquires a permit, if one is available and returns immediately, with the value true, reducing the number of available permits by one.

        If no permit is available then this method will return immediately with the value false.

        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
        Acquires the given number of permits only if all are available at the time of invocation.

        Acquires a permits, if all are available and returns immediately, with the value true, reducing the number of available permits by given number of permits.

        If no permits are available then this method will return immediately with the value false.

        Specified by:
        tryAcquire in interface RSemaphore
        Parameters:
        permits - the number of permits to acquire
        Returns:
        true if a permit was acquired and false otherwise
      • tryAcquireAsync

        public RFuture<Boolean> tryAcquireAsync()
        Description copied from interface: RSemaphoreAsync
        Acquires a permit only if one is available at the time of invocation.

        Acquires a permit, if one is available and returns immediately, with the value true, reducing the number of available permits by one.

        If no permit is available then this method will return immediately with the value false.

        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
        Acquires the given number of permits only if all are available at the time of invocation.

        Acquires a permits, if all are available and returns immediately, with the value true, reducing the number of available permits by given number of permitss.

        If no permits are available then this method will return immediately with the value false.

        Specified by:
        tryAcquireAsync in interface RSemaphoreAsync
        Parameters:
        permits - the number of permits to acquire
        Returns:
        true if a permit was acquired and false otherwise
      • tryAcquireAsync

        public RFuture<Boolean> tryAcquireAsync​(long waitTime,
                                                TimeUnit unit)
        Description copied from interface: RSemaphoreAsync

        Acquires a permit, if one is available and returns immediately, with the value true, reducing the number of available permits by one.

        If a permit is acquired then the value true is returned.

        If the specified waiting time elapses then the value false is returned. If the time is less than or equal to zero, the method will not wait at all.

        Specified by:
        tryAcquireAsync in interface RSemaphoreAsync
        Parameters:
        waitTime - the maximum time to wait for a permit
        unit - the time unit of the timeout argument
        Returns:
        true if a permit was acquired and false if the waiting time elapsed before a permit was acquired
      • tryAcquire

        public boolean tryAcquire​(int permits,
                                  long waitTime,
                                  TimeUnit unit)
                           throws InterruptedException
        Description copied from interface: RSemaphore
        Acquires the given number of permits only if all are available within the given waiting time and the current thread has not been interrupted.

        Acquires a permits, if all are available and returns immediately, with the value true, reducing the number of available permits by one.

        If no permit is available then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of three things happens:

        • Some other thread invokes the RSemaphore.release() method for this semaphore and the current thread is next to be assigned a permit; or
        • Some other thread interrupts the current thread; or
        • The specified waiting time elapses.

        If a permits is acquired then the value true is returned.

        If the specified waiting time elapses then the value false is returned. If the time is less than or equal to zero, the method will not wait at all.

        Specified by:
        tryAcquire in interface RSemaphore
        Parameters:
        permits - amount
        waitTime - the maximum time to wait for a permit
        unit - the time unit of the timeout argument
        Returns:
        true if a permit was acquired and false if the waiting time elapsed before a permit was acquired
        Throws:
        InterruptedException - if the current thread is interrupted
      • tryAcquireAsync

        public RFuture<Boolean> tryAcquireAsync​(int permits,
                                                long waitTime,
                                                TimeUnit unit)
        Description copied from interface: RSemaphoreAsync
        Acquires the given number of permits only if all are available within the given waiting time.

        Acquires a permits, if all are available and returns immediately, with the value true, reducing the number of available permits by one.

        If a permits is acquired then the value true is returned.

        If the specified waiting time elapses then the value false is returned. If the time is less than or equal to zero, the method will not wait at all.

        Specified by:
        tryAcquireAsync in interface RSemaphoreAsync
        Parameters:
        permits - amount
        waitTime - the maximum time to wait for a available permits
        unit - the time unit of the timeout argument
        Returns:
        true if a permit was acquired and false if the waiting time elapsed before a permit was acquired
      • tryAcquire

        public boolean tryAcquire​(long time,
                                  TimeUnit unit)
                           throws InterruptedException
        Description copied from interface: RSemaphore
        Acquires a permit from this semaphore, if one becomes available within the given waiting time and the current thread has not been interrupted.

        Acquires a permit, if one is available and returns immediately, with the value true, reducing the number of available permits by one.

        If no permit is available then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of three things happens:

        • Some other thread invokes the RSemaphore.release() method for this semaphore and the current thread is next to be assigned a permit; or
        • Some other thread interrupts the current thread; or
        • The specified waiting time elapses.

        If a permit is acquired then the value true is returned.

        If the specified waiting time elapses then the value false is returned. If the time is less than or equal to zero, the method will not wait at all.

        Specified by:
        tryAcquire in interface RSemaphore
        Parameters:
        time - the maximum time to wait for a permit
        unit - the time unit of the timeout argument
        Returns:
        true if a permit was acquired and false if the waiting time elapsed before a permit was acquired
        Throws:
        InterruptedException - if the current thread is interrupted
      • release

        public void release()
        Description copied from interface: RSemaphore
        Releases a permit, returning it to the semaphore.

        Releases a permit, increasing the number of available permits by one. If any threads of Redisson client are trying to acquire a permit, then one is selected and given the permit that was just released.

        There is no requirement that a thread that releases a permit must have acquired that permit by calling RSemaphore.acquire(). Correct usage of a semaphore is established by programming convention in the application.

        Specified by:
        release in interface RSemaphore
      • release

        public void release​(int permits)
        Description copied from interface: RSemaphore
        Releases the given number of permits, returning them to the semaphore.

        Releases the given number of permits, increasing the number of available permits by the given number of permits. If any threads of Redisson client are trying to acquire a permits, then next threads is selected and tries to acquire the permits that was just released.

        There is no requirement that a thread that releases a permits must have acquired that permit by calling RSemaphore.acquire(). Correct usage of a semaphore is established by programming convention in the application.

        Specified by:
        release in interface RSemaphore
        Parameters:
        permits - amount
      • releaseAsync

        public RFuture<Void> releaseAsync()
        Description copied from interface: RSemaphoreAsync
        Releases a permit, returning it to the semaphore.

        Releases a permit, increasing the number of available permits by one. If any threads of Redisson client are trying to acquire a permit, then one is selected and given the permit that was just released.

        There is no requirement that a thread that releases a permit must have acquired that permit by calling RSemaphoreAsync.acquireAsync(). Correct usage of a semaphore is established by programming convention in the application.

        Specified by:
        releaseAsync in interface RSemaphoreAsync
        Returns:
        void
      • releaseAsync

        public RFuture<Void> releaseAsync​(int permits)
        Description copied from interface: RSemaphoreAsync
        Releases the given number of permits, returning them to the semaphore.

        Releases the given number of permits, increasing the number of available permits by the given number of permits. If any threads of Redisson client are trying to acquire a permits, then next threads is selected and tries to acquire the permits that was just released.

        There is no requirement that a thread that releases a permits must have acquired that permit by calling RSemaphoreAsync.acquireAsync(). Correct usage of a semaphore is established by programming convention in the application.

        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:
        the number of permits acquired
      • availablePermits

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

        public boolean trySetPermits​(int permits)
        Description copied from interface: RSemaphore
        Sets number of permits.
        Specified by:
        trySetPermits in interface RSemaphore
        Parameters:
        permits - - number of permits
        Returns:
        true if permits has been set successfully, otherwise false.
      • trySetPermitsAsync

        public RFuture<Boolean> trySetPermitsAsync​(int permits)
        Description copied from interface: RSemaphoreAsync
        Sets number of permits.
        Specified by:
        trySetPermitsAsync in interface RSemaphoreAsync
        Parameters:
        permits - - number of permits
        Returns:
        true if permits has been set successfully, otherwise false.
      • reducePermits

        public void reducePermits​(int permits)
        Description copied from interface: RSemaphore
        Shrinks the number of available permits by the indicated reduction.
        Specified by:
        reducePermits in interface RSemaphore
        Parameters:
        permits - - reduction the number of permits to remove
      • reducePermitsAsync

        public RFuture<Void> reducePermitsAsync​(int permits)
        Description copied from interface: RSemaphoreAsync
        Shrinks the number of available permits by the indicated reduction. This method can be useful in subclasses that use semaphores to track resources that become unavailable. This method differs from RSemaphoreAsync.acquireAsync() in that it does not block waiting for permits to become available.
        Specified by:
        reducePermitsAsync in interface RSemaphoreAsync
        Parameters:
        permits - - reduction the number of permits to remove
        Returns:
        void
      • 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 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
        Set a timeout for object in async mode. After the timeout has expired, the key will automatically be deleted.
        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
        Set an expire date for object. When expire date comes the key will automatically be deleted.
        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
        Set an expire date for object in async mode. When expire date comes the key will automatically be deleted.
        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
      • 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 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
        Set an expire date for object in async mode. When expire date comes the key will automatically be deleted.
        Specified by:
        expireAtAsync in interface RExpirableAsync
        Parameters:
        timestamp - - expire date
        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.