Commit 3f4cf438 by ThomasAdv

获取oneId时的分布式锁超时时间灵活可控

1 parent bbfc6c91
package cn.com.glsx.cdp.common.distributeLock;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import lombok.extern.slf4j.Slf4j;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.data.redis.connection.RedisConnectionFactory;
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.integration.support.locks.LockRegistry;
import org.springframework.integration.redis.util.RedisLockRegistry;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.MessageFormat;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
@Slf4j
@Component
public class DistributeLockMethodInterceptor implements MethodInterceptor {
private LockRegistry lockRegistry;
@Autowired
private RedisConnectionFactory redisConnectionFactory;
private RedisLockRegistry lockRegistry;
@NacosValue(value = "${distributeLock.timeout.seconds:10}", autoRefreshed = true)
private Integer globalLockSecondsTime;
public DistributeLockMethodInterceptor(LockRegistry lockRegistry, Integer globalLockSecondsTime) {
this.lockRegistry = lockRegistry;
this.globalLockSecondsTime = globalLockSecondsTime;
}
private static final Integer DEFAULT_LOCK_TIMEOUT_SECONDS = 10;
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
if (lockRegistry == null) {
lockRegistry = new RedisLockRegistry(redisConnectionFactory, "lock");
}
String className = invocation.getThis().getClass().getName();
String methodName = invocation.getMethod().getName();
GlsxDistributeLockable axLockable = invocation.getThis().getClass().getMethod(invocation.getMethod().getName(), invocation.getMethod().getParameterTypes()).getAnnotation(GlsxDistributeLockable.class);
......@@ -38,8 +49,10 @@ public class DistributeLockMethodInterceptor implements MethodInterceptor {
} else {
int time = axLockable.time();
TimeUnit timeUnit = axLockable.timeUnit();
if (time < 0 && this.globalLockSecondsTime != null && this.globalLockSecondsTime > 0) {
time = this.globalLockSecondsTime;
log.info("globalLockSecondsTime:{}", globalLockSecondsTime);
globalLockSecondsTime = Optional.ofNullable(globalLockSecondsTime).orElse(DEFAULT_LOCK_TIMEOUT_SECONDS);
if (time < 0 && globalLockSecondsTime != null && globalLockSecondsTime > 0) {
time = globalLockSecondsTime;
timeUnit = TimeUnit.SECONDS;
}
......
......@@ -3,22 +3,23 @@ package cn.com.glsx.cdp.config;
import cn.com.glsx.cdp.common.distributeLock.DistributeLockMethodInterceptor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.integration.redis.util.RedisLockRegistry;
@Configuration
public class InteceptorConfig {
@Autowired
private DistributeLockMethodInterceptor distributeLockMethodInterceptor;
@Bean
public DefaultPointcutAdvisor glsxDistributeLockAdvisor(RedisConnectionFactory redisConnectionFactory){
public DefaultPointcutAdvisor glsxDistributeLockAdvisor(){
DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
RedisLockRegistry lockRegistry = new RedisLockRegistry(redisConnectionFactory, "lock");
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
pointcut.setExpression("@annotation(cn.com.glsx.cdp.common.distributeLock.GlsxDistributeLockable)");
advisor.setPointcut(pointcut);
advisor.setAdvice(new DistributeLockMethodInterceptor(lockRegistry, 10));
advisor.setAdvice(distributeLockMethodInterceptor);
return advisor;
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!