From e9746ee9af89b226ef22a072abfcc23402743311 Mon Sep 17 00:00:00 2001 From: Parker Date: Sun, 7 Feb 2021 13:39:46 +0800 Subject: [PATCH] =?UTF-8?q?redisson=20=E5=88=86=E5=B8=83=E5=BC=8F=E9=94=81?= =?UTF-8?q?=20BUG=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../annotation/DistributedLockHandler.java | 42 ++++++++++--------- .../plugins/redisson/conf/RedissonConfig.java | 2 +- .../properties/RedissonProperties.java | 2 + 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/opsli-plugins/opsli-plugins-redisson/src/main/java/org/opsli/plugins/redisson/annotation/DistributedLockHandler.java b/opsli-plugins/opsli-plugins-redisson/src/main/java/org/opsli/plugins/redisson/annotation/DistributedLockHandler.java index 3d58366..b10c47b 100644 --- a/opsli-plugins/opsli-plugins-redisson/src/main/java/org/opsli/plugins/redisson/annotation/DistributedLockHandler.java +++ b/opsli-plugins/opsli-plugins-redisson/src/main/java/org/opsli/plugins/redisson/annotation/DistributedLockHandler.java @@ -23,32 +23,36 @@ import org.springframework.stereotype.Component; @Slf4j public class DistributedLockHandler { - @Autowired + @Autowired(required = false) private RedissonLock redissonLock; @Around("@annotation(distributedLock)") - public Object around(ProceedingJoinPoint joinPoint, DistributedLock distributedLock) { + public Object around(ProceedingJoinPoint joinPoint, DistributedLock distributedLock) throws Throwable { Object returnValue = null; - log.info("[开始]执行RedisLock环绕通知,获取Redis分布式锁开始"); - //获取锁名称 - String lockName = distributedLock.value(); - //获取超时时间,默认10秒 - int leaseTime = distributedLock.leaseTime(); - redissonLock.lock(lockName, leaseTime); - try { - log.info("获取Redis分布式锁[成功],加锁完成,开始执行业务逻辑..."); - returnValue = joinPoint.proceed(); - } catch (Throwable throwable) { - log.error("获取Redis分布式锁[异常],加锁失败", throwable); - throwable.printStackTrace(); - } finally { - //如果该线程还持有该锁,那么释放该锁。如果该线程不持有该锁,说明该线程的锁已到过期时间,自动释放锁 - if (redissonLock.isHeldByCurrentThread(lockName)) { - redissonLock.unlock(lockName); + if(redissonLock != null){ + log.info("[开始]执行RedisLock环绕通知,获取Redis分布式锁开始"); + //获取锁名称 + String lockName = distributedLock.value(); + //获取超时时间,默认10秒 + int leaseTime = distributedLock.leaseTime(); + redissonLock.lock(lockName, leaseTime); + try { + log.info("获取Redis分布式锁[成功],加锁完成,开始执行业务逻辑..."); + returnValue = joinPoint.proceed(); + } catch (Throwable throwable) { + log.error("获取Redis分布式锁[异常],加锁失败", throwable); + throwable.printStackTrace(); + } finally { + //如果该线程还持有该锁,那么释放该锁。如果该线程不持有该锁,说明该线程的锁已到过期时间,自动释放锁 + if (redissonLock.isHeldByCurrentThread(lockName)) { + redissonLock.unlock(lockName); + } } + log.info("释放Redis分布式锁[成功],解锁完成,结束业务逻辑..."); + }else { + returnValue = joinPoint.proceed(); } - log.info("释放Redis分布式锁[成功],解锁完成,结束业务逻辑..."); return returnValue; } diff --git a/opsli-plugins/opsli-plugins-redisson/src/main/java/org/opsli/plugins/redisson/conf/RedissonConfig.java b/opsli-plugins/opsli-plugins-redisson/src/main/java/org/opsli/plugins/redisson/conf/RedissonConfig.java index 02d9d5e..7dd6752 100644 --- a/opsli-plugins/opsli-plugins-redisson/src/main/java/org/opsli/plugins/redisson/conf/RedissonConfig.java +++ b/opsli-plugins/opsli-plugins-redisson/src/main/java/org/opsli/plugins/redisson/conf/RedissonConfig.java @@ -34,8 +34,8 @@ import org.springframework.core.annotation.Order; */ @Slf4j @Configuration -@ConditionalOnProperty(prefix = RedissonProperties.PROP_PREFIX, name = "enable", havingValue = "true") @EnableConfigurationProperties(RedissonProperties.class) +@ConditionalOnProperty(prefix = RedissonProperties.PROP_PREFIX, name = "enable", havingValue = "true") public class RedissonConfig { @Bean diff --git a/opsli-plugins/opsli-plugins-redisson/src/main/java/org/opsli/plugins/redisson/properties/RedissonProperties.java b/opsli-plugins/opsli-plugins-redisson/src/main/java/org/opsli/plugins/redisson/properties/RedissonProperties.java index 8e59016..0b07a01 100644 --- a/opsli-plugins/opsli-plugins-redisson/src/main/java/org/opsli/plugins/redisson/properties/RedissonProperties.java +++ b/opsli-plugins/opsli-plugins-redisson/src/main/java/org/opsli/plugins/redisson/properties/RedissonProperties.java @@ -2,6 +2,7 @@ package org.opsli.plugins.redisson.properties; import lombok.AllArgsConstructor; import lombok.Data; +import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; import org.opsli.plugins.redisson.enums.RedissonType; import org.springframework.boot.context.properties.ConfigurationProperties; @@ -16,6 +17,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties; @Data @NoArgsConstructor @AllArgsConstructor +@EqualsAndHashCode(callSuper = false) public class RedissonProperties { public static final String PROP_PREFIX = "redisson.lock.server";