问题
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