blob: d1dc3ae8f7772bfa8c5807106932e49a74a66309 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
package xyz.driver.pdsuicommon.concurrent
import java.util.concurrent.ThreadFactory
import org.slf4j.MDC
object MdcThreadFactory {
def from(orig: ThreadFactory): ThreadFactory = new MdcThreadFactory(orig)
}
class MdcThreadFactory(orig: ThreadFactory) extends ThreadFactory {
override def newThread(runnable: Runnable): Thread = {
val parentMdcContext = MDC.getCopyOfContextMap
orig.newThread(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)
}
|