2015-02-23 334 views

回答

1

看看GitHub上的mdedetrich/mdc-async-netty-eventloopgroup项目。基本上,你必须装饰EventLoopGroup.execute

@Override 
public void execute(Runnable runnable) { 
    delegate.execute(new Runnable() { 
     @Override 
     public void run() { 
      Map<String, String> oldMdcContext = MDC.getCopyOfContextMap(); 
      setContextMap(mdcContext); 
      try { 
       runnable.run(); 
      } finally { 
       setContextMap(oldMdcContext); 
      } 

     } 
    }); 
} 

private void setContextMap(Map<String, String> context) { 
    if (context == null) { 
     MDC.clear(); 
    } else { 
     MDC.setContextMap(context); 
    } 
} 
+0

此解决方案不起作用。它还会调用线程调用MDCEventLoopGroup.fromThread()的上下文映射的“快照”,并丢失跟踪其所有未来更改,并将此快照传递给所有可运行的程序。即使在多线程多核心环境下,它仍然不起作用。我试图修改它,我试图重写提交(...)和计划...(...)方法,并重写newChild(...)方法,它创建一个覆盖exec(...)的子... ),提交(...)和计划...(...)方法没有成功 - 上下文仍然丢失。 – Uniqus 2017-06-02 22:02:54