8.1. Lock

Redis based distributed reentrant Lock object for Java and implements Lock interface. Uses pub/sub channel to notify other threads across all Redisson instances waiting to acquire a lock.

If Redisson instance which acquired lock crashes then such lock could hang forever in acquired state. To avoid this Redisson maintains lock watchdog, it prolongs lock expiration while lock holder Redisson instance is alive. By default lock watchdog timeout is 30 seconds and can be changed through Config.lockWatchdogTimeout setting.

leaseTime parameter during lock acquisition can be defined. After specified time interval locked lock will be released automatically.

RLock object behaves according to the Java Lock specification. It means only lock owner thread can unlock it otherwise IllegalMonitorStateException would be thrown. Otherwise consider to use RSemaphore object.

Code example:

  1. RLock lock = redisson.getLock("myLock");
  2. // traditional lock method
  3. lock.lock();
  4. // or acquire lock and automatically unlock it after 10 seconds
  5. lock.lock(10, TimeUnit.SECONDS);
  6. // or wait for lock aquisition up to 100 seconds
  7. // and automatically unlock it after 10 seconds
  8. boolean res = lock.tryLock(100, 10, TimeUnit.SECONDS);
  9. if (res) {
  10. try {
  11. ...
  12. } finally {
  13. lock.unlock();
  14. }
  15. }

Code example of Async interface usage:

  1. RLock lock = redisson.getLock("myLock");
  2. RFuture<Void> lockFuture = lock.lockAsync();
  3. // or acquire lock and automatically unlock it after 10 seconds
  4. RFuture<Void> lockFuture = lock.lockAsync(10, TimeUnit.SECONDS);
  5. // or wait for lock aquisition up to 100 seconds
  6. // and automatically unlock it after 10 seconds
  7. RFuture<Boolean> lockFuture = lock.tryLockAsync(100, 10, TimeUnit.SECONDS);
  8. lockFuture.whenComplete((res, exception) -> {
  9. // ...
  10. lock.unlockAsync();
  11. });

Code example of Reactive interface usage:

  1. RedissonReactiveClient redisson = redissonClient.reactive();
  2. RLockReactive lock = redisson.getLock("myLock");
  3. Mono<Void> lockMono = lock.lock();
  4. // or acquire lock and automatically unlock it after 10 seconds
  5. Mono<Void> lockMono = lock.lock(10, TimeUnit.SECONDS);
  6. // or wait for lock aquisition up to 100 seconds
  7. // and automatically unlock it after 10 seconds
  8. Mono<Boolean> lockMono = lock.tryLock(100, 10, TimeUnit.SECONDS);
  9. lockMono.doOnNext(res -> {
  10. // ...
  11. })
  12. .doFinally(lock.unlock())
  13. .subscribe();

Code example of RxJava3 interface usage:

  1. RedissonRxClient redisson = redissonClient.rxJava();
  2. RLockRx lock = redisson.getLock("myLock");
  3. Completable lockRes = lock.lock();
  4. // or acquire lock and automatically unlock it after 10 seconds
  5. Completable lockRes = lock.lock(10, TimeUnit.SECONDS);
  6. // or wait for lock aquisition up to 100 seconds
  7. // and automatically unlock it after 10 seconds
  8. Single<Boolean> lockRes = lock.tryLock(100, 10, TimeUnit.SECONDS);
  9. lockRes.doOnSuccess(res -> {
  10. // ...
  11. })
  12. .doFinally(lock.unlock())
  13. .subscribe();

8.2. Fair Lock

Redis based distributed reentrant fair Lock object for Java implements Lock interface.

Fair lock guarantees that threads will acquire it in is same order they requested it. All waiting threads are queued and if some thread has died then Redisson waits its return for 5 seconds. For example, if 5 threads are died for some reason then delay will be 25 seconds.

If Redisson instance which acquired lock crashes then such lock could hang forever in acquired state. To avoid this Redisson maintains lock watchdog, it prolongs lock expiration while lock holder Redisson instance is alive. By default lock watchdog timeout is 30 seconds and can be changed through Config.lockWatchdogTimeout setting.

leaseTime parameter during lock acquisition can be defined. After specified time interval locked lock will be released automatically.

RLock object behaves according to the Java Lock specification. It means only lock owner thread can unlock it otherwise IllegalMonitorStateException would be thrown. Otherwise consider to use RSemaphore object.

