update 优化redis锁工具代码结构
This commit is contained in:
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,17 +95,20 @@ 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;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取计数器锁
|
* 获取计数器锁
|
||||||
|
@ -75,10 +116,9 @@ public class RedisLockManager {
|
||||||
* @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…
Reference in New Issue