|
|
@@ -19,56 +19,56 @@ public class RedisLockUtil {
|
|
|
private RedissonClient redissonClient;
|
|
|
|
|
|
/**
|
|
|
- * 加锁
|
|
|
+ * 获取锁
|
|
|
*
|
|
|
* @param lockKey 锁的key
|
|
|
- * @return
|
|
|
*/
|
|
|
- public boolean tryLock(String lockKey) {
|
|
|
- return tryLock(lockKey, 5, 10);
|
|
|
+ public RLock getLock(String lockKey) {
|
|
|
+ return redissonClient.getLock(lockKey);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 加锁
|
|
|
+ * 默认加锁时间30秒
|
|
|
*
|
|
|
* @param lockKey 锁的key
|
|
|
* @return
|
|
|
*/
|
|
|
- public boolean tryLock(String lockKey, int waitTime, int leaseTime) {
|
|
|
+ public boolean tryLock(String lockKey) {
|
|
|
RLock lock = redissonClient.getLock(lockKey);
|
|
|
- try {
|
|
|
- return lock.tryLock(waitTime, leaseTime, TimeUnit.SECONDS);
|
|
|
- } catch (InterruptedException e) {
|
|
|
- log.info("tryLock lockKey:{} waitTime:{} leaseTime:{}", lockKey, waitTime, leaseTime);
|
|
|
- return false;
|
|
|
- }
|
|
|
+ return lock.tryLock();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 加锁
|
|
|
+ * 默认加锁时间30秒
|
|
|
*
|
|
|
- * @param lockKey 锁的key
|
|
|
+ * @param lockKey 持有的锁key
|
|
|
+ * @param waitTime 加锁等待时间
|
|
|
* @return
|
|
|
*/
|
|
|
- public RLock lock(String lockKey) {
|
|
|
- RLock lock = redissonClient.getLock(lockKey);
|
|
|
- lock.lock();
|
|
|
- return lock;
|
|
|
+ public boolean tryLock(String lockKey, int waitTime) {
|
|
|
+ return tryLock(lockKey, waitTime, -1);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 加锁
|
|
|
*
|
|
|
- * @param lockKey 锁的key
|
|
|
- * @param timeout 超时时间
|
|
|
- * @return
|
|
|
+ * @param lockKey 锁的key
|
|
|
+ * @param waitTime 加锁等待时间
|
|
|
+ * @param leaseTime 锁自动释放时间
|
|
|
*/
|
|
|
- public RLock lock(String lockKey, long timeout) {
|
|
|
+ public boolean tryLock(String lockKey, int waitTime, int leaseTime) {
|
|
|
RLock lock = redissonClient.getLock(lockKey);
|
|
|
- lock.lock(timeout, TimeUnit.SECONDS);
|
|
|
- return lock;
|
|
|
+ try {
|
|
|
+ return lock.tryLock(waitTime, leaseTime, TimeUnit.SECONDS);
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ log.info("tryLock lockKey:{} waitTime:{} leaseTime:{}", lockKey, waitTime, leaseTime);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* 解锁
|
|
|
*
|