Code example:

  1. RLock lock = redisson.getFairLock("myLock");
  2. // traditional lock method
  3. lock.lock();
  4. // or acquire lock and automatically unlock it after 10 seconds
  5. lock.lock(10, TimeUnit.SECONDS);
  6. // or wait for lock aquisition up to 100 seconds
  7. // and automatically unlock it after 10 seconds
  8. boolean res = lock.tryLock(100, 10, TimeUnit.SECONDS);
  9. if (res) {
  10. try {
  11. ...
  12. } finally {
  13. lock.unlock();
  14. }
  15. }

Code example of Async interface usage:

  1. RLock lock = redisson.getFairLock("myLock");
  2. RFuture<Void> lockFuture = lock.lockAsync();
  3. // or acquire lock and automatically unlock it after 10 seconds
  4. RFuture<Void> lockFuture = lock.lockAsync(10, TimeUnit.SECONDS);
  5. // or wait for lock aquisition up to 100 seconds
  6. // and automatically unlock it after 10 seconds
  7. RFuture<Boolean> lockFuture = lock.tryLockAsync(100, 10, TimeUnit.SECONDS);
  8. lockFuture.whenComplete((res, exception) -> {
  9. // ...
  10. lock.unlockAsync();
  11. });

Code example of Reactive interface usage:

  1. RedissonReactiveClient redisson = redissonClient.reactive();
  2. RLockReactive lock = redisson.getFairLock("myLock");
  3. Mono<Void> lockMono = lock.lock();
  4. // or acquire lock and automatically unlock it after 10 seconds
  5. Mono<Void> lockMono = lock.lock(10, TimeUnit.SECONDS);
  6. // or wait for lock aquisition up to 100 seconds
  7. // and automatically unlock it after 10 seconds
  8. Mono<Boolean> lockMono = lock.tryLock(100, 10, TimeUnit.SECONDS);
  9. lockMono.doOnNext(res -> {
  10. // ...
  11. })
  12. .doFinally(lock.unlock())
  13. .subscribe();

Code example of RxJava3 interface usage:

  1. RedissonRxClient redisson = redissonClient.rxJava();
  2. RLockRx lock = redisson.getFairLock("myLock");
  3. Completable lockRes = lock.lock();
  4. // or acquire lock and automatically unlock it after 10 seconds
  5. Completable lockRes = lock.lock(10, TimeUnit.SECONDS);
  6. // or wait for lock aquisition up to 100 seconds
  7. // and automatically unlock it after 10 seconds
  8. Single<Boolean> lockRes = lock.tryLock(100, 10, TimeUnit.SECONDS);
  9. lockRes.doOnSuccess(res -> {
  10. // ...
  11. })
  12. .doFinally(lock.unlock())
  13. .subscribe();

8.3. MultiLock

Redis based distributed MultiLock object allows to group Lock objects and handle them as a single lock. Each RLock object may belong to different Redisson instances.

If Redisson instance which acquired MultiLock crashes then such MultiLock could hang forever in acquired state. To avoid this Redisson maintains lock watchdog, it prolongs lock expiration while lock holder Redisson instance is alive. By default lock watchdog timeout is 30 seconds and can be changed through Config.lockWatchdogTimeout setting.

leaseTime parameter during lock acquisition can be defined. After specified time interval locked lock will be released automatically.

MultiLock object behaves according to the Java Lock specification. It means only lock owner thread can unlock it otherwise IllegalMonitorStateException would be thrown. Otherwise consider to use RSemaphore object.

Code example:

  1. RLock lock1 = redisson1.getLock("lock1");
  2. RLock lock2 = redisson2.getLock("lock2");
  3. RLock lock3 = redisson3.getLock("lock3");
  4. RLock multiLock = anyRedisson.getMultiLock(lock1, lock2, lock3);
  5. // traditional lock method
  6. multiLock.lock();
  7. // or acquire lock and automatically unlock it after 10 seconds
  8. multiLock.lock(10, TimeUnit.SECONDS);
  9. // or wait for lock aquisition up to 100 seconds
  10. // and automatically unlock it after 10 seconds
  11. boolean res = multiLock.tryLock(100, 10, TimeUnit.SECONDS);
  12. if (res) {
  13. try {
  14. ...
  15. } finally {
  16. multiLock.unlock();
  17. }
  18. }

