使用場景:在分布式系統中需要對一些方法進行分布式鎖的添加,保證原子性。但是加鎖的代碼基本是重複的,每個方法都寫一遍這個加鎖代碼,這顯然不合适,是以這裡才有了自定義一個注解和Aspect 切面程式設計實作加鎖。
自定義注解類:
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface OperateLock {
String value() default "orderNo";
}
切面類:
@Aspect
@Component
public class LockControllerAspect {
private static final Logger logger = LoggerFactory.getLogger(LockControllerAspect.class);
@Autowired
private ConcurrentLockService concurrentLockService;
@Pointcut("execution(* com.xinchengbaina.retail.middleend.api.controller.*.*(..))")
public void operate() {
}
@Around("operate()")
public Object aroundOperate(ProceedingJoinPoint joinPoint) throws Throwable {
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
Object result = null;
//是否加鎖
boolean noLock = true;
OperateLock operateLock = method.getAnnotation(OperateLock.class);
if (operateLock != null) {
OrderRequest operator = null;
for (Object o : joinPoint.getArgs()) {
if (o instanceof OrderRequest) {
operator = (OrderRequest) o;
break;
}
}
if (operator != null) {
Long orderNo = operator.getOrderNo();
if (!concurrentLockService.lock(orderNo)) {
throw new BusinessException(OrderExceptionEnum.OPERATE_BUSY.getCode());
}
try {
noLock = false;
result = joinPoint.proceed();
} catch (Exception e) {
throw e;
} finally {
concurrentLockService.unlock(orderNo);
}
}
}
if (noLock) {
result = joinPoint.proceed();
}
return result;
}
}