@Bean("logAuditThreadExecutor")
Executor logAuditThreadExecutor() {
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
threadPoolTaskExecutor.setCorePoolSize(Runtime.getRuntime().availableProcessors());
threadPoolTaskExecutor.setMaxPoolSize(100);
threadPoolTaskExecutor.setKeepAliveSeconds(100);
threadPoolTaskExecutor.setQueueCapacity(1024);
threadPoolTaskExecutor.setRejectedExecutionHandler(new LogCallRunnerReject("logAuditThreadExecutor"));
threadPoolTaskExecutor.setTaskDecorator(new MDCTaskDecorator());
threadPoolTaskExecutor.setThreadNamePrefix("logAuditThread-");
threadPoolTaskExecutor.initialize();
return threadPoolTaskExecutor;
}
import org.slf4j.MDC;
import org.springframework.core.task.TaskDecorator;
import java.util.Map;
/**
* @author lijian@jovision.com
* @date 2020/8/15
**/
public class MDCTaskDecorator implements TaskDecorator {
@Override
public Runnable decorate(Runnable runnable) {
Map<String, String> copyOfContextMap = MDC.getCopyOfContextMap();
return () -> {
try {
if (copyOfContextMap != null) {
MDC.setContextMap(copyOfContextMap);
}
runnable.run();
} finally {
MDC.clear();
}
};
}
}