Code example of Async interface usage:

  1. RLock lock1 = redisson1.getLock("lock1");
  2. RLock lock2 = redisson2.getLock("lock2");
  3. RLock lock3 = redisson3.getLock("lock3");
  4. RLock multiLock = anyRedisson.getMultiLock(lock1, lock2, lock3);
  5. RFuture<Void> lockFuture = multiLock.lockAsync();
  6. // or acquire lock and automatically unlock it after 10 seconds
  7. RFuture<Void> lockFuture = multiLock.lockAsync(10, TimeUnit.SECONDS);
  8. // or wait for lock aquisition up to 100 seconds
  9. // and automatically unlock it after 10 seconds
  10. RFuture<Boolean> lockFuture = multiLock.tryLockAsync(100, 10, TimeUnit.SECONDS);
  11. lockFuture.whenComplete((res, exception) -> {
  12. // ...
  13. multiLock.unlockAsync();
  14. });

Code example of Reactive interface usage:

  1. RedissonReactiveClient anyRedisson = redissonClient.reactive();
  2. RLockReactive lock1 = redisson1.getLock("lock1");
  3. RLockReactive lock2 = redisson2.getLock("lock2");
  4. RLockReactive lock3 = redisson3.getLock("lock3");
  5. RLockReactive multiLock = anyRedisson.getMultiLock(lock1, lock2, lock3);
  6. Mono<Void> lockMono = multiLock.lock();
  7. // or acquire lock and automatically unlock it after 10 seconds
  8. Mono<Void> lockMono = multiLock.lock(10, TimeUnit.SECONDS);
  9. // or wait for lock aquisition up to 100 seconds
  10. // and automatically unlock it after 10 seconds
  11. Mono<Boolean> lockMono = multiLock.tryLock(100, 10, TimeUnit.SECONDS);
  12. lockMono.doOnNext(res -> {
  13. // ...
  14. })
  15. .doFinally(multiLock.unlock())
  16. .subscribe();

Code example of RxJava3 interface usage:

  1. RedissonRxClient anyRedisson = redissonClient.rxJava();
  2. RLockRx lock1 = redisson1.getLock("lock1");
  3. RLockRx lock2 = redisson2.getLock("lock2");
  4. RLockRx lock3 = redisson3.getLock("lock3");
  5. RLockRx multiLock = anyRedisson.getMultiLock(lock1, lock2, lock3);
  6. Completable lockRes = multiLock.lock();
  7. // or acquire lock and automatically unlock it after 10 seconds
  8. Completable lockRes = multiLock.lock(10, TimeUnit.SECONDS);
  9. // or wait for lock aquisition up to 100 seconds
  10. // and automatically unlock it after 10 seconds
  11. Single<Boolean> lockRes = multiLock.tryLock(100, 10, TimeUnit.SECONDS);
  12. lockRes.doOnSuccess(res -> {
  13. // ...
  14. })
  15. .doFinally(multiLock.unlock())
  16. .subscribe();

8.4. RedLock

This object is deprecated. Use RLock or RFencedLock instead.

8.5. ReadWriteLock

Redis based distributed reentrant ReadWriteLock object for Java implements ReadWriteLock interface. Both Read and Write locks implement RLock interface.

Multiple ReadLock owners and only one WriteLock owner are allowed.

If Redisson instance which acquired lock crashes then such lock could hang forever in acquired state. To avoid this Redisson maintains lock watchdog, it prolongs lock expiration while lock holder Redisson instance is alive. By default lock watchdog timeout is 30 seconds and can be changed through Config.lockWatchdogTimeout setting.

Also Redisson allow to specify leaseTime parameter during lock acquisition. After specified time interval locked lock will be released automatically.

RLock object behaves according to the Java Lock specification. It means only lock owner thread can unlock it otherwise IllegalMonitorStateException would be thrown. Otherwise consider to use RSemaphore object.

Code example:

  1. RReadWriteLock rwlock = redisson.getReadWriteLock("myLock");
  2. RLock lock = rwlock.readLock();
  3. // or
  4. RLock lock = rwlock.writeLock();
  5. // traditional lock method
  6. lock.lock();
  7. // or acquire lock and automatically unlock it after 10 seconds
  8. lock.lock(10, TimeUnit.SECONDS);
  9. // or wait for lock aquisition up to 100 seconds
  10. // and automatically unlock it after 10 seconds
  11. boolean res = lock.tryLock(100, 10, TimeUnit.SECONDS);
  12. if (res) {
  13. try {
  14. ...
  15. } finally {
  16. lock.unlock();
  17. }
  18. }

