MDC上下文被复制到仅ForkJoinPool的一个线程

时间:2018-10-16 16:47:07

标签: multithreading mdc forkjoinpool

我想将MDC上下文传递给ForkJoinPool的线程,因为我需要在所有生成的线程日志中打印MDCContextMap中填充的requestId,以进行更好的调试。为此,我正在使用以下解决方案:How to use MDC with ForkJoinPool?

我将泳池用作:

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

但是从日志看来,MDC上下文似乎仅被复制到池的一个线程中。我还在MdcForkJoinPool的Submit方法和beforeExecution方法中放置了一些日志。以下是仅在一个线程(“ ForkJoinPool-1-worker-25”)日志中打印requestID的日志:

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

我使用的自定义ForkJoinPool是否正确?

0 个答案:

没有答案