Skip to content

Instantly share code, notes, and snippets.

@yanns
Created October 13, 2014 06:50
Show Gist options
  • Save yanns/ac7eb02ac153ab92e2b6 to your computer and use it in GitHub Desktop.
Save yanns/ac7eb02ac153ab92e2b6 to your computer and use it in GitHub Desktop.
MDCPropagatingDispatcher
/**
* A MDC propagating dispatcher.
*
* This dispatcher propagates the MDC current request context if it's set when it's executed.
*/
class MDCPropagatingDispatcher(_configurator: MessageDispatcherConfigurator,
id: String,
throughput: Int,
throughputDeadlineTime: Duration,
executorServiceFactoryProvider: ExecutorServiceFactoryProvider,
shutdownTimeout: FiniteDuration)
extends Dispatcher(_configurator, id, throughput, throughputDeadlineTime, executorServiceFactoryProvider, shutdownTimeout ) with SLF4JLogging {
override def execute(runnable: Runnable): Unit = {
// capture the callee MDC
val mdcContext = MDC.getCopyOfContextMap
val id = mdcContext.get("id")
log.info(s"new id = $id")
super.execute(new Runnable {
def run() = {
// backup the callee MDC context
val oldMDCContext = MDC.getCopyOfContextMap
val oldId = oldMDCContext.get("id")
log.info(s"new oldId = $oldId")
// Run the runnable with the captured context
setContextMap(mdcContext)
try {
runnable.run()
} finally {
// restore the callee MDC context
setContextMap(oldMDCContext)
}
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment