Skip to content

Instantly share code, notes, and snippets.

@jei0486
Last active January 10, 2023 02:18
Show Gist options
  • Save jei0486/edc727ccc81819314411507253f1637f to your computer and use it in GitHub Desktop.
Save jei0486/edc727ccc81819314411507253f1637f to your computer and use it in GitHub Desktop.
MDC with webflux
/**
* @link https://www.novatec-gmbh.de/en/blog/how-can-the-mdc-context-be-used-in-the-reactive-spring-applications/
* https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Hooks.html#onEachOperator-java.util.function.Function-
*/
@Configuration
public class MdcContextLifterConfig {
    public static final String MDC_CONTEXT_REACTOR_KEY = MdcContextLifterConfig.class.getName();
    @PostConstruct
    @SuppressWarnings("unchecked")
    public void contextOperatorHook() {
        Hooks.onEachOperator(
                MDC_CONTEXT_REACTOR_KEY,
                Operators.lift((scannable, subscriber) -> new MdcContextLifter(subscriber)));
    }
    @PreDestroy
    public void cleanupHook() {
        Hooks.resetOnEachOperator(MDC_CONTEXT_REACTOR_KEY);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment