MDC Context getting copied to only one thread of ForkJoinPool

风格不统一 提交于 2019-12-11 02:50:48

问题


I want to pass MDC context to threads of the ForkJoinPool as I need to print the requestId populated in the MDCContextMap in all the spawned threads logs for better debugging. I am using the following solution for this: How to use MDC with ForkJoinPool?.

I am using the pool as:

final ForkJoinPool mdcPool = new MdcForkJoinPool(30);
mdcPool.submit(() -> ruleset.getOperationList().parallelStream().forEach(operation -> {
                log.info("Sample log line");

But from the logs it looks like MDC Context is getting copied to only one thread of the pool. I have also put some logs in the submit method of the MdcForkJoinPool and also in the beforeExecution method. Following are the logs where the requestID is getting printed only in one thread ("ForkJoinPool-1-worker-25") logs:

16 Oct 2018 15:05:32,570 [INFO] eb73e823-d154-11e8-871a-d138501061d6 (Coral Endpoint : 1) com.amazon.service.utils.MdcForkJoinPool: submit: Runnable
16 Oct 2018 15:05:32,571 [INFO]  (ForkJoinPool-1-worker-25) com.amazon.service.utils.MdcForkJoinPool: Before Execution: {RequestId=eb73e823-d154-11e8-871a-d138501061d6}
16 Oct 2018 15:05:32,571 [INFO]  (ForkJoinPool-1-worker-25) com.amazon.service.utils.MdcForkJoinPool: Before Execution: {}
16 Oct 2018 15:05:32,571 [INFO]  (ForkJoinPool-1-worker-25) com.amazon.service.utils.MdcForkJoinPool: Setting mDC: {}
16 Oct 2018 15:05:32,574 [INFO]  (ForkJoinPool-1-worker-4) com.amazon.service.activity.business.VariablesEvaluator: Sample log line
16 Oct 2018 15:05:32,574 [INFO]  (ForkJoinPool-1-worker-29) com.amazon.service.activity.business.VariablesEvaluator: Sample log line
16 Oct 2018 15:05:32,575 [INFO] eb73e823-d154-11e8-871a-d138501061d6 (ForkJoinPool-1-worker-25) com.amazon.service.activity.business.VariablesEvaluator: Sample log line
16 Oct 2018 15:05:32,576 [INFO]  (ForkJoinPool-1-worker-22) com.amazon.service.activity.business.VariablesEvaluator: Sample log line
16 Oct 2018 15:05:32,576 [INFO]  (ForkJoinPool-1-worker-4) com.amazon.service.activity.business.VariablesEvaluator: some Other logs
16 Oct 2018 15:05:32,576 [INFO] eb73e823-d154-11e8-871a-d138501061d6 (ForkJoinPool-1-worker-25) com.amazon.service.activity.business.VariablesEvaluator: some Other logs

Am I not using the custom ForkJoinPool in the correct way?

来源:https://stackoverflow.com/questions/52840346/mdc-context-getting-copied-to-only-one-thread-of-forkjoinpool

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!