ARTICLE AD BOX
I'm starting an async-process through a REST request. As soon as the REST requests returns, I'm losing the MDC context inside my async execution code. I tried to "copy" it over with ThreadContext but it does not work. Also, manually copying it over didn't do the trick. Somehow MDC gets cleared after the outer REST request returns.
Map<String, String> copyOfContextMap = MDC.getCopyOfContextMap(); ThreadContext threadContext = ThreadContext.builder() .propagated(ThreadContext.ALL_REMAINING) .cleared() .build(); Runnable runnable = threadContext.contextualRunnable(() -> { try { MDC.setContextMap(copyOfContextMap); latch.countDown(); for (int i = 0; i < 10; i++) { log.info(i + ": Log inside async executor"); sleepQuietly(); } } catch (Throwable e) { log.error("Error during async execution", e); } }); executor.runAsync(runnable);How can I preserve my MDC context from the asynchronous executed thread? I see it's there during the first log lines, when the outer request hasn't returned but afterwards seems to get cleared.
