2024-05-20 10:01:23 +08:00
|
|
|
|
package com.zhgd.xmgl.config;
|
|
|
|
|
|
|
2024-06-01 14:37:49 +08:00
|
|
|
|
import com.zhgd.xmgl.constant.Cts;
|
2024-06-06 21:13:17 +08:00
|
|
|
|
import com.zhgd.xmgl.util.LogMdcUtil;
|
2024-06-01 14:37:49 +08:00
|
|
|
|
import com.zhgd.xmgl.util.ThreadLocalUtil;
|
2024-05-20 10:01:23 +08:00
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
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.MDC;
|
2024-06-01 14:37:49 +08:00
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
2024-05-20 10:01:23 +08:00
|
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
@Aspect
|
|
|
|
|
|
@Component
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
|
public class TaskAspect {
|
2024-06-01 14:37:49 +08:00
|
|
|
|
public static Integer onlyPrintInterface;
|
|
|
|
|
|
|
|
|
|
|
|
@Value("${onlyPrintInterface:0}")
|
|
|
|
|
|
public void setOnlyPrintInterface(Integer onlyPrintInterface) {
|
|
|
|
|
|
TaskAspect.onlyPrintInterface = onlyPrintInterface;
|
|
|
|
|
|
}
|
2024-05-20 10:01:23 +08:00
|
|
|
|
|
|
|
|
|
|
@Pointcut("@annotation(org.springframework.scheduling.annotation.Scheduled)")
|
|
|
|
|
|
public void servicePointcut() {
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Around("servicePointcut()")
|
|
|
|
|
|
public void doAround(ProceedingJoinPoint jointPoint) throws Throwable {
|
|
|
|
|
|
Map<String, String> context = MDC.getCopyOfContextMap(); //复制主线程MDC
|
|
|
|
|
|
// 获取当前访问的class类及类名
|
|
|
|
|
|
Class<?> clazz = jointPoint.getTarget().getClass();
|
|
|
|
|
|
String clazzName = jointPoint.getTarget().getClass().getName();
|
|
|
|
|
|
|
|
|
|
|
|
// 获取访问的方法名
|
|
|
|
|
|
String methodName = jointPoint.getSignature().getName();
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
|
|
if (null == context) {
|
2024-06-06 21:13:17 +08:00
|
|
|
|
LogMdcUtil.setRequestId("-t"); //主线程没有MDC就自己生成一个
|
2024-06-02 01:17:46 +08:00
|
|
|
|
ThreadLocalUtil.addInKey(Cts.TL_IS_FROM_TASK, true);
|
2024-06-01 14:37:49 +08:00
|
|
|
|
log.info("task方法开始:{}", methodName);
|
2024-05-20 10:01:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取方法所有参数及其类型
|
|
|
|
|
|
Object[] args = jointPoint.getArgs();
|
|
|
|
|
|
Class[] argClz = ((MethodSignature) jointPoint.getSignature()).getParameterTypes();
|
|
|
|
|
|
// 获取访问的方法对象
|
|
|
|
|
|
Method method = clazz.getDeclaredMethod(methodName, argClz);
|
|
|
|
|
|
|
|
|
|
|
|
// 判断当前访问的方法是否存在指定注解
|
|
|
|
|
|
if (method.isAnnotationPresent(Scheduled.class)) {
|
|
|
|
|
|
Scheduled annotation = method.getAnnotation(Scheduled.class);
|
|
|
|
|
|
|
|
|
|
|
|
// 获取注解标识值与注解描述
|
2024-06-01 14:37:49 +08:00
|
|
|
|
// String value = annotation.value();
|
|
|
|
|
|
// String desc = annotation.description();
|
2024-05-20 10:01:23 +08:00
|
|
|
|
|
|
|
|
|
|
// 执行目标方法
|
|
|
|
|
|
Object proceed = jointPoint.proceed();
|
|
|
|
|
|
}
|
2024-06-01 14:56:19 +08:00
|
|
|
|
} catch (Exception e) {
|
2024-06-06 21:13:17 +08:00
|
|
|
|
if (LogMdcUtil.isPrint()) {
|
2024-06-01 14:56:19 +08:00
|
|
|
|
throw e;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
log.error("", e);
|
|
|
|
|
|
}
|
2024-05-20 10:01:23 +08:00
|
|
|
|
} finally {
|
|
|
|
|
|
if (null == context) {
|
2024-06-01 14:37:49 +08:00
|
|
|
|
log.info("task方法结束:{}", methodName);
|
2024-06-06 21:13:17 +08:00
|
|
|
|
LogMdcUtil.clear();
|
2024-06-01 14:37:49 +08:00
|
|
|
|
ThreadLocalUtil.remove();
|
2024-05-20 10:01:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|