从这个答案https://stackoverflow.com/a/25125159/4367326,我有routingAppender工作,但我想为程序中的每个线程设置ThreadContext。
当我开始
ThreadContext.put("logFileName", "TestLogFile");它适用于主线程和日志,但不适用于我的应用程序中的任何其他线程。我怎样才能做到这一点?
发布于 2016-11-04 13:22:03
如果将系统属性isThreadContextMapInheritable设置为true,则每个子线程都将继承ThreadContext状态。但是这对执行者不起作用,所以您需要手动地将数据从一个线程复制到另一个线程。
Update#2
你可以这样做:
public abstract class ThreadContextRunnable implements Runnable {
private final Map context = ThreadContext.getContext();
@Override
public final void run() {
if (context != null) {
ThreadContext.putAll(context);
}
try {
runWithContext();
} finally {
ThreadContext.clearAll();
}
}
protected abstract void runWithContext();
}然后您只需要实现runWithContext方法。
https://stackoverflow.com/questions/40423667
复制相似问题