package cn.besteam.common.aspect;
|
|
import cn.besteam.common.aspect.annotation.LockApi;
|
import com.baomidou.lock.LockExecutor;
|
import com.baomidou.lock.LockInfo;
|
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;
|
|
/**
|
* 系统日志,切面处理类
|
*
|
* @author chenqilong
|
* @since 2019/4/19
|
*/
|
@Aspect
|
@Component
|
public class LockApiAspect {
|
|
@Autowired
|
private LockExecutor lockExecutor;
|
|
private LockKeyGenerator lockKeyGenerator = new LockKeyGenerator();
|
|
@Pointcut("@annotation(cn.besteam.common.aspect.annotation.LockApi)")
|
public void lockPointCut() {
|
|
}
|
|
@Around("lockPointCut()")
|
public Object around(ProceedingJoinPoint point) throws Throwable {
|
|
LockInfo lockInfo = null;
|
try {
|
MethodSignature signature = (MethodSignature) point.getSignature();
|
Method method = signature.getMethod();
|
LockApi lockApi = method.getAnnotation(LockApi.class);
|
String keyName = lockKeyGenerator.getKeyName(point, lockApi);
|
lockInfo = lockExecutor.tryLock(keyName, lockApi.expire(), lockApi.tryTimeout());
|
if (null != lockInfo) {
|
return point.proceed();
|
}
|
return null;
|
} finally {
|
if (null != lockInfo) {
|
lockExecutor.releaseLock(lockInfo);
|
}
|
}
|
}
|
|
}
|