org.redisson.RedissonRedLock Java Examples
The following examples show how to use
org.redisson.RedissonRedLock.
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 |
/** * 红锁(RedLock) * Redisson的RedissonRedLock对象实现了Redlock介绍的加锁算法。该对象也可以用来将多个RLock对象关联为一个红锁,每个RLock对象实例可以来自于不同的Redisson实例 * @param redisson1 * @param redisson2 * @param redisson3 */ public void testRedLock(RedissonClient redisson1,RedissonClient redisson2, RedissonClient redisson3){ RLock lock1 = redisson1.getLock("lock1"); RLock lock2 = redisson2.getLock("lock2"); RLock lock3 = redisson3.getLock("lock3"); RedissonRedLock lock = new RedissonRedLock(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: SeedLockHelper.java From seed with Apache License 2.0 | 5 votes |
public static void unlock(){ RedissonRedLock redLock = redLockMap.get(); if(null != redLock){ redLock.unlock(); LogUtil.getLogger().info("资源[{}]解锁-->完毕", keyMap.get()); } keyMap.remove(); redLockMap.remove(); }