add 新增 ThreadLocalHolder 替代 SaHolder 支持异步

add 新增 ThreadLocalHolder 替代 SaHolder 支持异步
This commit is contained in:
疯狂的狮子Li
2023-12-29 13:14:16 +08:00
parent 77cfb9137e
commit c68eb5701b
10 changed files with 110 additions and 51 deletions

View File

@@ -4,7 +4,6 @@ import cn.hutool.core.lang.Dict;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.ttl.TransmittableThreadLocal;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
@@ -14,6 +13,7 @@ import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.dromara.common.core.context.ThreadLocalHolder;
import org.dromara.common.core.utils.ServletUtils;
import org.dromara.common.core.utils.SpringUtils;
import org.dromara.common.core.utils.StringUtils;
@@ -49,9 +49,9 @@ public class LogAspect {
/**
* 计算操作消耗时间
* 计时 key
*/
private static final ThreadLocal<StopWatch> TIME_THREADLOCAL = new TransmittableThreadLocal<>();
private static final String LOG_STOP_WATCH_KEY = "logStopwatch";
/**
* 处理请求前执行
@@ -59,7 +59,7 @@ public class LogAspect {
@Before(value = "@annotation(controllerLog)")
public void boBefore(JoinPoint joinPoint, Log controllerLog) {
StopWatch stopWatch = new StopWatch();
TIME_THREADLOCAL.set(stopWatch);
ThreadLocalHolder.set(LOG_STOP_WATCH_KEY, stopWatch);
stopWatch.start();
}
@@ -112,7 +112,7 @@ public class LogAspect {
// 处理设置注解上的参数
getControllerMethodDescription(joinPoint, controllerLog, operLog, jsonResult);
// 设置消耗时间
StopWatch stopWatch = TIME_THREADLOCAL.get();
StopWatch stopWatch = ThreadLocalHolder.get(LOG_STOP_WATCH_KEY);
stopWatch.stop();
operLog.setCostTime(stopWatch.getTime());
// 发布事件保存数据库
@@ -122,7 +122,7 @@ public class LogAspect {
log.error("异常信息:{}", exp.getMessage());
exp.printStackTrace();
} finally {
TIME_THREADLOCAL.remove();
ThreadLocalHolder.remove(LOG_STOP_WATCH_KEY);
}
}
@@ -161,7 +161,7 @@ public class LogAspect {
Map<String, String> paramsMap = ServletUtils.getParamMap(ServletUtils.getRequest());
String requestMethod = operLog.getRequestMethod();
if (MapUtil.isEmpty(paramsMap)
&& HttpMethod.PUT.name().equals(requestMethod) || HttpMethod.POST.name().equals(requestMethod)) {
&& HttpMethod.PUT.name().equals(requestMethod) || HttpMethod.POST.name().equals(requestMethod)) {
String params = argsArrayToString(joinPoint.getArgs(), excludeParamNames);
operLog.setOperParam(StringUtils.substring(params, 0, 2000));
} else {