blob: cd2b394933783fc073a74b85ee203812733d93f0 (
plain) (
tree)
|
|
package xyz.driver.common.concurrent
import org.slf4j.MDC
import scala.concurrent.{ExecutionContext, ExecutionContextExecutor}
object MdcExecutionContext {
def from(orig: ExecutionContext): ExecutionContext = new MdcExecutionContext(orig)
}
class MdcExecutionContext(orig: ExecutionContext) extends ExecutionContextExecutor {
def execute(runnable: Runnable): Unit = {
val parentMdcContext = MDC.getCopyOfContextMap
orig.execute(new Runnable {
def run(): Unit = {
val saveMdcContext = MDC.getCopyOfContextMap
setContextMap(parentMdcContext)
try {
runnable.run()
} finally {
setContextMap(saveMdcContext)
}
}
})
}
private[this] def setContextMap(context: java.util.Map[String, String]): Unit =
Option(context).fold(MDC.clear())(MDC.setContextMap)
def reportFailure(t: Throwable): Unit = orig.reportFailure(t)
}
|