【fix sonar】DistributedLockHandler文件
This commit is contained in:
+25
-26
@@ -1,24 +1,19 @@
|
||||
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.extern.slf4j.Slf4j;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
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.RedissonRedLock;
|
||||
import org.redisson.api.RLock;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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 java.util.ArrayList;
|
||||
@@ -98,25 +93,14 @@ public class DistributedLockHandler extends BaseAspect{
|
||||
}
|
||||
RLock rLock = null;
|
||||
String keyConstant = jLock.keyConstant();
|
||||
if (lockModel.equals(LockModel.AUTO)) {
|
||||
if (keys.length > 1) {
|
||||
lockModel = LockModel.REDLOCK;
|
||||
} else {
|
||||
lockModel = LockModel.REENTRANT;
|
||||
}
|
||||
}
|
||||
lockModel = getLockModel(keys, lockModel);
|
||||
switch (lockModel) {
|
||||
case FAIR:
|
||||
rLock = redissonClient.getFairLock(getValueBySpEL(keys[0], parameterNames, args, keyConstant).get(0));
|
||||
break;
|
||||
case REDLOCK:
|
||||
List<RLock> rLocks = new ArrayList<>();
|
||||
for (String key : keys) {
|
||||
List<String> valueBySpEL = getValueBySpEL(key, parameterNames, args, keyConstant);
|
||||
for (String s : valueBySpEL) {
|
||||
rLocks.add(redissonClient.getLock(s));
|
||||
}
|
||||
}
|
||||
getRedLock(keys, parameterNames, args, keyConstant, rLocks);
|
||||
RLock[] locks = new RLock[rLocks.size()];
|
||||
int index = 0;
|
||||
for (RLock r : rLocks) {
|
||||
@@ -127,12 +111,7 @@ public class DistributedLockHandler extends BaseAspect{
|
||||
case MULTIPLE:
|
||||
rLocks = new ArrayList<>();
|
||||
|
||||
for (String key : keys) {
|
||||
List<String> valueBySpEL = getValueBySpEL(key, parameterNames, args, keyConstant);
|
||||
for (String s : valueBySpEL) {
|
||||
rLocks.add(redissonClient.getLock(s));
|
||||
}
|
||||
}
|
||||
getRedLock(keys, parameterNames, args, keyConstant, rLocks);
|
||||
locks = new RLock[rLocks.size()];
|
||||
index = 0;
|
||||
for (RLock r : rLocks) {
|
||||
@@ -164,4 +143,24 @@ public class DistributedLockHandler extends BaseAspect{
|
||||
}
|
||||
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