csongyu/mdc-with-thread-pools
logging.pattern.console
1
| logging.pattern.console=%d{yyyy-MMM-dd HH:mm:ss.SSS} %-5level [%thread] %X{traceNumber} %logger{15} - %msg%n
|
ThreadPoolTaskExecutor
1 2 3 4 5 6 7 8 9 10 11 12 13
| final ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor(); threadPoolTaskExecutor.setTaskDecorator(runnable -> { final Map<String, String> copyOfContextMap = MDC.getCopyOfContextMap(); return () -> { try { MDC.setContextMap(copyOfContextMap); runnable.run(); } finally { MDC.clear(); } }; }); threadPoolTaskExecutor.initialize();
|
ThreadPoolExecutor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(Integer.MAX_VALUE)) { @Override public void execute(final Runnable command) { final Map<String, String> copyOfContextMap = MDC.getCopyOfContextMap(); super.execute(() -> { try { MDC.setContextMap(copyOfContextMap); command.run(); } finally { MDC.clear(); } }); } };
|