package com.elab.redis.interceptor.impl;

import org.redisson.api.RLock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.framework.ReflectiveMethodInvocation;

import java.util.concurrent.TimeUnit;

/**
 * 抽象执行器
 *
 * @author ： liukx
 * @time ： 2020/7/28 - 15:51
 */
public abstract class AbstractProcess {

    private Logger logger = LoggerFactory.getLogger(AbstractProcess.class);

    protected Object processBussiness(ReflectiveMethodInvocation invocation, int timeOut, RLock lock)
            throws Throwable {
        try {
            if (lock.tryLock(timeOut, TimeUnit.SECONDS)) {
                Object proceed = invocation.proceed();
                return proceed;
            }
        } catch (InterruptedException e) {
            logger.warn("尝试获取锁超时", e);
        } finally {
            lock.unlock();
        }
        return null;
    }
}
