From d6bd37bcbac8a5507465cf04fb4696e5f381060f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=99=AF=E9=98=B3?= Date: Sat, 13 Nov 2021 08:50:15 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E5=AE=8C=E6=88=90request-id?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../anyin/gitee/shiro/base/MdcExecutor.java | 27 +++++++++++++++++++ .../anyin/gitee/shiro/config/AppConfig.java | 18 +++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/main/java/org/anyin/gitee/shiro/base/MdcExecutor.java diff --git a/src/main/java/org/anyin/gitee/shiro/base/MdcExecutor.java b/src/main/java/org/anyin/gitee/shiro/base/MdcExecutor.java new file mode 100644 index 0000000..26f4502 --- /dev/null +++ b/src/main/java/org/anyin/gitee/shiro/base/MdcExecutor.java @@ -0,0 +1,27 @@ +package org.anyin.gitee.shiro.base; + +import org.slf4j.MDC; + +import java.util.concurrent.Executor; + +public class MdcExecutor implements Executor { + + private Executor executor; + + public MdcExecutor(Executor executor) { + this.executor = executor; + } + + @Override + public void execute(Runnable command) { + final String requestId = MDC.get("REQUEST_ID"); + executor.execute(() -> { + MDC.put("REQUEST_ID", requestId); + try { + command.run(); + } finally { + MDC.remove("REQUEST_ID"); + } + }); + } +} diff --git a/src/main/java/org/anyin/gitee/shiro/config/AppConfig.java b/src/main/java/org/anyin/gitee/shiro/config/AppConfig.java index fe1b69f..41731b9 100644 --- a/src/main/java/org/anyin/gitee/shiro/config/AppConfig.java +++ b/src/main/java/org/anyin/gitee/shiro/config/AppConfig.java @@ -1,8 +1,13 @@ package org.anyin.gitee.shiro.config; import org.anyin.gitee.shiro.advisor.ApiMessageAdvisor; +import org.anyin.gitee.shiro.base.MdcExecutor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; + +import java.util.concurrent.Executor; +import java.util.concurrent.ThreadPoolExecutor; @Configuration public class AppConfig { @@ -11,4 +16,17 @@ public class AppConfig { public ApiMessageAdvisor apiMessageAdvisor(){ return new ApiMessageAdvisor(); } + + @Bean + public Executor getAsyncExecutor() { + ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); + executor.setCorePoolSize(10); + executor.setMaxPoolSize(100); + executor.setQueueCapacity(200); + executor.setThreadNamePrefix("AsyncExecutorThread-"); + executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); + executor.initialize(); + // 处理request-id打印的问题 + return new MdcExecutor(executor); + } }