Marcel Overdijk
10/31/2019, 10:34 PMclass MdcTaskDecorator implements TaskDecorator {
@Override
public Runnable decorate(Runnable runnable) {
// Right now: Web thread context !
// (Grab the current thread MDC data)
Map<String, String> contextMap = MDC.getCopyOfContextMap();
return () -> {
try {
// Right now: @Async thread context !
// (Restore the Web thread context's MDC data)
MDC.setContextMap(contextMap);
runnable.run();
} finally {
MDC.clear();
}
};
}
}
Thiago Nerys
10/31/2019, 10:36 PMinternal class MDCRunnable(val parentContext: Map<String, String>, private val runnable: Runnable) : Runnable {
override fun run() {
try {
MDC.setContextMap(parentContext)
runnable.run()
} catch (ex: Exception) {
throw ex
} finally {
MDC.clear()
}
}
}
internal class MdcTaskDecorator : TaskDecorator {
override fun decorate(runnable: Runnable): Runnable {
val contextMap = MDC.getCopyOfContextMap()
return MDCRunnable(contextMap, runnable)
}
}