新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
Java異步線程池中復(fù)制參數(shù)MDC-創(chuàng)新互聯(lián)
最近在查詢(xún)?nèi)罩镜臅r(shí)候,發(fā)現(xiàn)異步線程池里面執(zhí)行的任務(wù) 沒(méi)有打印公司自定義的 日志id
排查發(fā)現(xiàn)日志id是存在MDC中,創(chuàng)建新的線程的時(shí)候并未傳遞給子線程
查詢(xún)文章發(fā)現(xiàn)spring可以使用線程裝飾器解決,如下
@EnableAsync(proxyTargetClass = true)
@SpringBootApplication
public class Application extends AsyncConfigurerSupport { @Override
public Executor getAsyncExecutor() {ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setTaskDecorator(new MdcTaskDecorator()); //定義裝飾器
executor.initialize();
return executor;
}
public static void main(String[] args) {SpringApplication.run(Application.class, args);
}
}
class MdcTaskDecorator implements TaskDecorator { @Override
public Runnable decorate(Runnable runnable) {// Right now: Web thread context !
// (Grab the current thread MDC data)
MapcontextMap = MDC.getCopyOfContextMap();
return () ->{ try {// Right now: @Async thread context !
// (Restore the Web thread context's MDC data)
MDC.setContextMap(contextMap);
runnable.run();
} finally {MDC.clear();
}
};
}
}
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧
新聞標(biāo)題:Java異步線程池中復(fù)制參數(shù)MDC-創(chuàng)新互聯(lián)
分享路徑:http://fisionsoft.com.cn/article/csesjc.html