前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >aop计时,超时输出日志

aop计时,超时输出日志

作者头像
阿超
发布2022-11-22 14:18:23
1.1K0
发布2022-11-22 14:18:23
举报
文章被收录于专栏:快乐阿超

一个正在顺着生活规律挺近的青年,首先应注意,自己的才能和愿望与事业相衡。——培根

代码如下:

代码语言:javascript
复制
package com.ruben.simplestreamquery.aop;

import com.alibaba.ttl.TransmittableThreadLocal;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author VampireAchao
 * @since 2022/11/18 13:01
 */
@Slf4j
@Aspect
@Component
public class LogAop {

    public static final ThreadLocal<Boolean> HAS_FINISHED = TransmittableThreadLocal.withInitial(() -> false);

    @Around("execution (public * com..controller..*(..))")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        final ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        final HttpServletRequest request = requestAttributes.getRequest();

        AtomicReference<Object> captureRef = new AtomicReference<>(TransmittableThreadLocal.Transmitter.capture());
        try {
            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    TransmittableThreadLocal.Transmitter.replay(captureRef.get());
                    if (!HAS_FINISHED.get()) {
                        log.warn("Request timeout, url: {}, method: {}, uri: {}, params: {}",
                                request.getRequestURL(), request.getMethod(), request.getRequestURI(), request.getQueryString());
                        HAS_FINISHED.remove();
                    }
                    // 三十秒后执行
                }
            }, TimeUnit.SECONDS.toMillis(30));
            final Object result = pjp.proceed();
            HAS_FINISHED.set(true);
            captureRef.set(TransmittableThreadLocal.Transmitter.capture());
            return result;
        } finally {
            TransmittableThreadLocal.Transmitter.restore(captureRef.get());
        }
    }

    
}

所用依赖:

代码语言:javascript
复制
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>transmittable-thread-local</artifactId>
    <version>2.14.2</version>
</dependency>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-11-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档