2gm)DyRKublj4oo&08-L+*IZ_}V<%9f)#63PLQI``&
zfEI%Ba>CwQzpf^r3H*BkxIYMRRGYL^Dz^?Ox1QYrL+ag_?H
zQaN2QxJrk1L2))|sZ^yiE@y^uXl70~6fS3nb|J$Vx0UKr6VL?i5~!$03(x=E$M64l
zlk}4&pb0!H0=&B8?6lyM=xiPPI6P|w_%S#d#+6EC3KVo4%L*RF%iz+WPhbVqCM}gh
R3z+^9kQj8K3H(
Date: Fri, 11 Jun 2021 11:56:03 +0800
Subject: [PATCH 4/4] =?UTF-8?q?[fix]=20=E6=8D=95=E8=8E=B7=E4=B8=9A?=
=?UTF-8?q?=E5=8A=A1=E5=BC=82=E5=B8=B8?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../exception/RedissonLockException.java | 16 ++++++++
mate-core/mate-starter-lock/pom.xml | 7 +++-
.../lock/aspect/DistributedLockHandler.java | 37 +++++++++----------
3 files changed, 39 insertions(+), 21 deletions(-)
create mode 100644 mate-core/mate-starter-common/src/main/java/vip/mate/core/common/exception/RedissonLockException.java
diff --git a/mate-core/mate-starter-common/src/main/java/vip/mate/core/common/exception/RedissonLockException.java b/mate-core/mate-starter-common/src/main/java/vip/mate/core/common/exception/RedissonLockException.java
new file mode 100644
index 00000000..625f8502
--- /dev/null
+++ b/mate-core/mate-starter-common/src/main/java/vip/mate/core/common/exception/RedissonLockException.java
@@ -0,0 +1,16 @@
+package vip.mate.core.common.exception;
+
+
+/**
+ * 获取锁处理异常
+ *
+ * @author zhangyu
+ */
+public class RedissonLockException extends RuntimeException {
+
+ private static final long serialVersionUID = -6422212844622271825L;
+
+ public RedissonLockException(String message) {
+ super(message);
+ }
+}
diff --git a/mate-core/mate-starter-lock/pom.xml b/mate-core/mate-starter-lock/pom.xml
index 65ec6995..edbf0a68 100644
--- a/mate-core/mate-starter-lock/pom.xml
+++ b/mate-core/mate-starter-lock/pom.xml
@@ -12,6 +12,11 @@
mate-starter-lock
+
+ vip.mate
+ mate-starter-common
+ provided
+
org.redisson
redisson
@@ -31,4 +36,4 @@
-
\ No newline at end of file
+
diff --git a/mate-core/mate-starter-lock/src/main/java/vip/mate/core/lock/aspect/DistributedLockHandler.java b/mate-core/mate-starter-lock/src/main/java/vip/mate/core/lock/aspect/DistributedLockHandler.java
index 5974c622..68bd202a 100644
--- a/mate-core/mate-starter-lock/src/main/java/vip/mate/core/lock/aspect/DistributedLockHandler.java
+++ b/mate-core/mate-starter-lock/src/main/java/vip/mate/core/lock/aspect/DistributedLockHandler.java
@@ -1,12 +1,13 @@
package vip.mate.core.lock.aspect;
+import java.text.MessageFormat;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
-import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
+import vip.mate.core.common.exception.RedissonLockException;
import vip.mate.core.lock.RedissonLock;
import vip.mate.core.lock.annotation.DistributedLock;
@@ -27,33 +28,29 @@ public class DistributedLockHandler {
/**
* 切面环绕通知
- * @param joinPoint ProceedingJoinPoint
+ *
+ * @param joinPoint ProceedingJoinPoint
* @param distributedLock DistributedLock
* @return Object
*/
@Around("@annotation(distributedLock)")
- public Object around(ProceedingJoinPoint joinPoint, DistributedLock distributedLock) {
+ public Object around(ProceedingJoinPoint joinPoint, DistributedLock distributedLock) throws Throwable {
log.info("[开始]执行RedisLock环绕通知,获取Redis分布式锁开始");
//获取锁名称
String lockName = distributedLock.value();
- //获取超时时间
- int expireSeconds = distributedLock.expireSeconds();
-
- if (redissonLock.lock(lockName, expireSeconds)) {
- try {
- log.info("获取Redis分布式锁[成功],加锁完成,开始执行业务逻辑...");
- return joinPoint.proceed();
- } catch (Throwable throwable) {
- log.error("获取Redis分布式锁[异常],加锁失败", throwable);
- throwable.printStackTrace();
- } finally {
- redissonLock.release(lockName);
- }
- log.info("释放Redis分布式锁[成功],解锁完成,结束业务逻辑...");
- } else {
+ //获取超时时间并获取锁
+ if (!redissonLock.lock(lockName, distributedLock.expireSeconds())) {
log.error("获取Redis分布式锁[失败]");
+ throw new RedissonLockException(MessageFormat.format("获取Redis分布式失败:{0}", lockName));
+ }
+ log.info("获取Redis分布式锁[成功],加锁完成,开始执行业务逻辑...");
+ try {
+ return joinPoint.proceed();
+ } finally {
+ redissonLock.release(lockName);
+ log.info("释放Redis分布式锁[成功],解锁完成,结束业务逻辑...");
+ log.info("[结束]执行RedisLock环绕通知");
}
- log.info("[结束]执行RedisLock环绕通知");
- return null;
}
+
}