Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Propogate MDC context into newly created thread pool
public class ThreadPoolTaskExecutorWithMdcPropagation extends ThreadPoolExecutor {
private static final Logger LOGGER = LoggerFactory.getLogger(ThreadPoolTaskExecutorWithMdcPropagation.class);
private final Map<String, String> parentMdcContextMap;
public ThreadPoolTaskExecutorWithMdcPropagation(int corePoolSize, int maxPoolSize, long keepAliveTime, TimeUnit keepAliveTimeUnit) {
super(corePoolSize, maxPoolSize, keepAliveTime, keepAliveTimeUnit, new LinkedBlockingQueue<>());
parentMdcContextMap = MDC.getCopyOfContextMap();
protected void beforeExecute(Thread workerThread, Runnable runnable) {
try {
if (parentMdcContextMap != null && !parentMdcContextMap.isEmpty()) {
} catch (Exception e) {
LOGGER.error("Exception occurred while passing MDC context to thread.", e);
super.beforeExecute(workerThread, runnable);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.