1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
public class MdcThreadPoolTaskExecutor extends ThreadPoolTaskExecutor {
public static MdcThreadPoolTaskExecutor newWithInheritedMdc(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, int queueCapacity, RejectedExecutionHandler rejectedExecutionHandler) { return new MdcThreadPoolTaskExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, queueCapacity, rejectedExecutionHandler); }
private MdcThreadPoolTaskExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, int queueCapacity, RejectedExecutionHandler rejectedExecutionHandler) { setCorePoolSize(corePoolSize); setMaxPoolSize(maximumPoolSize); setKeepAliveSeconds((int) unit.toSeconds(keepAliveTime)); setQueueCapacity(queueCapacity); setRejectedExecutionHandler(rejectedExecutionHandler); }
private Map<String, String> initMdcContext() { Map<String, String> context = MDC.getCopyOfContextMap(); if (Validator.isNull(context)) { context = new HashMap<>(); } if (CollectionUtil.isEmpty(context)) { context.put(MdcApiConstant.HTTP_HEADER_TRACE_ID, MdcTool.getInstance().currentTraceId()); } return context; }
@Override public void execute(@NonNull Runnable runnable) { super.execute(() -> run(runnable, initMdcContext())); }
@NonNull @Override public Future<?> submit(@NonNull Runnable task) { return super.submit(() -> run(task, initMdcContext())); }
@NonNull @Override public <T> Future<T> submit(@NonNull Callable<T> task) { return super.submit(submit(task, initMdcContext())); }
private void run(Runnable runnable, Map<String, String> context) { if (context != null) { try { MDC.setContextMap(context); } catch (Exception e) { logger.error(e.getMessage(), e); } } try { runnable.run(); } finally { MDC.clear(); } }
private static <T> Callable<T> submit(final Callable<T> task, final Map<String, String> context) { return () -> { Map<String, String> previous = MDC.getCopyOfContextMap(); if (context == null) { MDC.clear(); } else { MDC.setContextMap(context); } try { return task.call(); } finally { if (previous == null) { MDC.clear(); } else { MDC.setContextMap(previous); } } }; } }
package cn.z201.mdc.log;
import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit;
public class MdcThreadTaskUtils {
private static MdcThreadPoolTaskExecutor taskExecutor = null;
static {
} public static void run(Runnable runnable) { if (null == taskExecutor) { taskExecutor = MdcThreadPoolTaskExecutor.newWithInheritedMdc(5, 50, 1000, TimeUnit.SECONDS, 120, new ThreadPoolExecutor.AbortPolicy()); taskExecutor.initialize(); } taskExecutor.execute(runnable); } }
|