Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged

Dev #32

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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);
}
}
7 changes: 6 additions & 1 deletion mate-core/mate-starter-lock/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
<artifactId>mate-starter-lock</artifactId>

<dependencies>
<dependency>
<groupId>vip.mate</groupId>
<artifactId>mate-starter-common</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
Expand All @@ -31,4 +36,4 @@
</dependency>
</dependencies>

</project>
</project>
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;
}

}