Search in sources :

Example 1 with RedissonMultiLock

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()));
}
Also used : RedissonMultiLock(org.redisson.RedissonMultiLock) Test(org.junit.Test)

Example 2 with RedissonMultiLock

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;
}
Also used : RedissonSet(org.redisson.RedissonSet) RedissonPromise(org.redisson.misc.RedissonPromise) RedissonObject(org.redisson.RedissonObject) HashValue(org.redisson.misc.HashValue) RedissonMultiLock(org.redisson.RedissonMultiLock)

Example 3 with RedissonMultiLock

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);
        }
    });
}
Also used : ArrayList(java.util.ArrayList) RLock(org.redisson.api.RLock) RedissonMultiLock(org.redisson.RedissonMultiLock)

Example 4 with RedissonMultiLock

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);
        }
    });
}
Also used : RLock(org.redisson.api.RLock) RedissonMultiLock(org.redisson.RedissonMultiLock)

Example 5 with RedissonMultiLock

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);
        }
    });
}
Also used : RedissonObject(org.redisson.RedissonObject) RedissonMultiLock(org.redisson.RedissonMultiLock)

Aggregations

RedissonMultiLock (org.redisson.RedissonMultiLock)5 RedissonObject (org.redisson.RedissonObject)2 RLock (org.redisson.api.RLock)2 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1 RedissonSet (org.redisson.RedissonSet)1 HashValue (org.redisson.misc.HashValue)1 RedissonPromise (org.redisson.misc.RedissonPromise)1