![JAR search and dependency download from the Maven repository](/logo.png)
com.jiangkedev.lock.DistributedLockAspect Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of service-base Show documentation
Show all versions of service-base Show documentation
a common utils for spring boot project
The newest version!
package com.jiangkedev.lock;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.Arrays;
/**
* @author jiangke
* @date 22:06
*/
@Aspect
@Component
@Slf4j
public class DistributedLockAspect {
@Autowired
private DistributedLock distributedLock;
@Pointcut("@annotation(com.jiangkedev.lock.RedisLock)")
private void lockPoint(){
}
@Around("lockPoint()")
public Object around(ProceedingJoinPoint pjp) throws Throwable{
Method method = ((MethodSignature)pjp.getSignature()).getMethod();
RedisLock redisLock = method.getAnnotation(RedisLock.class);
String key = redisLock.value();
if(StringUtils.isEmpty(key)) {
Object[] args = pjp.getArgs();
key = Arrays.toString(args);
}
int retryTimes = redisLock.action().equals(RedisLock.LockFailAction.CONTINUE) ? redisLock.retryTimes():0;
boolean lock = distributedLock.lock(key,redisLock.keepMills(),retryTimes,redisLock.sllepMills());
if(!lock){
log.debug("get lock failed: " + key);
return null;
}
//得到锁执行方法
log.info("get lock sucess: "+ key);
try{
return pjp.proceed();
}catch (Exception e){
log.error("execute locked method occured an exception {}", e);
}finally {
boolean releaseResult = distributedLock.releaseLock(key);
log.debug("release lock : " + key + (releaseResult ? " success" : " failed"));
}
return null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy