最近在查詢?nèi)罩镜臅r(shí)候,發(fā)現(xiàn)異步線程池里面執(zhí)行的任務(wù) 沒(méi)有打印公司自定義的 日志id
排查發(fā)現(xiàn)日志id是存在MDC中,創(chuàng)建新的線程的時(shí)候并未傳遞給子線程
查詢文章發(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)查看詳情吧
當(dāng)前題目:Java異步線程池中復(fù)制參數(shù)MDC-創(chuàng)新互聯(lián)
分享路徑:http://vcdvsql.cn/article22/csesjc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、企業(yè)建站、ChatGPT、定制開(kāi)發(fā)、移動(dòng)網(wǎng)站建設(shè)、軟件開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容