use of org.redisson.RedissonMultiLock in project tutorials by eugenp.
the class RedissonIntegrationTest method givenMultipleLocksSet_thenEnsureAllCanUnlock.
@Test
public void givenMultipleLocksSet_thenEnsureAllCanUnlock() {
RedissonClient clientInstance1 = Redisson.create();
RedissonClient clientInstance2 = Redisson.create();
RedissonClient clientInstance3 = Redisson.create();
RLock lock1 = clientInstance1.getLock("lock1");
RLock lock2 = clientInstance2.getLock("lock2");
RLock lock3 = clientInstance3.getLock("lock3");
RedissonMultiLock lock = new RedissonMultiLock(lock1, lock2, lock3);
lock.lock();
assertTrue(lock1.isLocked() && lock2.isLocked() && lock3.isLocked());
lock.unlock();
assertTrue(!(lock1.isLocked() || lock2.isLocked() || lock3.isLocked()));
}
use of org.redisson.RedissonMultiLock in project redisson by redisson.
the class BaseTransactionalSet method moveAsync.
public RFuture<Boolean> moveAsync(String destination, V value) {
RSet<V> destinationSet = new RedissonSet<V>(object.getCodec(), commandExecutor, destination, null);
RPromise<Boolean> result = new RedissonPromise<Boolean>();
RLock destinationLock = getLock(destinationSet, value);
RLock lock = getLock(set, value);
RedissonMultiLock multiLock = new RedissonMultiLock(destinationLock, lock);
long threadId = Thread.currentThread().getId();
multiLock.lockAsync(timeout, TimeUnit.MILLISECONDS).onComplete((res, e) -> {
if (e != null) {
multiLock.unlockAsync(threadId);
result.tryFailure(e);
return;
}
HashValue keyHash = toHash(value);
Object currentValue = state.get(keyHash);
if (currentValue != null) {
operations.add(createMoveOperation(destination, value, threadId));
if (currentValue == NULL) {
result.trySuccess(false);
} else {
state.put(keyHash, NULL);
result.trySuccess(true);
}
return;
}
set.containsAsync(value).onComplete((r, ex) -> {
if (ex != null) {
result.tryFailure(ex);
return;
}
operations.add(createMoveOperation(destination, value, threadId));
if (r) {
state.put(keyHash, NULL);
}
result.trySuccess(r);
});
});
return result;
}
use of org.redisson.RedissonMultiLock in project redisson by redisson.
the class BaseTransactionalMap method executeLocked.
protected <R> void executeLocked(RPromise<R> promise, Runnable runnable, Collection<K> keys) {
List<RLock> locks = new ArrayList<>(keys.size());
for (K key : keys) {
RLock lock = getLock(key);
locks.add(lock);
}
RedissonMultiLock multiLock = new RedissonMultiLock(locks.toArray(new RLock[locks.size()]));
long threadId = Thread.currentThread().getId();
multiLock.lockAsync(timeout, TimeUnit.MILLISECONDS).onComplete((res, e) -> {
if (e == null) {
runnable.run();
} else {
multiLock.unlockAsync(threadId);
promise.tryFailure(e);
}
});
}
use of org.redisson.RedissonMultiLock in project redisson by redisson.
the class RedissonTransactionalBuckets method executeLocked.
protected <R> void executeLocked(RPromise<R> promise, Runnable runnable, Collection<String> keys) {
List<RLock> locks = new ArrayList<>(keys.size());
for (String key : keys) {
RLock lock = getLock(key);
locks.add(lock);
}
RedissonMultiLock multiLock = new RedissonMultiLock(locks.toArray(new RLock[locks.size()]));
long threadId = Thread.currentThread().getId();
multiLock.lockAsync(timeout, TimeUnit.MILLISECONDS).onComplete((res, e) -> {
if (e == null) {
runnable.run();
} else {
multiLock.unlockAsync(threadId);
promise.tryFailure(e);
}
});
}
use of org.redisson.RedissonMultiLock in project redisson by redisson.
the class BaseTransactionalSet method executeLocked.
protected <R> void executeLocked(RPromise<R> promise, Runnable runnable, Collection<?> values) {
List<RLock> locks = new ArrayList<RLock>(values.size());
for (Object value : values) {
RLock lock = getLock(set, (V) value);
locks.add(lock);
}
RedissonMultiLock multiLock = new RedissonMultiLock(locks.toArray(new RLock[locks.size()]));
long threadId = Thread.currentThread().getId();
multiLock.lockAsync(timeout, TimeUnit.MILLISECONDS).onComplete((res, e) -> {
if (e == null) {
runnable.run();
} else {
multiLock.unlockAsync(threadId);
promise.tryFailure(e);
}
});
}
Aggregations