Code example of Async interface usage:

  1. RReadWriteLock rwlock = redisson.getReadWriteLock("myLock");
  2. RLock lock = rwlock.readLock();
  3. // or
  4. RLock lock = rwlock.writeLock();
  5. RFuture<Void> lockFuture = lock.lockAsync();
  6. // or acquire lock and automatically unlock it after 10 seconds
  7. RFuture<Void> lockFuture = lock.lockAsync(10, TimeUnit.SECONDS);
  8. // or wait for lock aquisition up to 100 seconds
  9. // and automatically unlock it after 10 seconds
  10. RFuture<Boolean> lockFuture = lock.tryLockAsync(100, 10, TimeUnit.SECONDS);
  11. lockFuture.whenComplete((res, exception) -> {
  12. // ...
  13. lock.unlockAsync();
  14. });

Code example of Reactive interface usage:

  1. RedissonReactiveClient redisson = redissonClient.reactive();
  2. RReadWriteLockReactive rwlock = redisson.getReadWriteLock("myLock");
  3. RLockReactive lock = rwlock.readLock();
  4. // or
  5. RLockReactive lock = rwlock.writeLock();
  6. Mono<Void> lockMono = lock.lock();
  7. // or acquire lock and automatically unlock it after 10 seconds
  8. Mono<Void> lockMono = lock.lock(10, TimeUnit.SECONDS);
  9. // or wait for lock aquisition up to 100 seconds
  10. // and automatically unlock it after 10 seconds
  11. Mono<Boolean> lockMono = lock.tryLock(100, 10, TimeUnit.SECONDS);
  12. lockMono.doOnNext(res -> {
  13. // ...
  14. })
  15. .doFinally(lock.unlock())
  16. .subscribe();

Code example of RxJava3 interface usage:

  1. RedissonRxClient redisson = redissonClient.rxJava();
  2. RReadWriteLockRx rwlock = redisson.getReadWriteLock("myLock");
  3. RLockRx lock = rwlock.readLock();
  4. // or
  5. RLockRx lock = rwlock.writeLock();
  6. Completable lockRes = lock.lock();
  7. // or acquire lock and automatically unlock it after 10 seconds
  8. Completable lockRes = lock.lock(10, TimeUnit.SECONDS);
  9. // or wait for lock aquisition up to 100 seconds
  10. // and automatically unlock it after 10 seconds
  11. Single<Boolean> lockRes = lock.tryLock(100, 10, TimeUnit.SECONDS);
  12. lockRes.doOnSuccess(res -> {
  13. // ...
  14. })
  15. .doFinally(lock.unlock())
  16. .subscribe();

8.6. Semaphore

Redis based distributed Semaphore object for Java similar to Semaphore object.

Could be initialized before usage, but it’s not requirement, with available permits amount through trySetPermits(permits) method.

Code example:

  1. RSemaphore semaphore = redisson.getSemaphore("mySemaphore");
  2. // acquire single permit
  3. semaphore.acquire();
  4. // or acquire 10 permits
  5. semaphore.acquire(10);
  6. // or try to acquire permit
  7. boolean res = semaphore.tryAcquire();
  8. // or try to acquire permit or wait up to 15 seconds
  9. boolean res = semaphore.tryAcquire(15, TimeUnit.SECONDS);
  10. // or try to acquire 10 permit
  11. boolean res = semaphore.tryAcquire(10);
  12. // or try to acquire 10 permits or wait up to 15 seconds
  13. boolean res = semaphore.tryAcquire(10, 15, TimeUnit.SECONDS);
  14. if (res) {
  15. try {
  16. ...
  17. } finally {
  18. semaphore.release();
  19. }
  20. }

Code example of Async interface usage:

  1. RSemaphore semaphore = redisson.getSemaphore("mySemaphore");
  2. // acquire single permit
  3. RFuture<Void> acquireFuture = semaphore.acquireAsync();
  4. // or acquire 10 permits
  5. RFuture<Void> acquireFuture = semaphore.acquireAsync(10);
  6. // or try to acquire permit
  7. RFuture<Boolean> acquireFuture = semaphore.tryAcquireAsync();
  8. // or try to acquire permit or wait up to 15 seconds
  9. RFuture<Boolean> acquireFuture = semaphore.tryAcquireAsync(15, TimeUnit.SECONDS);
  10. // or try to acquire 10 permit
  11. RFuture<Boolean> acquireFuture = semaphore.tryAcquireAsync(10);
  12. // or try to acquire 10 permits or wait up to 15 seconds
  13. RFuture<Boolean> acquireFuture = semaphore.tryAcquireAsync(10, 15, TimeUnit.SECONDS);
  14. acquireFuture.whenComplete((res, exception) -> {
  15. // ...
  16. semaphore.releaseAsync();
  17. });

Code example of Reactive interface usage:

  1. RedissonReactiveClient redisson = redissonClient.reactive();
  2. RSemaphoreReactive semaphore = redisson.getSemaphore("mySemaphore");
  3. // acquire single permit
  4. Mono<Void> acquireMono = semaphore.acquire();
  5. // or acquire 10 permits
  6. Mono<Void> acquireMono = semaphore.acquire(10);
  7. // or try to acquire permit
  8. Mono<Boolean> acquireMono = semaphore.tryAcquire();
  9. // or try to acquire permit or wait up to 15 seconds
  10. Mono<Boolean> acquireMono = semaphore.tryAcquire(15, TimeUnit.SECONDS);
  11. // or try to acquire 10 permit
  12. Mono<Boolean> acquireMono = semaphore.tryAcquire(10);
  13. // or try to acquire 10 permits or wait up to 15 seconds
  14. Mono<Boolean> acquireMono = semaphore.tryAcquire(10, 15, TimeUnit.SECONDS);
  15. acquireMono.doOnNext(res -> {
  16. // ...
  17. })
  18. .doFinally(semaphore.release())
  19. .subscribe();

Code example of RxJava3 interface usage:

  1. RedissonRxClient redisson = redissonClient.rxJava();
  2. RSemaphoreRx semaphore = redisson.getSemaphore("mySemaphore");
  3. // acquire single permit
  4. Completable acquireRx = semaphore.acquire();
  5. // or acquire 10 permits
  6. Completable acquireRx = semaphore.acquire(10);
  7. // or try to acquire permit
  8. Single<Boolean> acquireRx = semaphore.tryAcquire();
  9. // or try to acquire permit or wait up to 15 seconds
  10. Single<Boolean> acquireRx = semaphore.tryAcquire(15, TimeUnit.SECONDS);
  11. // or try to acquire 10 permit
  12. Single<Boolean> acquireRx = semaphore.tryAcquire(10);
  13. // or try to acquire 10 permits or wait up to 15 seconds
  14. Single<Boolean> acquireRx = semaphore.tryAcquire(10, 15, TimeUnit.SECONDS);
  15. acquireRx.doOnSuccess(res -> {
  16. // ...
  17. })
  18. .doFinally(semaphore.release())
  19. .subscribe();

8.7. PermitExpirableSemaphore

Redis based distributed Semaphore object for Java with lease time parameter support for each acquired permit. Each permit identified by own id and could be released only using its id.

Should be initialized before usage with available permits amount through trySetPermits(permits) method. Allows to increase/decrease number of available permits through addPermits(permits) method.

Code example:

  1. RPermitExpirableSemaphore semaphore = redisson.getPermitExpirableSemaphore("mySemaphore");
  2. semaphore.trySetPermits(23);
  3. // acquire permit
  4. String id = semaphore.acquire();
  5. // or acquire permit with lease time in 10 seconds
  6. String id = semaphore.acquire(10, TimeUnit.SECONDS);
  7. // or try to acquire permit
  8. String id = semaphore.tryAcquire();
  9. // or try to acquire permit or wait up to 15 seconds
  10. String id = semaphore.tryAcquire(15, TimeUnit.SECONDS);
  11. // or try to acquire permit with least time 15 seconds or wait up to 10 seconds
  12. String id = semaphore.tryAcquire(10, 15, TimeUnit.SECONDS);
  13. if (id != null) {
  14. try {
  15. ...
  16. } finally {
  17. semaphore.release(id);
  18. }
  19. }

Code example of Async interface usage:

  1. RPermitExpirableSemaphore semaphore = redisson.getPermitExpirableSemaphore("mySemaphore");
  2. RFuture<Boolean> setFuture = semaphore.trySetPermitsAsync(23);
  3. // acquire permit
  4. RFuture<String> acquireFuture = semaphore.acquireAsync();
  5. // or acquire permit with lease time in 10 seconds
  6. RFuture<String> acquireFuture = semaphore.acquireAsync(10, TimeUnit.SECONDS);
  7. // or try to acquire permit
  8. RFuture<String> acquireFuture = semaphore.tryAcquireAsync();
  9. // or try to acquire permit or wait up to 15 seconds
  10. RFuture<String> acquireFuture = semaphore.tryAcquireAsync(15, TimeUnit.SECONDS);
  11. // or try to acquire permit with least time 15 seconds or wait up to 10 seconds
  12. RFuture<String> acquireFuture = semaphore.tryAcquireAsync(10, 15, TimeUnit.SECONDS);
  13. acquireFuture.whenComplete((id, exception) -> {
  14. // ...
  15. semaphore.releaseAsync(id);
  16. });

Code example of Reactive interface usage:

  1. RedissonReactiveClient redisson = redissonClient.reactive();
  2. RPermitExpirableSemaphoreReactive semaphore = redisson.getPermitExpirableSemaphore("mySemaphore");
  3. Mono<Boolean> setMono = semaphore.trySetPermits(23);
  4. // acquire permit
  5. Mono<String> acquireMono = semaphore.acquire();
  6. // or acquire permit with lease time in 10 seconds
  7. Mono<String> acquireMono = semaphore.acquire(10, TimeUnit.SECONDS);
  8. // or try to acquire permit
  9. Mono<String> acquireMono = semaphore.tryAcquire();
  10. // or try to acquire permit or wait up to 15 seconds
  11. Mono<String> acquireMono = semaphore.tryAcquire(15, TimeUnit.SECONDS);
  12. // or try to acquire permit with least time 15 seconds or wait up to 10 seconds
  13. Mono<String> acquireMono = semaphore.tryAcquire(10, 15, TimeUnit.SECONDS);
  14. acquireMono.flatMap(id -> {
  15. // ...
  16. return semaphore.release(id);
  17. }).subscribe();

Code example of RxJava3 interface usage:

  1. RedissonRxClient redisson = redissonClient.rxJava();
  2. RPermitExpirableSemaphoreRx semaphore = redisson.getPermitExpirableSemaphore("mySemaphore");
  3. Single<Boolean> setRx = semaphore.trySetPermits(23);
  4. // acquire permit
  5. Single<String> acquireRx = semaphore.acquire();
  6. // or acquire permit with lease time in 10 seconds
  7. Single<String> acquireRx = semaphore.acquire(10, TimeUnit.SECONDS);
  8. // or try to acquire permit
  9. Maybe<String> acquireRx = semaphore.tryAcquire();
  10. // or try to acquire permit or wait up to 15 seconds
  11. Maybe<String> acquireRx = semaphore.tryAcquire(15, TimeUnit.SECONDS);
  12. // or try to acquire permit with least time 15 seconds or wait up to 10 seconds
  13. Maybe<String> acquireRx = semaphore.tryAcquire(10, 15, TimeUnit.SECONDS);
  14. acquireRx.flatMap(id -> {
  15. // ...
  16. return semaphore.release(id);
  17. }).subscribe();

8.8. CountDownLatch

Redis based distributed CountDownLatch object for Java has structure similar to CountDownLatch object.

Should be initialized with count by trySetCount(count) method before usage.

Code example:

  1. RCountDownLatch latch = redisson.getCountDownLatch("myCountDownLatch");
  2. latch.trySetCount(1);
  3. // await for count down
  4. latch.await();
  5. // in other thread or JVM
  6. RCountDownLatch latch = redisson.getCountDownLatch("myCountDownLatch");
  7. latch.countDown();

Code example of Async interface usage:

  1. RCountDownLatch latch = redisson.getCountDownLatch("myCountDownLatch");
  2. RFuture<Boolean> setFuture = lock.trySetCountAsync(1);
  3. // await for count down
  4. RFuture<Void> awaitFuture = latch.awaitAsync();
  5. // in other thread or JVM
  6. RCountDownLatch latch = redisson.getCountDownLatch("myCountDownLatch");
  7. RFuture<Void> countFuture = latch.countDownAsync();

Code example of Reactive interface usage:

  1. RedissonReactiveClient redisson = redissonClient.reactive();
  2. RCountDownLatchReactive latch = redisson.getCountDownLatch("myCountDownLatch");
  3. Mono<Boolean> setMono = latch.trySetCount(1);
  4. // await for count down
  5. Mono<Void> awaitMono = latch.await();
  6. // in other thread or JVM
  7. RCountDownLatchReactive latch = redisson.getCountDownLatch("myCountDownLatch");
  8. Mono<Void> countMono = latch.countDown();

