Search in sources :

Example 1 with RequestLockable

use of com.jim.framework.annotationlock.annotation.RequestLockable in project jim-framework by jiangmin168168.

the class AbstractRequestLockInterceptor method doAround.

@Around("pointcut()")
public Object doAround(ProceedingJoinPoint point) throws Throwable {
    Signature signature = point.getSignature();
    MethodSignature methodSignature = (MethodSignature) signature;
    Method method = methodSignature.getMethod();
    String targetName = point.getTarget().getClass().getName();
    String methodName = point.getSignature().getName();
    Object[] arguments = point.getArgs();
    if (method != null && method.isAnnotationPresent(RequestLockable.class)) {
        logger.info("RequestLockable doAround start");
        RequestLockable requestLockable = method.getAnnotation(RequestLockable.class);
        String requestLockKey = getLockKey(method, targetName, methodName, requestLockable.key(), arguments);
        Lock lock = this.getLock(requestLockKey);
        boolean isLock = this.tryLock(requestLockable.maximumWaiteTime(), requestLockable.expirationTime(), requestLockable.timeUnit(), lock);
        if (isLock) {
            try {
                logger.info("RequestLockable point.proceed start");
                return point.proceed();
            } finally {
                try {
                    lock.unlock();
                } catch (IllegalMonitorStateException e) {
                    logger.info("not locked by current thread", e);
                }
            }
        } else {
            logger.error("get lock error,key:{}", requestLockKey);
            // 多线程场景下主线程捕获异常需要注意,不同的调用方式可能会影响异常的抛出
            throw new RuntimeException("get lock error");
        }
    }
    return point.proceed();
}
Also used : RequestLockable(com.jim.framework.annotationlock.annotation.RequestLockable) MethodSignature(org.aspectj.lang.reflect.MethodSignature) Signature(org.aspectj.lang.Signature) MethodSignature(org.aspectj.lang.reflect.MethodSignature) Method(java.lang.reflect.Method) Lock(java.util.concurrent.locks.Lock) Around(org.aspectj.lang.annotation.Around)

Aggregations

RequestLockable (com.jim.framework.annotationlock.annotation.RequestLockable)1 Method (java.lang.reflect.Method)1 Lock (java.util.concurrent.locks.Lock)1 Signature (org.aspectj.lang.Signature)1 Around (org.aspectj.lang.annotation.Around)1 MethodSignature (org.aspectj.lang.reflect.MethodSignature)1