[undertow-dev] pass MDC to worker thread

Andrea Di Cesare andrea at softinstigate.com
Wed Jun 3 03:22:54 EDT 2020


If a thread switch occurs the MDC context is lost.

You can attach it to the exchange:

public static final AttachmentKey<Map<String, String>> MDC_CONTEXT_KEY =
AttachmentKey.create(Map.class);
....
exchange.putAttachment(MDC_CONTEXT_KEY, MDC.getCopyOfContextMap());
...

and then restore it in your worker thread:

MDC.setContextMap(exchange.getAttachment(MDC_CONTEXT_KEY));

Hope this helps,

 Andrea Di Cesare
 Partner at Soft*Instigate* <https://softinstigate.com/>
 MongoDB Technology Partner
  <https://sched.co/b2Hz>
 Via Copernico 38, 20125 Milano (Italy)
 Mobile: +39 329 737 6417
 Skype: ujibang

 The company behind *REST*Heart <https://restheart.org>, the NoCoding and
NoSQL Platform for MongoDB


Il giorno mar 2 giu 2020 alle ore 14:57 Мартынов Илья <imartynovsp at gmail.com>
ha scritto:

> Hello,
>
> I've created a handler to fill MDC from incoming request header.
> public class CorrelationHandler implements HttpHandler
> {
>     private HttpHandler next;
>     public CorrelationHandler(HttpHandler next) {
>         this.next = next;
>     }
>     @Override
>     public void handleRequest(HttpServerExchange e) throws Exception {
>         String corr = e.getRequestHeaders().getFirst("IDP-CORR-ID");
>         MDC.put("corr", corr);
>         next.handleRequest(e);
>     }
> }
>
> But most of my processing is done in worker thread and mdc is not passed
> threre. How to pass it?
>
> _______________________________________________
> undertow-dev mailing list
> undertow-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/undertow-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/undertow-dev/attachments/20200603/2cef8eb3/attachment.html 


More information about the undertow-dev mailing list