Skip to content

Instantly share code, notes, and snippets.

@wreulicke
Last active August 29, 2018 08:49
Show Gist options
  • Save wreulicke/09f904dc338ac82edacc2836b9b9dfca to your computer and use it in GitHub Desktop.
Save wreulicke/09f904dc338ac82edacc2836b9b9dfca to your computer and use it in GitHub Desktop.
RxJava2でMDCのcontextを引き回すコード

RxJava2でMDCのcontextを引き回すコード

RxJavaPlugins.setScheduleHandler(runnable -> {
	Map<String, String> context = MDC.getCopyOfContextMap();
	return () -> {
		Map<String, String> beforeContext = MDC.getCopyOfContextMap();
		if (context == null) {
			MDC.clear();
		} else {
			MDC.setContextMap(context);
		}

		try {
			runnable.run();
		} finally {
			if (beforeContext == null) {
				MDC.clear();
			} else {
				MDC.setContextMap(beforeContext);
			}
		}
	};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment