java MDC_java – 如何使用线程池的MDC?

是的,这是我遇到的常见问题。有几个解决方法(如手动设置它,如描述),但理想情况下,你想要一个解决方案

>一致地设置MDC;

>避免默认的错误,MDC是不正确的,但你不知道它;和

>最小化对如何使用线程池的更改(例如,将Callable与MyCallable无处不在,或类似的丑陋)。

这里有一个我使用的解决方案,满足这三个需求。代码应该是不言自明的。

(注意,这个执行器可以被创建并馈送到Guava的MoreExecutors.listeningDecorator(),if

你使用Guava的ListanableFuture。)

import org.slf4j.MDC;

import java.util.Map;

import java.util.concurrent.*;

/**

* A SLF4J MDC-compatible {@link ThreadPoolExecutor}.

*

* In general, MDC is used to store diagnostic information (e.g. a user's session id) in per-thread variables, to facilitate

* logging. However, although MDC data is passed to thread children, this doesn't work when threads are reused in a

* thread pool. This is a drop-in replacement for {@link ThreadPoolExecutor} sets MDC data before each task appropriately.

*

* Created by jlevy.

* Date: 6/14/13

*/

public class MdcThreadPoolExecutor extends ThreadPoolExecutor {

final private boolean useFixedContext;

final private Map fixedContext;

/**

* Pool where task threads take MDC from the submitting thread.

*/

public static MdcThreadPoolExecutor newWithInheritedMdc(int corePoolSize, int maximumPoolSize, long keepAliveTime,

TimeUnit unit, BlockingQueue workQueue) {

return new MdcThreadPoolExecutor(null, corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);

}

/**

* Pool where task threads take fixed MDC from the thread that creates the pool.

*/

@SuppressWarnings("unchecked")

public static MdcThreadPoolExecutor newWithCurrentMdc(int corePoolSize, int maximumPoolSize, long keepAliveTime,

TimeUnit unit, BlockingQueue workQueue) {

return new MdcThreadPoolExecutor(MDC.getCopyOfContextMap(), corePoolSize, maximumPoolSize, keepAliveTime, unit,

workQueue);

}

/**

* Pool where task threads always have a specified, fixed MDC.

*/

public static MdcThreadPoolExecutor newWithFixedMdc(Map fixedContext, int corePoolSize,

int maximumPoolSize, long keepAliveTime, TimeUnit unit,

BlockingQueue workQueue) {

return new MdcThreadPoolExecutor(fixedContext, corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);

}

private MdcThreadPoolExecutor(Map fixedContext, int corePoolSize, int maximumPoolSize,

long keepAliveTime, TimeUnit unit, BlockingQueue workQueue) {

super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);

this.fixedContext = fixedContext;

useFixedContext = (fixedContext != null);

}

@SuppressWarnings("unchecked")

private Map getContextForTask() {

return useFixedContext ? fixedContext : MDC.getCopyOfContextMap();

}

/**

* All executions will have MDC injected. {@code ThreadPoolExecutor}'s submission methods ({@code submit()} etc.)

* all delegate to this.

*/

@Override

public void execute(Runnable command) {

super.execute(wrap(command, getContextForTask()));

}

public static Runnable wrap(final Runnable runnable, final Map context) {

return new Runnable() {

@Override

public void run() {

Map previous = MDC.getCopyOfContextMap();

if (context == null) {

MDC.clear();

} else {

MDC.setContextMap(context);

}

try {

runnable.run();

} finally {

if (previous == null) {

MDC.clear();

} else {

MDC.setContextMap(previous);

}

}

}

};

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值