Created
October 13, 2014 06:50
-
-
Save yanns/ac7eb02ac153ab92e2b6 to your computer and use it in GitHub Desktop.
MDCPropagatingDispatcher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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