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:
RLock lock = redisson.getLock("myLock");// traditional lock methodlock.lock();// or acquire lock and automatically unlock it after 10 secondslock.lock(10, TimeUnit.SECONDS);// or wait for lock aquisition up to 100 seconds// and automatically unlock it after 10 secondsboolean res = lock.tryLock(100, 10, TimeUnit.SECONDS);if (res) {try {...} finally {lock.unlock();}}
Code example of Async interface usage:
RLock lock = redisson.getLock("myLock");RFuture<Void> lockFuture = lock.lockAsync();// or acquire lock and automatically unlock it after 10 secondsRFuture<Void> lockFuture = lock.lockAsync(10, TimeUnit.SECONDS);// or wait for lock aquisition up to 100 seconds// and automatically unlock it after 10 secondsRFuture<Boolean> lockFuture = lock.tryLockAsync(100, 10, TimeUnit.SECONDS);lockFuture.whenComplete((res, exception) -> {// ...lock.unlockAsync();});
Code example of Reactive interface usage:
RedissonReactiveClient redisson = redissonClient.reactive();RLockReactive lock = redisson.getLock("myLock");Mono<Void> lockMono = lock.lock();// or acquire lock and automatically unlock it after 10 secondsMono<Void> lockMono = lock.lock(10, TimeUnit.SECONDS);// or wait for lock aquisition up to 100 seconds// and automatically unlock it after 10 secondsMono<Boolean> lockMono = lock.tryLock(100, 10, TimeUnit.SECONDS);lockMono.doOnNext(res -> {// ...}).doFinally(lock.unlock()).subscribe();
Code example of RxJava3 interface usage:
RedissonRxClient redisson = redissonClient.rxJava();RLockRx lock = redisson.getLock("myLock");Completable lockRes = lock.lock();// or acquire lock and automatically unlock it after 10 secondsCompletable lockRes = lock.lock(10, TimeUnit.SECONDS);// or wait for lock aquisition up to 100 seconds// and automatically unlock it after 10 secondsSingle<Boolean> lockRes = lock.tryLock(100, 10, TimeUnit.SECONDS);lockRes.doOnSuccess(res -> {// ...}).doFinally(lock.unlock()).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:
RLock lock = redisson.getFairLock("myLock");// traditional lock methodlock.lock();// or acquire lock and automatically unlock it after 10 secondslock.lock(10, TimeUnit.SECONDS);// or wait for lock aquisition up to 100 seconds// and automatically unlock it after 10 secondsboolean res = lock.tryLock(100, 10, TimeUnit.SECONDS);if (res) {try {...} finally {lock.unlock();}}
Code example of Async interface usage:
RLock lock = redisson.getFairLock("myLock");RFuture<Void> lockFuture = lock.lockAsync();// or acquire lock and automatically unlock it after 10 secondsRFuture<Void> lockFuture = lock.lockAsync(10, TimeUnit.SECONDS);// or wait for lock aquisition up to 100 seconds// and automatically unlock it after 10 secondsRFuture<Boolean> lockFuture = lock.tryLockAsync(100, 10, TimeUnit.SECONDS);lockFuture.whenComplete((res, exception) -> {// ...lock.unlockAsync();});
Code example of Reactive interface usage:
RedissonReactiveClient redisson = redissonClient.reactive();RLockReactive lock = redisson.getFairLock("myLock");Mono<Void> lockMono = lock.lock();// or acquire lock and automatically unlock it after 10 secondsMono<Void> lockMono = lock.lock(10, TimeUnit.SECONDS);// or wait for lock aquisition up to 100 seconds// and automatically unlock it after 10 secondsMono<Boolean> lockMono = lock.tryLock(100, 10, TimeUnit.SECONDS);lockMono.doOnNext(res -> {// ...}).doFinally(lock.unlock()).subscribe();
Code example of RxJava3 interface usage:
RedissonRxClient redisson = redissonClient.rxJava();RLockRx lock = redisson.getFairLock("myLock");Completable lockRes = lock.lock();// or acquire lock and automatically unlock it after 10 secondsCompletable lockRes = lock.lock(10, TimeUnit.SECONDS);// or wait for lock aquisition up to 100 seconds// and automatically unlock it after 10 secondsSingle<Boolean> lockRes = lock.tryLock(100, 10, TimeUnit.SECONDS);lockRes.doOnSuccess(res -> {// ...}).doFinally(lock.unlock()).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:
RLock lock1 = redisson1.getLock("lock1");RLock lock2 = redisson2.getLock("lock2");RLock lock3 = redisson3.getLock("lock3");RLock multiLock = anyRedisson.getMultiLock(lock1, lock2, lock3);// traditional lock methodmultiLock.lock();// or acquire lock and automatically unlock it after 10 secondsmultiLock.lock(10, TimeUnit.SECONDS);// or wait for lock aquisition up to 100 seconds// and automatically unlock it after 10 secondsboolean res = multiLock.tryLock(100, 10, TimeUnit.SECONDS);if (res) {try {...} finally {multiLock.unlock();}}
Code example of Async interface usage:
RLock lock1 = redisson1.getLock("lock1");RLock lock2 = redisson2.getLock("lock2");RLock lock3 = redisson3.getLock("lock3");RLock multiLock = anyRedisson.getMultiLock(lock1, lock2, lock3);RFuture<Void> lockFuture = multiLock.lockAsync();// or acquire lock and automatically unlock it after 10 secondsRFuture<Void> lockFuture = multiLock.lockAsync(10, TimeUnit.SECONDS);// or wait for lock aquisition up to 100 seconds// and automatically unlock it after 10 secondsRFuture<Boolean> lockFuture = multiLock.tryLockAsync(100, 10, TimeUnit.SECONDS);lockFuture.whenComplete((res, exception) -> {// ...multiLock.unlockAsync();});
Code example of Reactive interface usage:
RedissonReactiveClient anyRedisson = redissonClient.reactive();RLockReactive lock1 = redisson1.getLock("lock1");RLockReactive lock2 = redisson2.getLock("lock2");RLockReactive lock3 = redisson3.getLock("lock3");RLockReactive multiLock = anyRedisson.getMultiLock(lock1, lock2, lock3);Mono<Void> lockMono = multiLock.lock();// or acquire lock and automatically unlock it after 10 secondsMono<Void> lockMono = multiLock.lock(10, TimeUnit.SECONDS);// or wait for lock aquisition up to 100 seconds// and automatically unlock it after 10 secondsMono<Boolean> lockMono = multiLock.tryLock(100, 10, TimeUnit.SECONDS);lockMono.doOnNext(res -> {// ...}).doFinally(multiLock.unlock()).subscribe();
Code example of RxJava3 interface usage:
RedissonRxClient anyRedisson = redissonClient.rxJava();RLockRx lock1 = redisson1.getLock("lock1");RLockRx lock2 = redisson2.getLock("lock2");RLockRx lock3 = redisson3.getLock("lock3");RLockRx multiLock = anyRedisson.getMultiLock(lock1, lock2, lock3);Completable lockRes = multiLock.lock();// or acquire lock and automatically unlock it after 10 secondsCompletable lockRes = multiLock.lock(10, TimeUnit.SECONDS);// or wait for lock aquisition up to 100 seconds// and automatically unlock it after 10 secondsSingle<Boolean> lockRes = multiLock.tryLock(100, 10, TimeUnit.SECONDS);lockRes.doOnSuccess(res -> {// ...}).doFinally(multiLock.unlock()).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:
RReadWriteLock rwlock = redisson.getReadWriteLock("myLock");RLock lock = rwlock.readLock();// orRLock lock = rwlock.writeLock();// traditional lock methodlock.lock();// or acquire lock and automatically unlock it after 10 secondslock.lock(10, TimeUnit.SECONDS);// or wait for lock aquisition up to 100 seconds// and automatically unlock it after 10 secondsboolean res = lock.tryLock(100, 10, TimeUnit.SECONDS);if (res) {try {...} finally {lock.unlock();}}
Code example of Async interface usage:
RReadWriteLock rwlock = redisson.getReadWriteLock("myLock");RLock lock = rwlock.readLock();// orRLock lock = rwlock.writeLock();RFuture<Void> lockFuture = lock.lockAsync();// or acquire lock and automatically unlock it after 10 secondsRFuture<Void> lockFuture = lock.lockAsync(10, TimeUnit.SECONDS);// or wait for lock aquisition up to 100 seconds// and automatically unlock it after 10 secondsRFuture<Boolean> lockFuture = lock.tryLockAsync(100, 10, TimeUnit.SECONDS);lockFuture.whenComplete((res, exception) -> {// ...lock.unlockAsync();});
Code example of Reactive interface usage:
RedissonReactiveClient redisson = redissonClient.reactive();RReadWriteLockReactive rwlock = redisson.getReadWriteLock("myLock");RLockReactive lock = rwlock.readLock();// orRLockReactive lock = rwlock.writeLock();Mono<Void> lockMono = lock.lock();// or acquire lock and automatically unlock it after 10 secondsMono<Void> lockMono = lock.lock(10, TimeUnit.SECONDS);// or wait for lock aquisition up to 100 seconds// and automatically unlock it after 10 secondsMono<Boolean> lockMono = lock.tryLock(100, 10, TimeUnit.SECONDS);lockMono.doOnNext(res -> {// ...}).doFinally(lock.unlock()).subscribe();
Code example of RxJava3 interface usage:
RedissonRxClient redisson = redissonClient.rxJava();RReadWriteLockRx rwlock = redisson.getReadWriteLock("myLock");RLockRx lock = rwlock.readLock();// orRLockRx lock = rwlock.writeLock();Completable lockRes = lock.lock();// or acquire lock and automatically unlock it after 10 secondsCompletable lockRes = lock.lock(10, TimeUnit.SECONDS);// or wait for lock aquisition up to 100 seconds// and automatically unlock it after 10 secondsSingle<Boolean> lockRes = lock.tryLock(100, 10, TimeUnit.SECONDS);lockRes.doOnSuccess(res -> {// ...}).doFinally(lock.unlock()).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:
RSemaphore semaphore = redisson.getSemaphore("mySemaphore");// acquire single permitsemaphore.acquire();// or acquire 10 permitssemaphore.acquire(10);// or try to acquire permitboolean res = semaphore.tryAcquire();// or try to acquire permit or wait up to 15 secondsboolean res = semaphore.tryAcquire(15, TimeUnit.SECONDS);// or try to acquire 10 permitboolean res = semaphore.tryAcquire(10);// or try to acquire 10 permits or wait up to 15 secondsboolean res = semaphore.tryAcquire(10, 15, TimeUnit.SECONDS);if (res) {try {...} finally {semaphore.release();}}
Code example of Async interface usage:
RSemaphore semaphore = redisson.getSemaphore("mySemaphore");// acquire single permitRFuture<Void> acquireFuture = semaphore.acquireAsync();// or acquire 10 permitsRFuture<Void> acquireFuture = semaphore.acquireAsync(10);// or try to acquire permitRFuture<Boolean> acquireFuture = semaphore.tryAcquireAsync();// or try to acquire permit or wait up to 15 secondsRFuture<Boolean> acquireFuture = semaphore.tryAcquireAsync(15, TimeUnit.SECONDS);// or try to acquire 10 permitRFuture<Boolean> acquireFuture = semaphore.tryAcquireAsync(10);// or try to acquire 10 permits or wait up to 15 secondsRFuture<Boolean> acquireFuture = semaphore.tryAcquireAsync(10, 15, TimeUnit.SECONDS);acquireFuture.whenComplete((res, exception) -> {// ...semaphore.releaseAsync();});
Code example of Reactive interface usage:
RedissonReactiveClient redisson = redissonClient.reactive();RSemaphoreReactive semaphore = redisson.getSemaphore("mySemaphore");// acquire single permitMono<Void> acquireMono = semaphore.acquire();// or acquire 10 permitsMono<Void> acquireMono = semaphore.acquire(10);// or try to acquire permitMono<Boolean> acquireMono = semaphore.tryAcquire();// or try to acquire permit or wait up to 15 secondsMono<Boolean> acquireMono = semaphore.tryAcquire(15, TimeUnit.SECONDS);// or try to acquire 10 permitMono<Boolean> acquireMono = semaphore.tryAcquire(10);// or try to acquire 10 permits or wait up to 15 secondsMono<Boolean> acquireMono = semaphore.tryAcquire(10, 15, TimeUnit.SECONDS);acquireMono.doOnNext(res -> {// ...}).doFinally(semaphore.release()).subscribe();
Code example of RxJava3 interface usage:
RedissonRxClient redisson = redissonClient.rxJava();RSemaphoreRx semaphore = redisson.getSemaphore("mySemaphore");// acquire single permitCompletable acquireRx = semaphore.acquire();// or acquire 10 permitsCompletable acquireRx = semaphore.acquire(10);// or try to acquire permitSingle<Boolean> acquireRx = semaphore.tryAcquire();// or try to acquire permit or wait up to 15 secondsSingle<Boolean> acquireRx = semaphore.tryAcquire(15, TimeUnit.SECONDS);// or try to acquire 10 permitSingle<Boolean> acquireRx = semaphore.tryAcquire(10);// or try to acquire 10 permits or wait up to 15 secondsSingle<Boolean> acquireRx = semaphore.tryAcquire(10, 15, TimeUnit.SECONDS);acquireRx.doOnSuccess(res -> {// ...}).doFinally(semaphore.release()).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:
RPermitExpirableSemaphore semaphore = redisson.getPermitExpirableSemaphore("mySemaphore");semaphore.trySetPermits(23);// acquire permitString id = semaphore.acquire();// or acquire permit with lease time in 10 secondsString id = semaphore.acquire(10, TimeUnit.SECONDS);// or try to acquire permitString id = semaphore.tryAcquire();// or try to acquire permit or wait up to 15 secondsString id = semaphore.tryAcquire(15, TimeUnit.SECONDS);// or try to acquire permit with least time 15 seconds or wait up to 10 secondsString id = semaphore.tryAcquire(10, 15, TimeUnit.SECONDS);if (id != null) {try {...} finally {semaphore.release(id);}}
Code example of Async interface usage:
RPermitExpirableSemaphore semaphore = redisson.getPermitExpirableSemaphore("mySemaphore");RFuture<Boolean> setFuture = semaphore.trySetPermitsAsync(23);// acquire permitRFuture<String> acquireFuture = semaphore.acquireAsync();// or acquire permit with lease time in 10 secondsRFuture<String> acquireFuture = semaphore.acquireAsync(10, TimeUnit.SECONDS);// or try to acquire permitRFuture<String> acquireFuture = semaphore.tryAcquireAsync();// or try to acquire permit or wait up to 15 secondsRFuture<String> acquireFuture = semaphore.tryAcquireAsync(15, TimeUnit.SECONDS);// or try to acquire permit with least time 15 seconds or wait up to 10 secondsRFuture<String> acquireFuture = semaphore.tryAcquireAsync(10, 15, TimeUnit.SECONDS);acquireFuture.whenComplete((id, exception) -> {// ...semaphore.releaseAsync(id);});
Code example of Reactive interface usage:
RedissonReactiveClient redisson = redissonClient.reactive();RPermitExpirableSemaphoreReactive semaphore = redisson.getPermitExpirableSemaphore("mySemaphore");Mono<Boolean> setMono = semaphore.trySetPermits(23);// acquire permitMono<String> acquireMono = semaphore.acquire();// or acquire permit with lease time in 10 secondsMono<String> acquireMono = semaphore.acquire(10, TimeUnit.SECONDS);// or try to acquire permitMono<String> acquireMono = semaphore.tryAcquire();// or try to acquire permit or wait up to 15 secondsMono<String> acquireMono = semaphore.tryAcquire(15, TimeUnit.SECONDS);// or try to acquire permit with least time 15 seconds or wait up to 10 secondsMono<String> acquireMono = semaphore.tryAcquire(10, 15, TimeUnit.SECONDS);acquireMono.flatMap(id -> {// ...return semaphore.release(id);}).subscribe();
Code example of RxJava3 interface usage:
RedissonRxClient redisson = redissonClient.rxJava();RPermitExpirableSemaphoreRx semaphore = redisson.getPermitExpirableSemaphore("mySemaphore");Single<Boolean> setRx = semaphore.trySetPermits(23);// acquire permitSingle<String> acquireRx = semaphore.acquire();// or acquire permit with lease time in 10 secondsSingle<String> acquireRx = semaphore.acquire(10, TimeUnit.SECONDS);// or try to acquire permitMaybe<String> acquireRx = semaphore.tryAcquire();// or try to acquire permit or wait up to 15 secondsMaybe<String> acquireRx = semaphore.tryAcquire(15, TimeUnit.SECONDS);// or try to acquire permit with least time 15 seconds or wait up to 10 secondsMaybe<String> acquireRx = semaphore.tryAcquire(10, 15, TimeUnit.SECONDS);acquireRx.flatMap(id -> {// ...return semaphore.release(id);}).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:
RCountDownLatch latch = redisson.getCountDownLatch("myCountDownLatch");latch.trySetCount(1);// await for count downlatch.await();// in other thread or JVMRCountDownLatch latch = redisson.getCountDownLatch("myCountDownLatch");latch.countDown();
Code example of Async interface usage:
RCountDownLatch latch = redisson.getCountDownLatch("myCountDownLatch");RFuture<Boolean> setFuture = lock.trySetCountAsync(1);// await for count downRFuture<Void> awaitFuture = latch.awaitAsync();// in other thread or JVMRCountDownLatch latch = redisson.getCountDownLatch("myCountDownLatch");RFuture<Void> countFuture = latch.countDownAsync();
Code example of Reactive interface usage:
RedissonReactiveClient redisson = redissonClient.reactive();RCountDownLatchReactive latch = redisson.getCountDownLatch("myCountDownLatch");Mono<Boolean> setMono = latch.trySetCount(1);// await for count downMono<Void> awaitMono = latch.await();// in other thread or JVMRCountDownLatchReactive latch = redisson.getCountDownLatch("myCountDownLatch");Mono<Void> countMono = latch.countDown();
Code example of RxJava3 interface usage:
RedissonRxClient redisson = redissonClient.rxJava();RCountDownLatchRx latch = redisson.getCountDownLatch("myCountDownLatch");Single<Boolean> setRx = latch.trySetCount(1);// await for count downCompletable awaitRx = latch.await();// in other thread or JVMRCountDownLatchRx latch = redisson.getCountDownLatch("myCountDownLatch");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:
RLock lock = redisson.getSpinLock("myLock");// traditional lock methodlock.lock();// or acquire lock and automatically unlock it after 10 secondslock.lock(10, TimeUnit.SECONDS);// or wait for lock aquisition up to 100 seconds// and automatically unlock it after 10 secondsboolean res = lock.tryLock(100, 10, TimeUnit.SECONDS);if (res) {try {...} finally {lock.unlock();}}
Code example of Async interface usage:
RLock lock = redisson.getSpinLock("myLock");RFuture<Void> lockFuture = lock.lockAsync();// or acquire lock and automatically unlock it after 10 secondsRFuture<Void> lockFuture = lock.lockAsync(10, TimeUnit.SECONDS);// or wait for lock aquisition up to 100 seconds// and automatically unlock it after 10 secondsRFuture<Boolean> lockFuture = lock.tryLockAsync(100, 10, TimeUnit.SECONDS);lockFuture.whenComplete((res, exception) -> {// ...lock.unlockAsync();});
Code example of Reactive interface usage:
RedissonReactiveClient redisson = redissonClient.reactive();RLockReactive lock = redisson.getSpinLock("myLock");Mono<Void> lockMono = lock.lock();// or acquire lock and automatically unlock it after 10 secondsMono<Void> lockMono = lock.lock(10, TimeUnit.SECONDS);// or wait for lock aquisition up to 100 seconds// and automatically unlock it after 10 secondsMono<Boolean> lockMono = lock.tryLock(100, 10, TimeUnit.SECONDS);lockMono.doOnNext(res -> {// ...}).doFinally(lock.unlock()).subscribe();
Code example of RxJava3 interface usage:
RedissonRxClient redisson = redissonClient.rxJava();RLockRx lock = redisson.getSpinLock("myLock");Completable lockRes = lock.lock();// or acquire lock and automatically unlock it after 10 secondsCompletable lockRes = lock.lock(10, TimeUnit.SECONDS);// or wait for lock aquisition up to 100 seconds// and automatically unlock it after 10 secondsSingle<Boolean> lockRes = lock.tryLock(100, 10, TimeUnit.SECONDS);lockRes.doOnSuccess(res -> {// ...}).doFinally(lock.unlock()).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:
RFencedLock lock = redisson.getFencedLock("myLock");// traditional lock methodLong token = lock.lockAndGetToken();// or acquire lock and automatically unlock it after 10 secondstoken = lock.lockAndGetToken(10, TimeUnit.SECONDS);// or wait for lock aquisition up to 100 seconds// and automatically unlock it after 10 secondsLong token = lock.tryLockAndGetToken(100, 10, TimeUnit.SECONDS);if (token != null) {try {// check if token >= old token...} finally {lock.unlock();}}
Code example of Async interface usage:
RFencedLock lock = redisson.getFencedLock("myLock");RFuture<Long> lockFuture = lock.lockAndGetTokenAsync();// or acquire lock and automatically unlock it after 10 secondsRFuture<Long> lockFuture = lock.lockAndGetTokenAsync(10, TimeUnit.SECONDS);// or wait for lock aquisition up to 100 seconds// and automatically unlock it after 10 secondsRFuture<Long> lockFuture = lock.tryLockAndGetTokenAsync(100, 10, TimeUnit.SECONDS);long threadId = Thread.currentThread().getId();lockFuture.whenComplete((token, exception) -> {if (token != null) {try {// check if token >= old token...} finally {lock.unlockAsync(threadId);}}});
Code example of Reactive interface usage:
RedissonReactiveClient redisson = redissonClient.reactive();RFencedLockReactive lock = redisson.getFencedLock("myLock");Mono<Long> lockMono = lock.lockAndGetToken();// or acquire lock and automatically unlock it after 10 secondsMono<Long> lockMono = lock.lockAndGetToken(10, TimeUnit.SECONDS);// or wait for lock aquisition up to 100 seconds// and automatically unlock it after 10 secondsMono<Long> lockMono = lock.tryLockAndGetToken(100, 10, TimeUnit.SECONDS);long threadId = Thread.currentThread().getId();lockMono.doOnNext(token -> {if (token != null) {try {// check if token >= old token...} finally {lock.unlock(threadId);}}}).subscribe();
Code example of RxJava3 interface usage:
RedissonRxClient redisson = redissonClient.rxJava();RFencedLockRx lock = redisson.getFencedLock("myLock");Single<Long> lockRes = lock.lockAndGetToken();// or acquire lock and automatically unlock it after 10 secondsSingle<Long> lockRes = lock.lockAndGetToken(10, TimeUnit.SECONDS);// or wait for lock aquisition up to 100 seconds// and automatically unlock it after 10 secondsSingle<Long> lockRes = lock.tryLock(100, 10, TimeUnit.SECONDS);long threadId = Thread.currentThread().getId();lockRes.doOnSuccess(token -> {if (token != null) {try {// check if token >= old token...} finally {lock.unlock(threadId);}}}).subscribe();
