문서의 이전 판입니다!
@EnableAsync public class SpringAsyncConfig extends AsyncConfigurerSupport { // 기본 taskExecutor @Bean // @Bean 필수 @Override public Executor getAsyncExecutor() { return new ThreadPoolTaskExecutor(); // 객체 설정해줄것. } }
waitForTasksToCompleteOnShutdown=true
awaitTerminationSeconds=초
beanName
을 지정해주면 로그에 이름이 찍힘.ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor(); threadPoolTaskExecutor.setThreadNamePrefix("XXXX-"); threadPoolTaskExecutor.setCorePoolSize(10); threadPoolTaskExecutor.setMaxPoolSize(Integer.MAX_VALUE); threadPoolTaskExecutor.setQueueCapacity(0); threadPoolTaskExecutor.setKeepAliveSeconds(60); threadPoolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true); threadPoolTaskExecutor.setAwaitTerminationSeconds(15);
@Async
로 새로 생성된 쓰레드에서 호출자 쓰레드의 Slf4j MDC를 복제하도록 할 수 있다.@Override public Executor getAsyncExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setTaskDecorator(new MdcTaskDecorator()); executor.initialize(); return executor; }
class MdcTaskDecorator implements TaskDecorator { @Override public Runnable decorate(Runnable runnable) { // Right now: Web thread context ! // (Grab the current thread MDC data) Map<String, String> contextMap = MDC.getCopyOfContextMap(); return () -> { try { // Right now: @Async thread context ! // (Restore the Web thread context's MDC data) MDC.setContextMap(contextMap); runnable.run(); } finally { MDC.clear(); } }; } }