You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
8.1. Lock
Redis based distributed reentrant
Lock
object for Java and implements
Lock
interface.
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:
RLocklock = 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 secondsbooleanres = lock.tryLock(100, 10, TimeUnit.SECONDS);
if (res) {
try {
} finally {
lock.unlock();
RLocklock = 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();
RedissonReactiveClientredisson = redissonClient.reactive();
RLockReactivelock = 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();
RedissonRxClientredisson = redissonClient.rxJava();
RLockRxlock = redisson.getLock("myLock");
CompletablelockRes = lock.lock();
// or acquire lock and automatically unlock it after 10 secondsCompletablelockRes = 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:
RLocklock = 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 secondsbooleanres = lock.tryLock(100, 10, TimeUnit.SECONDS);
if (res) {
try {
} finally {
lock.unlock();
RLocklock = 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();
RedissonRxClientredisson = redissonClient.rxJava();
RLockRxlock = redisson.getFairLock("myLock");
CompletablelockRes = lock.lock();
// or acquire lock and automatically unlock it after 10 secondsCompletablelockRes = 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:
RLocklock1 = redisson1.getLock("lock1");
RLocklock2 = redisson2.getLock("lock2");
RLocklock3 = redisson3.getLock("lock3");
RLockmultiLock = 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 secondsbooleanres = multiLock.tryLock(100, 10, TimeUnit.SECONDS);
if (res) {
try {
} finally {
multiLock.unlock();
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:
RReadWriteLockrwlock = redisson.getReadWriteLock("myLock");
RLocklock = rwlock.readLock();
// orRLocklock = 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 secondsbooleanres = lock.tryLock(100, 10, TimeUnit.SECONDS);
if (res) {
try {
} finally {
lock.unlock();
RedissonRxClientredisson = redissonClient.rxJava();
RReadWriteLockRxrwlock = redisson.getReadWriteLock("myLock");
RLockRxlock = rwlock.readLock();
// orRLockRxlock = rwlock.writeLock();
CompletablelockRes = lock.lock();
// or acquire lock and automatically unlock it after 10 secondsCompletablelockRes = 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:
RSemaphoresemaphore = redisson.getSemaphore("mySemaphore");
// acquire single permitsemaphore.acquire();
// or acquire 10 permitssemaphore.acquire(10);
// or try to acquire permitbooleanres = semaphore.tryAcquire();
// or try to acquire permit or wait up to 15 secondsbooleanres = semaphore.tryAcquire(15, TimeUnit.SECONDS);
// or try to acquire 10 permitbooleanres = semaphore.tryAcquire(10);
// or try to acquire 10 permits or wait up to 15 secondsbooleanres = semaphore.tryAcquire(10, 15, TimeUnit.SECONDS);
if (res) {
try {
} finally {
semaphore.release();
RSemaphoresemaphore = 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();
RedissonRxClientredisson = redissonClient.rxJava();
RSemaphoreRxsemaphore = redisson.getSemaphore("mySemaphore");
// acquire single permitCompletableacquireRx = semaphore.acquire();
// or acquire 10 permitsCompletableacquireRx = 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:
RPermitExpirableSemaphoresemaphore = redisson.getPermitExpirableSemaphore("mySemaphore");
semaphore.trySetPermits(23);
// acquire permitStringid = semaphore.acquire();
// or acquire permit with lease time in 10 secondsStringid = semaphore.acquire(10, TimeUnit.SECONDS);
// or try to acquire permitStringid = semaphore.tryAcquire();
// or try to acquire permit or wait up to 15 secondsStringid = semaphore.tryAcquire(15, TimeUnit.SECONDS);
// or try to acquire permit with least time 15 seconds or wait up to 10 secondsStringid = semaphore.tryAcquire(10, 15, TimeUnit.SECONDS);
if (id != null) {
try {
} finally {
semaphore.release(id);
RPermitExpirableSemaphoresemaphore = 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);
RedissonReactiveClientredisson = redissonClient.reactive
();
RPermitExpirableSemaphoreReactivesemaphore = 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 -> {
// ...returnsemaphore.release(id);
}).subscribe();
RedissonRxClientredisson = redissonClient.rxJava();
RPermitExpirableSemaphoreRxsemaphore = 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 -> {
// ...returnsemaphore.release(id);
}).subscribe();
Should be initialized with count by trySetCount(count) method before usage.
Code example:
RCountDownLatchlatch = redisson.getCountDownLatch("myCountDownLatch");
latch.trySetCount(1);
// await for count downlatch.await();
// in other thread or JVMRCountDownLatchlatch = redisson.getCountDownLatch("myCountDownLatch");
latch.countDown();
RedissonRxClientredisson = redissonClient.rxJava();
RCountDownLatchRxlatch = redisson.getCountDownLatch("myCountDownLatch");
Single<Boolean> setRx = latch.trySetCount(1);
// await for count downCompletableawaitRx = latch.await();
// in other thread or JVMRCountDownLatchRxlatch = redisson.getCountDownLatch("myCountDownLatch");
CompletablecountRx = 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:
RLocklock = 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 secondsbooleanres = lock.tryLock(100, 10, TimeUnit.SECONDS);
if (res) {
try {
} finally {
lock.unlock();
RLocklock = 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();
RedissonReactiveClientredisson = redissonClient.reactive();
RLockReactivelock = 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();
RedissonRxClientredisson = redissonClient.rxJava();
RLockRxlock = redisson.getSpinLock("myLock");
CompletablelockRes = lock.lock();
// or acquire lock and automatically unlock it after 10 secondsCompletablelockRes = 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:
RFencedLocklock = redisson.getFencedLock("myLock");
// traditional lock methodLongtoken = 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 secondsLongtoken = lock.tryLockAndGetToken(100, 10, TimeUnit.SECONDS);
if (token != null) {
try {
// check if token >= old token
} finally {
lock.unlock();
RFencedLocklock = 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);
longthreadId = Thread.currentThread().getId();
lockFuture.whenComplete((token, exception) -> {
if (token != null) {
try {
// check if token >= old token
} finally {
lock.unlockAsync(threadId);