Skip to content

Commit 0b1b181

Browse files
committed
feat: 비동기 스레드에 MDC 데이터 적재
- 비동기 스레드에 요청 스레드의 MDC 데이터를 적재하여 로그 추적이 안되는 문제 해결 - 데코레이터 디자인 패턴 적용
1 parent 474d7da commit 0b1b181

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

src/main/java/site/praytogether/pray_together/config/AsyncConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public Executor notificationExecutor() {
1717
executor.setQueueCapacity(100);
1818
executor.setMaxPoolSize(10);
1919
executor.setThreadNamePrefix("notification-");
20+
executor.setTaskDecorator(new MdcTaskDecorator());
2021
executor.setWaitForTasksToCompleteOnShutdown(true);
2122
executor.setAwaitTerminationSeconds(30);
2223
executor.initialize();
@@ -30,6 +31,7 @@ public Executor emailExecutor() {
3031
executor.setQueueCapacity(50);
3132
executor.setMaxPoolSize(10);
3233
executor.setThreadNamePrefix("email-");
34+
executor.setTaskDecorator(new MdcTaskDecorator());
3335
executor.setWaitForTasksToCompleteOnShutdown(true);
3436
executor.setAwaitTerminationSeconds(60);
3537
executor.initialize();
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package site.praytogether.pray_together.config;
2+
3+
import java.util.Map;
4+
import org.slf4j.MDC;
5+
import org.springframework.core.task.TaskDecorator;
6+
import org.springframework.lang.NonNull;
7+
8+
public class MdcTaskDecorator implements TaskDecorator {
9+
10+
@Override
11+
@NonNull
12+
public Runnable decorate(@NonNull Runnable runnable) {
13+
Map<String, String> contextMap = MDC.getCopyOfContextMap();
14+
return () -> {
15+
if (contextMap != null) {
16+
MDC.setContextMap(contextMap);
17+
}
18+
try {
19+
runnable.run();
20+
} finally {
21+
MDC.clear();
22+
}
23+
};
24+
}
25+
}

0 commit comments

Comments
 (0)