Code example of RxJava3 interface usage:

  1. RedissonRxClient redisson = redissonClient.rxJava();
  2. RCountDownLatchRx latch = redisson.getCountDownLatch("myCountDownLatch");
  3. Single<Boolean> setRx = latch.trySetCount(1);
  4. // await for count down
  5. Completable awaitRx = latch.await();
  6. // in other thread or JVM
  7. RCountDownLatchRx latch = redisson.getCountDownLatch("myCountDownLatch");
  8. Completable countRx = latch.countDown();

8.9. Spin Lock

Redis based distributed reentrant SpinLock object for Java and implements Lock interface.

Thousands or more locks acquired/released per short time interval may cause reaching of network throughput limit and Redis CPU overload because of pubsub usage in Lock object. This occurs due to nature of Redis pubsub - messages are distributed to all nodes in Redis cluster. Spin Lock uses Exponential Backoff strategy by default for lock acquisition instead of pubsub channel.

If Redisson instance which acquired lock crashes then such lock could hang forever in acquired state. To avoid this Redisson maintains lock watchdog, it prolongs lock expiration while lock holder Redisson instance is alive. By default lock watchdog timeout is 30 seconds and can be changed through Config.lockWatchdogTimeout setting.

leaseTime parameter during lock acquisition can be defined. After specified time interval locked lock will be released automatically.

RLock object behaves according to the Java Lock specification. It means only lock owner thread can unlock it otherwise IllegalMonitorStateException would be thrown. Otherwise consider to use RSemaphore object.

Code example:

  1. RLock lock = redisson.getSpinLock("myLock");
  2. // traditional lock method
  3. lock.lock();
  4. // or acquire lock and automatically unlock it after 10 seconds
  5. lock.lock(10, TimeUnit.SECONDS);
  6. // or wait for lock aquisition up to 100 seconds
  7. // and automatically unlock it after 10 seconds
  8. boolean res = lock.tryLock(100, 10, TimeUnit.SECONDS);
  9. if (res) {
  10. try {
  11. ...
  12. } finally {
  13. lock.unlock();
  14. }
  15. }

Code example of Async interface usage:

  1. RLock lock = redisson.getSpinLock("myLock");
  2. RFuture<Void> lockFuture = lock.lockAsync();
  3. // or acquire lock and automatically unlock it after 10 seconds
  4. RFuture<Void> lockFuture = lock.lockAsync(10, TimeUnit.SECONDS);
  5. // or wait for lock aquisition up to 100 seconds
  6. // and automatically unlock it after 10 seconds
  7. RFuture<Boolean> lockFuture = lock.tryLockAsync(100, 10, TimeUnit.SECONDS);
  8. lockFuture.whenComplete((res, exception) -> {
  9. // ...
  10. lock.unlockAsync();
  11. });

Code example of Reactive interface usage:

  1. RedissonReactiveClient redisson = redissonClient.reactive();
  2. RLockReactive lock = redisson.getSpinLock("myLock");
  3. Mono<Void> lockMono = lock.lock();
  4. // or acquire lock and automatically unlock it after 10 seconds
  5. Mono<Void> lockMono = lock.lock(10, TimeUnit.SECONDS);
  6. // or wait for lock aquisition up to 100 seconds
  7. // and automatically unlock it after 10 seconds
  8. Mono<Boolean> lockMono = lock.tryLock(100, 10, TimeUnit.SECONDS);
  9. lockMono.doOnNext(res -> {
  10. // ...
  11. })
  12. .doFinally(lock.unlock())
  13. .subscribe();

Code example of RxJava3 interface usage:

  1. RedissonRxClient redisson = redissonClient.rxJava();
  2. RLockRx lock = redisson.getSpinLock("myLock");
  3. Completable lockRes = lock.lock();
  4. // or acquire lock and automatically unlock it after 10 seconds
  5. Completable lockRes = lock.lock(10, TimeUnit.SECONDS);
  6. // or wait for lock aquisition up to 100 seconds
  7. // and automatically unlock it after 10 seconds
  8. Single<Boolean> lockRes = lock.tryLock(100, 10, TimeUnit.SECONDS);
  9. lockRes.doOnSuccess(res -> {
  10. // ...
  11. })
  12. .doFinally(lock.unlock())
  13. .subscribe();

8.10. Fenced Lock

Redis based distributed reentrant FencedLock object for Java and implements Lock interface.

