org.redisson.RedissonMultiLock Java Examples
The following examples show how to use
org.redisson.RedissonMultiLock.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RedissLockDemo.java From spring-boot-seckill with GNU General Public License v2.0 | 6 votes |
/** * 联锁(MultiLock) * Redisson的RedissonMultiLock对象可以将多个RLock对象关联为一个联锁,每个RLock对象实例可以来自于不同的Redisson实例 * @param redisson1 * @param redisson2 * @param redisson3 */ public void testMultiLock(RedissonClient redisson1,RedissonClient redisson2, RedissonClient redisson3){ RLock lock1 = redisson1.getLock("lock1"); RLock lock2 = redisson2.getLock("lock2"); RLock lock3 = redisson3.getLock("lock3"); RedissonMultiLock lock = new RedissonMultiLock(lock1, lock2, lock3); try { // 同时加锁:lock1 lock2 lock3, 所有的锁都上锁成功才算成功。 lock.lock(); // 尝试加锁,最多等待100秒,上锁以后10秒自动解锁 boolean res = lock.tryLock(100, 10, TimeUnit.SECONDS); if (res) { // do your business } } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } }
Example #2
Source File: BaseTransactionalSet.java From redisson with Apache License 2.0 | 6 votes |
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); } }); }
Example #3
Source File: RedissonTransactionalBuckets.java From redisson with Apache License 2.0 | 6 votes |
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); } }); }
Example #4
Source File: BaseTransactionalMap.java From redisson with Apache License 2.0 | 6 votes |
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); } }); }
Example #5
Source File: RedissonIntegrationTest.java From tutorials with MIT License | 6 votes |
@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())); }
Example #6
Source File: BaseTransactionalSet.java From redisson with Apache License 2.0 | 4 votes |
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; }