天天看点

自定义注解和Aspect实现在控制层上方法统一加锁

使用场景:在分布式系统中需要对一些方法进行分布式锁的添加,保证原子性。但是加锁的代码基本是重复的,每个方法都写一遍这个加锁代码,这显然不合适,所以这里才有了自定义一个注解和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;
    }
}