This type of lock maintains the fencing token to avoid cases when Client acquired the lock was delayed due to long GC pause or other reason and can’t detect that it doesn’t own the lock anymore. To resolve this issue token is returned by locking methods or getToken() method. Token should be checked if it’s greater or equal with the previous one by the service guarded by this lock and reject operation if condition is false.

If Redisson instance which acquired lock crashes then such lock could hang forever in acquired state. To avoid this Redisson maintains lock watchdog, it prolongs lock expiration while lock holder Redisson instance is alive. By default lock watchdog timeout is 30 seconds and can be changed through Config.lockWatchdogTimeout setting.

leaseTime parameter during lock acquisition can be defined. After specified time interval locked lock will be released automatically.

RLock object behaves according to the Java Lock specification. It means only lock owner thread can unlock it otherwise IllegalMonitorStateException would be thrown. Otherwise consider to use RSemaphore object.

Code example:

  1. RFencedLock lock = redisson.getFencedLock("myLock");
  2. // traditional lock method
  3. Long token = lock.lockAndGetToken();
  4. // or acquire lock and automatically unlock it after 10 seconds
  5. token = lock.lockAndGetToken(10, TimeUnit.SECONDS);
  6. // or wait for lock aquisition up to 100 seconds
  7. // and automatically unlock it after 10 seconds
  8. Long token = lock.tryLockAndGetToken(100, 10, TimeUnit.SECONDS);
  9. if (token != null) {
  10. try {
  11. // check if token >= old token
  12. ...
  13. } finally {
  14. lock.unlock();
  15. }
  16. }

Code example of Async interface usage:

  1. RFencedLock lock = redisson.getFencedLock("myLock");
  2. RFuture<Long> lockFuture = lock.lockAndGetTokenAsync();
  3. // or acquire lock and automatically unlock it after 10 seconds
  4. RFuture<Long> lockFuture = lock.lockAndGetTokenAsync(10, TimeUnit.SECONDS);
  5. // or wait for lock aquisition up to 100 seconds
  6. // and automatically unlock it after 10 seconds
  7. RFuture<Long> lockFuture = lock.tryLockAndGetTokenAsync(100, 10, TimeUnit.SECONDS);
  8. long threadId = Thread.currentThread().getId();
  9. lockFuture.whenComplete((token, exception) -> {
  10. if (token != null) {
  11. try {
  12. // check if token >= old token
  13. ...
  14. } finally {
  15. lock.unlockAsync(threadId);
  16. }
  17. }
  18. });

Code example of Reactive interface usage:

  1. RedissonReactiveClient redisson = redissonClient.reactive();
  2. RFencedLockReactive lock = redisson.getFencedLock("myLock");
  3. Mono<Long> lockMono = lock.lockAndGetToken();
  4. // or acquire lock and automatically unlock it after 10 seconds
  5. Mono<Long> lockMono = lock.lockAndGetToken(10, TimeUnit.SECONDS);
  6. // or wait for lock aquisition up to 100 seconds
  7. // and automatically unlock it after 10 seconds
  8. Mono<Long> lockMono = lock.tryLockAndGetToken(100, 10, TimeUnit.SECONDS);
  9. long threadId = Thread.currentThread().getId();
  10. lockMono.doOnNext(token -> {
  11. if (token != null) {
  12. try {
  13. // check if token >= old token
  14. ...
  15. } finally {
  16. lock.unlock(threadId);
  17. }
  18. }
  19. })
  20. .subscribe();

Code example of RxJava3 interface usage:

  1. RedissonRxClient redisson = redissonClient.rxJava();
  2. RFencedLockRx lock = redisson.getFencedLock("myLock");
  3. Single<Long> lockRes = lock.lockAndGetToken();
  4. // or acquire lock and automatically unlock it after 10 seconds
  5. Single<Long> lockRes = lock.lockAndGetToken(10, TimeUnit.SECONDS);
  6. // or wait for lock aquisition up to 100 seconds
  7. // and automatically unlock it after 10 seconds
  8. Single<Long> lockRes = lock.tryLock(100, 10, TimeUnit.SECONDS);
  9. long threadId = Thread.currentThread().getId();
  10. lockRes.doOnSuccess(token -> {
  11. if (token != null) {
  12. try {
  13. // check if token >= old token
  14. ...
  15. } finally {
  16. lock.unlock(threadId);
  17. }
  18. }
  19. })
  20. .subscribe();