update 优化redis锁工具代码结构

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

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

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

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

Loading…
Cancel
Save