fix 完成request-id

master
陈景阳 4 years ago
parent 614d3fc273
commit d6bd37bcba
  1. 27
      src/main/java/org/anyin/gitee/shiro/base/MdcExecutor.java
  2. 18
      src/main/java/org/anyin/gitee/shiro/config/AppConfig.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");
}
});
}
}

@ -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);
}
}

Loading…
Cancel
Save