All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.snower.jaslock.spring.boot.aspects.LockAspect Maven / Gradle / Ivy

Go to download

High-performance distributed sync service and atomic DB. Provides good multi-core support through lock queues, high-performance asynchronous binary network protocols. Can be used for spikes, synchronization, event notification, concurrency control. https://github.com/snower/slock

The newest version!
package io.github.snower.jaslock.spring.boot.aspects;

import io.github.snower.jaslock.Lock;
import io.github.snower.jaslock.exceptions.LockTimeoutException;
import io.github.snower.jaslock.exceptions.SlockException;
import io.github.snower.jaslock.spring.boot.AbstractBaseAspect;
import io.github.snower.jaslock.spring.boot.SlockTemplate;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

@Aspect
@Order(Ordered.HIGHEST_PRECEDENCE + 10000)
public class LockAspect extends AbstractBaseAspect {
    private static final Logger logger = LoggerFactory.getLogger(LockAspect.class);

    public LockAspect(SlockTemplate slockTemplate) {
        super(slockTemplate);
    }

    @Pointcut("@annotation(io.github.snower.jaslock.spring.boot.annotations.Lock)")
    private void Lock() {}

    @Around("Lock()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodInvocationProceedingJoinPoint methodJoinPoint = (MethodInvocationProceedingJoinPoint) joinPoint;
        MethodSignature methodSignature = (MethodSignature) methodJoinPoint.getSignature();
        io.github.snower.jaslock.spring.boot.annotations.Lock lockAnnotation = methodSignature.getMethod().getAnnotation(io.github.snower.jaslock.spring.boot.annotations.Lock.class);
        if (lockAnnotation == null) {
            throw new IllegalArgumentException("unknown Lock annotation");
        }

        String templateKey = lockAnnotation.value();
        if (isBlank(templateKey)) {
            templateKey = lockAnnotation.key();
            if (isBlank(templateKey)) {
                throw new IllegalArgumentException("key is empty");
            }
        }
        String key = evaluateKey(templateKey, methodSignature.getMethod(), methodJoinPoint.getArgs(), methodJoinPoint.getThis());
        if (isBlank(key)) {
            return joinPoint.proceed();
        }
        Lock lock = lockAnnotation.databaseId() >= 0 && lockAnnotation.databaseId() < 127 ?
                slockTemplate.selectDatabase(lockAnnotation.databaseId())
                        .newLock(key, lockAnnotation.timeout(), lockAnnotation.expried()) :
                slockTemplate.newLock(key, lockAnnotation.timeout(), lockAnnotation.expried());
        try {
            lock.acquire();
            try {
                return joinPoint.proceed();
            } finally {
                try {
                    lock.release();
                } catch (Exception e) {
                    logger.warn("Slock LockAspect release {} error {}", templateKey, e, e);
                }
            }
        } catch (LockTimeoutException e) {
            if (!lockAnnotation.timeoutException().isInstance(e)) {
                throw lockAnnotation.timeoutException().getConstructor(String.class, Throwable.class)
                        .newInstance(e.getMessage(), e);
            } else {
                throw e;
            }
        } catch (SlockException e) {
            if (!lockAnnotation.exception().isInstance(e)) {
                throw lockAnnotation.exception().getConstructor(String.class, Throwable.class)
                        .newInstance(e.getMessage(), e);
            } else {
                throw e;
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy