``` class MdcTaskDecorator implements TaskDecorato...
# spring
m
Copy code
class 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();
      }
    };
  }
}
t
Something like this. I had to remove a few lines from our business logic.
Copy code
internal 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)
    }
}
114 Views