美文网首页
解决线程池ExecutorService里面slf4j打log无

解决线程池ExecutorService里面slf4j打log无

作者: JoneSnow | 来源:发表于2019-03-07 19:37 被阅读0次
    public class XHSThreadPoolExecutor extends ThreadPoolExecutor {
    
        public XHSThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) {
            super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
        }
    
        @Override
        public void execute(Runnable command) {
            Map<String, String> map = MDC.getCopyOfContextMap();
            super.execute(()-> {
                MDC.setContextMap(map);
                command.run();
            });
        }
    }
    

    然后实例化ExecutorService

    @Configuration
    public class ExecutorServiceConfig {
    
        @Bean
        public ExecutorService getExecutorService() {
            ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
                    .setNameFormat("asyncExecutorService-%d").build();
            ExecutorService executorService = new XHSThreadPoolExecutor(200,
                    2000, 60L , TimeUnit.SECONDS, new SynchronousQueue<>(), namedThreadFactory);
            return executorService;
        }
    }
    

    相关文章

      网友评论

          本文标题:解决线程池ExecutorService里面slf4j打log无

          本文链接:https://www.haomeiwen.com/subject/vdbhpqtx.html