【fix sonar】DistributedLockHandler文件
This commit is contained in:
+25
-26
@@ -1,24 +1,19 @@
|
|||||||
package com.jero.boot.starter.lock.aspect;
|
package com.jero.boot.starter.lock.aspect;
|
||||||
|
|
||||||
|
import com.jero.boot.starter.lock.annotation.JLock;
|
||||||
|
import com.jero.boot.starter.lock.enums.LockModel;
|
||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.aspectj.lang.ProceedingJoinPoint;
|
import org.aspectj.lang.ProceedingJoinPoint;
|
||||||
import org.aspectj.lang.annotation.Around;
|
import org.aspectj.lang.annotation.Around;
|
||||||
import org.aspectj.lang.annotation.Aspect;
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
import org.aspectj.lang.reflect.MethodSignature;
|
import org.aspectj.lang.reflect.MethodSignature;
|
||||||
import com.jero.boot.starter.lock.annotation.JLock;
|
|
||||||
import com.jero.boot.starter.lock.enums.LockModel;
|
|
||||||
import org.redisson.RedissonMultiLock;
|
import org.redisson.RedissonMultiLock;
|
||||||
import org.redisson.RedissonRedLock;
|
import org.redisson.RedissonRedLock;
|
||||||
import org.redisson.api.RLock;
|
import org.redisson.api.RLock;
|
||||||
import org.redisson.api.RedissonClient;
|
import org.redisson.api.RedissonClient;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
|
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
|
||||||
import org.springframework.expression.EvaluationContext;
|
|
||||||
import org.springframework.expression.Expression;
|
|
||||||
import org.springframework.expression.ExpressionParser;
|
|
||||||
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
|
||||||
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -98,25 +93,14 @@ public class DistributedLockHandler extends BaseAspect{
|
|||||||
}
|
}
|
||||||
RLock rLock = null;
|
RLock rLock = null;
|
||||||
String keyConstant = jLock.keyConstant();
|
String keyConstant = jLock.keyConstant();
|
||||||
if (lockModel.equals(LockModel.AUTO)) {
|
lockModel = getLockModel(keys, lockModel);
|
||||||
if (keys.length > 1) {
|
|
||||||
lockModel = LockModel.REDLOCK;
|
|
||||||
} else {
|
|
||||||
lockModel = LockModel.REENTRANT;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
switch (lockModel) {
|
switch (lockModel) {
|
||||||
case FAIR:
|
case FAIR:
|
||||||
rLock = redissonClient.getFairLock(getValueBySpEL(keys[0], parameterNames, args, keyConstant).get(0));
|
rLock = redissonClient.getFairLock(getValueBySpEL(keys[0], parameterNames, args, keyConstant).get(0));
|
||||||
break;
|
break;
|
||||||
case REDLOCK:
|
case REDLOCK:
|
||||||
List<RLock> rLocks = new ArrayList<>();
|
List<RLock> rLocks = new ArrayList<>();
|
||||||
for (String key : keys) {
|
getRedLock(keys, parameterNames, args, keyConstant, rLocks);
|
||||||
List<String> valueBySpEL = getValueBySpEL(key, parameterNames, args, keyConstant);
|
|
||||||
for (String s : valueBySpEL) {
|
|
||||||
rLocks.add(redissonClient.getLock(s));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
RLock[] locks = new RLock[rLocks.size()];
|
RLock[] locks = new RLock[rLocks.size()];
|
||||||
int index = 0;
|
int index = 0;
|
||||||
for (RLock r : rLocks) {
|
for (RLock r : rLocks) {
|
||||||
@@ -127,12 +111,7 @@ public class DistributedLockHandler extends BaseAspect{
|
|||||||
case MULTIPLE:
|
case MULTIPLE:
|
||||||
rLocks = new ArrayList<>();
|
rLocks = new ArrayList<>();
|
||||||
|
|
||||||
for (String key : keys) {
|
getRedLock(keys, parameterNames, args, keyConstant, rLocks);
|
||||||
List<String> valueBySpEL = getValueBySpEL(key, parameterNames, args, keyConstant);
|
|
||||||
for (String s : valueBySpEL) {
|
|
||||||
rLocks.add(redissonClient.getLock(s));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
locks = new RLock[rLocks.size()];
|
locks = new RLock[rLocks.size()];
|
||||||
index = 0;
|
index = 0;
|
||||||
for (RLock r : rLocks) {
|
for (RLock r : rLocks) {
|
||||||
@@ -164,4 +143,24 @@ public class DistributedLockHandler extends BaseAspect{
|
|||||||
}
|
}
|
||||||
return rLock;
|
return rLock;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private LockModel getLockModel(String[] keys, LockModel lockModel) {
|
||||||
|
if (lockModel.equals(LockModel.AUTO)) {
|
||||||
|
if (keys.length > 1) {
|
||||||
|
lockModel = LockModel.REDLOCK;
|
||||||
|
} else {
|
||||||
|
lockModel = LockModel.REENTRANT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return lockModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void getRedLock(String[] keys, String[] parameterNames, Object[] args, String keyConstant, List<RLock> rLocks) {
|
||||||
|
for (String key : keys) {
|
||||||
|
List<String> valueBySpEL = getValueBySpEL(key, parameterNames, args, keyConstant);
|
||||||
|
for (String s : valueBySpEL) {
|
||||||
|
rLocks.add(redissonClient.getLock(s));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user