Logback (9) | MDC And Managed Threads

在使用如 `java.util.concurrent.Executors` 的线程管理时,MDC(Mapped Diagnostic Context)的内容不能总是被工作线程继承。推荐在主线程中调用 `MDC.getCopyOfContextMap()`,然后在任务运行时通过 `MDC.setContextMapValues()` 将 MDC 值与新的 Executor 管理线程关联。MDCInsertingServletFilter 是一种在 Web 应用中将 HTTP 请求的主机名、请求 URI 和用户代理插入 MDC 的方式,以便在日志中获取这些信息。安装该过滤器需要在 `web.xml` 文件中配置,并确保其在其他过滤器之前声明,以确保 MDC 值能被后续过滤器看到。

MDC And Managed Threads
A copy of the mapped diagnostic context can not always be inherited by worker threads from the initiating thread. This is the case when java.util.concurrent.Executors is used for thread management. For instance, newCachedThreadPool method creates a ThreadPoolExecutor and like other thread pooling code, it has intricate thread creation logic.

In such cases, it is recommended that MDC.getCopyOfContextMap() is invoked on the original (master) thread before submitting a task to the executor. When the task runs, as its first action, it should invoke MDC.setContextMapValues() to associate the stored copy of the original MDC values with the new Executor managed thread.

MDCInsertingServletFilter
Within web applications, it often proves helpful t

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Web3&Basketball

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值