update 优化redis锁工具代码结构

master
疯狂的狮子li 5 years ago
parent 524683eabd
commit 1bf9dd1217

@ -22,15 +22,50 @@ public class RedisLockManager {
@Autowired @Autowired
private RedissonClient redissonClient; private RedissonClient redissonClient;
private final ThreadLocal<RLock> threadLocal = new ThreadLocal<>(); /**
*
*/
private final static Integer BASE_LOCK = 1;
/**
*
*/
private final static Integer FAIR_LOCK = 2;
/**
*
*/
private final static Integer COUNT_LOCK = 3;
/**
* 线
*/
private final ThreadLocal<Integer> threadLocal = new ThreadLocal<>();
/**
*
*/
private <T> T getLock(String key, Integer lockType) {
Assert.isTrue(StrUtil.isNotBlank(key), "key不能为空");
threadLocal.set(lockType);
Object lock;
if (BASE_LOCK.equals(lockType)) {
lock = redissonClient.getLock(key);
} else if (FAIR_LOCK.equals(lockType)) {
lock = redissonClient.getFairLock(key);
} else if (COUNT_LOCK.equals(lockType)) {
lock = redissonClient.getCountDownLatch(key);
} else {
throw new RuntimeException("锁不存在!");
}
return (T)lock;
}
/** /**
* *
*/ */
public boolean getLock(String key) { public boolean getLock(String key) {
Assert.isTrue(StrUtil.isNotBlank(key), "key不能为空"); RLock lock = getLock(key, BASE_LOCK);
RLock lock = redissonClient.getLock(key);
threadLocal.set(lock);
return lock.tryLock(); return lock.tryLock();
} }
@ -41,13 +76,16 @@ public class RedisLockManager {
* @param time * @param time
* @param expireUnit * @param expireUnit
*/ */
public boolean getLock(String key, long time, TimeUnit expireUnit) throws InterruptedException { public boolean getLock(String key, long time, TimeUnit expireUnit) {
Assert.isTrue(StrUtil.isNotBlank(key), "key不能为空");
Assert.isTrue(time > 0, "过期时间必须大于0"); Assert.isTrue(time > 0, "过期时间必须大于0");
Assert.isTrue(Validator.isNotEmpty(expireUnit), "时间单位不能为空"); Assert.isTrue(Validator.isNotEmpty(expireUnit), "时间单位不能为空");
RLock lock = redissonClient.getLock(key); RLock lock = getLock(key, BASE_LOCK);
threadLocal.set(lock); try {
return lock.tryLock(time, expireUnit); return lock.tryLock(time, expireUnit);
} catch (InterruptedException e) {
e.printStackTrace();
return false;
}
} }
/** /**
@ -57,28 +95,30 @@ public class RedisLockManager {
* @param waitTime * @param waitTime
* @param leaseTime * @param leaseTime
* @param expireUnit * @param expireUnit
* @throws InterruptedException
*/ */
public boolean getLock(String key, long waitTime, long leaseTime, TimeUnit expireUnit) throws InterruptedException { public boolean getLock(String key, long waitTime, long leaseTime, TimeUnit expireUnit) {
Assert.isTrue(StrUtil.isNotBlank(key), "key不能为空");
Assert.isTrue(waitTime > 0, "获取锁等待时间必须大于0"); Assert.isTrue(waitTime > 0, "获取锁等待时间必须大于0");
Assert.isTrue(leaseTime > 0, "保留锁的时间必须大于0"); Assert.isTrue(leaseTime > 0, "保留锁的时间必须大于0");
Assert.isTrue(Validator.isNotEmpty(expireUnit), "时间单位不能为空"); Assert.isTrue(Validator.isNotEmpty(expireUnit), "时间单位不能为空");
RLock lock = redissonClient.getLock(key); RLock lock = getLock(key, BASE_LOCK);
threadLocal.set(lock); try {
return lock.tryLock(waitTime, leaseTime, expireUnit); return lock.tryLock(waitTime, leaseTime, expireUnit);
} catch (InterruptedException e) {
e.printStackTrace();
return false;
}
} }
/** /**
* *
* *
* @param key * @param key
* @param count countDownLatch * @param count countDownLatch
*/ */
public RCountDownLatch countDownLatch(String key, long count) { public RCountDownLatch getCountDownLatch(String key, long count) {
Assert.isTrue(StrUtil.isNotBlank(key), "key不能为空");
Assert.isTrue(count >= 0, "count数量必须大于等于0"); Assert.isTrue(count >= 0, "count数量必须大于等于0");
RCountDownLatch rCountDownLatch = redissonClient.getCountDownLatch(key); RCountDownLatch rCountDownLatch = getLock(key, COUNT_LOCK);
rCountDownLatch.trySetCount(count); rCountDownLatch.trySetCount(count);
return rCountDownLatch; return rCountDownLatch;
} }
@ -93,14 +133,17 @@ public class RedisLockManager {
* @return * @return
* @throws InterruptedException * @throws InterruptedException
*/ */
public boolean getFairLock(String key, long waitTime, long leaseTime, TimeUnit expireUnit) throws InterruptedException { public boolean getFairLock(String key, long waitTime, long leaseTime, TimeUnit expireUnit) {
Assert.isTrue(StrUtil.isNotBlank(key), "key不能为空");
Assert.isTrue(waitTime > 0, "获取锁等待时间必须大于0"); Assert.isTrue(waitTime > 0, "获取锁等待时间必须大于0");
Assert.isTrue(leaseTime > 0, "保留锁的时间必须大于0"); Assert.isTrue(leaseTime > 0, "保留锁的时间必须大于0");
Assert.isTrue(Validator.isNotEmpty(expireUnit), "时间单位不能为空"); Assert.isTrue(Validator.isNotEmpty(expireUnit), "时间单位不能为空");
RLock lock = redissonClient.getFairLock(key); RLock lock = getLock(key, FAIR_LOCK);
threadLocal.set(lock); try {
return lock.tryLock(waitTime, leaseTime, expireUnit); return lock.tryLock(waitTime, leaseTime, expireUnit);
} catch (InterruptedException e) {
e.printStackTrace();
return false;
}
} }
/** /**
@ -109,23 +152,25 @@ public class RedisLockManager {
* @param key * @param key
* @param leaseTime * @param leaseTime
* @param expireUnit * @param expireUnit
* @return
* @throws InterruptedException
*/ */
public boolean getFairLock(String key, long leaseTime, TimeUnit expireUnit) throws InterruptedException { public boolean getFairLock(String key, long leaseTime, TimeUnit expireUnit) {
Assert.isTrue(StrUtil.isNotBlank(key), "key不能为空");
Assert.isTrue(leaseTime > 0, "保留锁的时间必须大于0"); Assert.isTrue(leaseTime > 0, "保留锁的时间必须大于0");
Assert.isTrue(Validator.isNotEmpty(expireUnit), "时间单位不能为空"); Assert.isTrue(Validator.isNotEmpty(expireUnit), "时间单位不能为空");
RLock lock = redissonClient.getFairLock(key); RLock lock = getLock(key, FAIR_LOCK);
threadLocal.set(lock); try {
return lock.tryLock(leaseTime, expireUnit); return lock.tryLock(leaseTime, expireUnit);
} catch (InterruptedException e) {
e.printStackTrace();
return false;
}
} }
/** /**
* () * ()
*/ */
public void unLock() { public void unLock(String key) {
RLock lock = threadLocal.get(); Integer lockType = threadLocal.get();
RLock lock = getLock(key, lockType);
lock.unlock(); lock.unlock();
threadLocal.remove(); threadLocal.remove();
} }

@ -52,7 +52,7 @@ public class RedisLockController {
if (flag) { if (flag) {
log.info("获取锁成功: " + key); log.info("获取锁成功: " + key);
Thread.sleep(3000); Thread.sleep(3000);
redisLockManager.unLock(); redisLockManager.unLock(key);
log.info("释放锁成功: " + key); log.info("释放锁成功: " + key);
} else { } else {
log.error("获取锁失败: " + key); log.error("获取锁失败: " + key);

@ -10,7 +10,6 @@ import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature; import org.aspectj.lang.reflect.MethodSignature;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -78,7 +77,7 @@ public class RedisLockAspect {
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} finally { } finally {
redisLockManager.unLock(); redisLockManager.unLock(key);
log.info("unlock => key : " + key + " , ThreadName : " + Thread.currentThread().getName()); log.info("unlock => key : " + key + " , ThreadName : " + Thread.currentThread().getName());
} }
} else { } else {

Loading…
Cancel
Save