Skip to content

Commit

Permalink
#338 [feat] 타 스레드에 mdc 문맥 전파
Browse files Browse the repository at this point in the history
  • Loading branch information
KWY0218 committed Jan 19, 2025
1 parent edd69ad commit 617c66d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.asap.server.presentation.common.log;

import java.util.Map;
import org.slf4j.MDC;
import org.springframework.core.task.TaskDecorator;
import org.springframework.stereotype.Component;

@Component
public class MDCContextTaskDecorator implements TaskDecorator {
@Override
public Runnable decorate(Runnable runnable) {
final Map<String, String> mdc = MDC.getCopyOfContextMap();
return () -> {
try {
if (mdc != null) {
MDC.setContextMap(mdc);
}
runnable.run();
} finally {
if (mdc != null) {
mdc.clear();
}
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
import java.util.concurrent.Executor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskDecorator;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@Configuration
@EnableAsync
public class AsyncConfig {
@Bean
public Executor threadPoolTaskExecutor() {
public Executor threadPoolTaskExecutor(TaskDecorator taskDecorator) {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(1);
taskExecutor.setQueueCapacity(10);
taskExecutor.setMaxPoolSize(3);
taskExecutor.setTaskDecorator(taskDecorator);
return taskExecutor;
}
}

0 comments on commit 617c66d

Please sign in to comment.