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

com.gitee.fufu669.aspect.MockExecuteLaterAop Maven / Gradle / Ivy

There is a newer version: 6.666.66021
Show newest version
package com.gitee.fufu669.aspect;

import com.gitee.fufu669.common.CacheKeyCommon;
import com.gitee.fufu669.service.CacheService;
import com.gitee.fufu669.utils.CacheAopUtil;
import com.gitee.fufu669.utils.CacheHtmlUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;


@Lazy
@Aspect
@Component
/** @author wangfupeng */
public class MockExecuteLaterAop {

    public static final Logger logger = LoggerFactory.getLogger(MockExecuteLaterAop.class);

    @Autowired
    @Qualifier(value = "threadPoolTaskExecutorCacheFetchLater")
    ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @Autowired
    CacheService cacheService;

    @Around("@annotation(com.gitee.fufu669.aspect.MockExecuteLater)")
    public Object aroundMethod(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        int secondsBetweenExecutions = CacheKeyCommon.MOCKEXECUTELATER_DEFAULT_SECONDS_BETWEEN_EXECUTIONS;
        String methodName = null;
        String description = "";
        try {
            String targetName = proceedingJoinPoint.getTarget().getClass().getName();
            methodName = proceedingJoinPoint.getSignature().getName();
            Object[] arguments = proceedingJoinPoint.getArgs();
            Class targetClass = Class.forName(targetName);
            Method[] methods = targetClass.getMethods();
            for (Method method : methods) {
                if (method.getName().equals(methodName)) {
                    secondsBetweenExecutions = method.getAnnotation(MockExecuteLater.class).secondsBetweenExecutions();
                    description = method.getAnnotation(MockExecuteLater.class).description();
                    break;
                }
            }
        } catch (Exception e) {
            logger.info(e.toString());
        }
        String mockExecuteLaterCacheKey = "mockexecutelater:" + CacheAopUtil.getCacheKey(proceedingJoinPoint);
        if (CacheAopUtil.isRefreshFlag(secondsBetweenExecutions, mockExecuteLaterCacheKey, cacheService) == true) {
            CustomThreadToRunAndSaveToRedis customThreadToRunAndSaveToRedis = new CustomThreadToRunAndSaveToRedis();
            customThreadToRunAndSaveToRedis.setProceedingJoinPoint(proceedingJoinPoint);
            customThreadToRunAndSaveToRedis.setDescription(description);
            threadPoolTaskExecutor.execute(customThreadToRunAndSaveToRedis);
        }
        return null;
    }

    public class CustomThreadToRunAndSaveToRedis implements Runnable {
        private ProceedingJoinPoint proceedingJoinPoint;
        private String description = "";

        public void setProceedingJoinPoint(ProceedingJoinPoint proceedingJoinPoint) {
            this.proceedingJoinPoint = proceedingJoinPoint;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public void run() {
            String targetName = proceedingJoinPoint.getTarget().getClass().getName();
            String methodName = proceedingJoinPoint.getSignature().getName();
            logger.info("%%%%%%MOCKEXECUTELATERAop:multiThread start:Classname:" + CacheHtmlUtil.getLastStringSplitHtml(targetName, "\\.") + ":methodName:" + methodName + ":description:" + description + "%%%%%%");
            Object value = null;
            try {
                value = proceedingJoinPoint.proceed();
            } catch (Throwable throwable) {
                logger.info(throwable.getMessage(), throwable);
            } finally {
                logger.info("%%%%%%MOCKEXECUTELATERAop:multiThread end:Classname:" + CacheHtmlUtil.getLastStringSplitHtml(targetName, "\\.") + ":methodName:" + methodName + ":description:" + description + "%%%%%%");
            }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy