地质所 沉降监测网建设项目
zmk
2024-05-15 9e3afc6d0fa514f986d3fea40fa23124e6fb5070
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.javaweb.framework.aspectj;
 
 
import com.javaweb.common.utils.TimeUuidUtil;
import com.javaweb.framework.manager.AsyncManager;
import com.javaweb.framework.manager.factory.AsyncFactory;
import com.javaweb.system.domain.CostTime;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
 
/**
 * 计算统计服务器响应时间
 * @author lws
 *
 */
@Aspect
@Component
public class CountResponseTimeAspect {
 
    public static final int CONDITION_TIME = 500;
    ThreadLocal<Long> startTime = new ThreadLocal<>();
    String className = null;
    String methodName = null;
 
    // 配置织入点
    //第一个*代表任意返回值;controller..代表controller包下及子包下;第二个*代表所有类;*(..)代表任意参数的所有方法
    @Pointcut("execution(* com.javaweb..controller..*.*(..))")
    public void webLog(){}
    @Before("webLog()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
        startTime.set(System.currentTimeMillis());
        className = joinPoint.getTarget().getClass().getName();
        methodName = joinPoint.getSignature().getName()+"()";
    }
    @AfterReturning(returning = "ret", pointcut = "webLog()")
    public void doAfterReturning(Object ret) throws Throwable {
        // 处理完请求,返回内容
        long spendTime = System.currentTimeMillis() - startTime.get();
        if(spendTime>CONDITION_TIME){
            CostTime costTime = new CostTime();
            costTime.setId(TimeUuidUtil.get16UUID());
            costTime.setClassName(className);
            costTime.setMethodName(methodName);
            costTime.setSpendTime(spendTime);
            AsyncManager.me().execute(AsyncFactory.recordCostTime(costTime));
        }
    }
}