redisson 分布式锁 BUG修复

v1.4.1
Parker 5 years ago
parent 2230591f9e
commit e9746ee9af

@ -23,32 +23,36 @@ import org.springframework.stereotype.Component;
@Slf4j @Slf4j
public class DistributedLockHandler { public class DistributedLockHandler {
@Autowired @Autowired(required = false)
private RedissonLock redissonLock; private RedissonLock redissonLock;
@Around("@annotation(distributedLock)") @Around("@annotation(distributedLock)")
public Object around(ProceedingJoinPoint joinPoint, DistributedLock distributedLock) { public Object around(ProceedingJoinPoint joinPoint, DistributedLock distributedLock) throws Throwable {
Object returnValue = null; Object returnValue = null;
log.info("[开始]执行RedisLock环绕通知,获取Redis分布式锁开始"); if(redissonLock != null){
//获取锁名称 log.info("[开始]执行RedisLock环绕通知,获取Redis分布式锁开始");
String lockName = distributedLock.value(); //获取锁名称
//获取超时时间默认10秒 String lockName = distributedLock.value();
int leaseTime = distributedLock.leaseTime(); //获取超时时间默认10秒
redissonLock.lock(lockName, leaseTime); int leaseTime = distributedLock.leaseTime();
try { redissonLock.lock(lockName, leaseTime);
log.info("获取Redis分布式锁[成功],加锁完成,开始执行业务逻辑..."); try {
returnValue = joinPoint.proceed(); log.info("获取Redis分布式锁[成功],加锁完成,开始执行业务逻辑...");
} catch (Throwable throwable) { returnValue = joinPoint.proceed();
log.error("获取Redis分布式锁[异常],加锁失败", throwable); } catch (Throwable throwable) {
throwable.printStackTrace(); log.error("获取Redis分布式锁[异常],加锁失败", throwable);
} finally { throwable.printStackTrace();
//如果该线程还持有该锁,那么释放该锁。如果该线程不持有该锁,说明该线程的锁已到过期时间,自动释放锁 } finally {
if (redissonLock.isHeldByCurrentThread(lockName)) { //如果该线程还持有该锁,那么释放该锁。如果该线程不持有该锁,说明该线程的锁已到过期时间,自动释放锁
redissonLock.unlock(lockName); if (redissonLock.isHeldByCurrentThread(lockName)) {
redissonLock.unlock(lockName);
}
} }
log.info("释放Redis分布式锁[成功],解锁完成,结束业务逻辑...");
}else {
returnValue = joinPoint.proceed();
} }
log.info("释放Redis分布式锁[成功],解锁完成,结束业务逻辑...");
return returnValue; return returnValue;
} }

@ -34,8 +34,8 @@ import org.springframework.core.annotation.Order;
*/ */
@Slf4j @Slf4j
@Configuration @Configuration
@ConditionalOnProperty(prefix = RedissonProperties.PROP_PREFIX, name = "enable", havingValue = "true")
@EnableConfigurationProperties(RedissonProperties.class) @EnableConfigurationProperties(RedissonProperties.class)
@ConditionalOnProperty(prefix = RedissonProperties.PROP_PREFIX, name = "enable", havingValue = "true")
public class RedissonConfig { public class RedissonConfig {
@Bean @Bean

@ -2,6 +2,7 @@ package org.opsli.plugins.redisson.properties;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import org.opsli.plugins.redisson.enums.RedissonType; import org.opsli.plugins.redisson.enums.RedissonType;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
@ -16,6 +17,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
@Data @Data
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class RedissonProperties { public class RedissonProperties {
public static final String PROP_PREFIX = "redisson.lock.server"; public static final String PROP_PREFIX = "redisson.lock.server";

Loading…
Cancel
Save