如何在线程池中传递Slf4J MDC内容

本文介绍了一种在Spring框架中实现线程池的方法,通过继承ThreadPoolTaskExecutor并覆盖其execute和submit方法,实现在任务执行前和执行后对MDC(Mapped Diagnostic Context)的上下文进行操作,确保了在多线程环境中日志记录的一致性和可追溯性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面给出在spring中如何实现
线程池继承ThreadPoolTaskExecutor

public class MdcThreadPoolTaskExecutor extends ThreadPoolTaskExecutor {
    
        private static final long serialVersionUID = 1L;
        private boolean useFixedContext = false;
        private Map<String, String> fixedContext;
    
        MdcThreadPoolTaskExecutor() {
            super();
        }
    
        public MdcThreadPoolTaskExecutor(Map<String, String> fixedContext) {
            super();
            this.fixedContext = fixedContext;
            useFixedContext = (fixedContext != null);
        }
    
        private Map<String, String> 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(wrapExecute(command, getContextForTask()));
        }
    
        @Override
        public <T> Future<T> submit(Callable<T> task) {
            return super.submit(wrapSubmit(task, getContextForTask()));
        }
    
        private <T> Callable<T> wrapSubmit(Callable<T> task, final Map<String, String> context) {
            return () -> {
                Map<String, String> previous = MDC.getCopyOfContextMap();
                if (context == null) {
                    MDC.clear();
                } else {
                    MDC.setContextMap(context);
                }
                try {
                    return task.call();
                } finally {
                    if (previous == null) {
                        MDC.clear();
                    } else {
                        MDC.setContextMap(previous);
                    }
                }
            };
        }
    
        private Runnable wrapExecute(final Runnable runnable, final Map<String, String> context) {
            return () -> {
                Map<String, String> 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);
                    }
                }
            };
        }
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值