From cfe5a11778be0e113a89eb9af4da7704711ac69a Mon Sep 17 00:00:00 2001 From: "chen.ma" Date: Wed, 2 Mar 2022 21:32:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B5=8B=E8=AF=95=E7=94=A8?= =?UTF-8?q?=E4=BE=8B.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/bootstrap.yaml | 1 + .../example/core/config/ThreadPoolConfig.java | 3 ++ .../core/handler/TaskTraceBuilderHandler.java | 38 +++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 hippo4j-example/hippo4j-example-core/src/main/java/cn/hippo4j/example/core/handler/TaskTraceBuilderHandler.java diff --git a/hippo4j-example/hippo4j-core-nacos-spring-boot-starter-example/src/main/resources/bootstrap.yaml b/hippo4j-example/hippo4j-core-nacos-spring-boot-starter-example/src/main/resources/bootstrap.yaml index d790e957..b0bcbdce 100644 --- a/hippo4j-example/hippo4j-core-nacos-spring-boot-starter-example/src/main/resources/bootstrap.yaml +++ b/hippo4j-example/hippo4j-core-nacos-spring-boot-starter-example/src/main/resources/bootstrap.yaml @@ -38,6 +38,7 @@ spring: maximum-pool-size: 1 queue-capacity: 1 blocking-queue: 'LinkedBlockingQueue' + executeTimeOut: 1000 rejected-handler: 'AbortPolicy' keep-alive-time: 6691 allow-core-thread-time-out: true diff --git a/hippo4j-example/hippo4j-example-core/src/main/java/cn/hippo4j/example/core/config/ThreadPoolConfig.java b/hippo4j-example/hippo4j-example-core/src/main/java/cn/hippo4j/example/core/config/ThreadPoolConfig.java index 6ab0ff85..fd01f4f4 100644 --- a/hippo4j-example/hippo4j-example-core/src/main/java/cn/hippo4j/example/core/config/ThreadPoolConfig.java +++ b/hippo4j-example/hippo4j-example-core/src/main/java/cn/hippo4j/example/core/config/ThreadPoolConfig.java @@ -4,6 +4,7 @@ import cn.hippo4j.core.executor.DynamicThreadPool; import cn.hippo4j.core.executor.DynamicThreadPoolExecutor; import cn.hippo4j.core.executor.DynamicThreadPoolWrapper; import cn.hippo4j.core.executor.support.ThreadPoolBuilder; +import cn.hippo4j.example.core.handler.TaskTraceBuilderHandler; import cn.hippo4j.example.core.inittest.TaskDecoratorTest; import lombok.extern.slf4j.Slf4j; import org.springframework.context.annotation.Bean; @@ -34,7 +35,9 @@ public class ThreadPoolConfig { ThreadPoolExecutor customExecutor = ThreadPoolBuilder.builder() .dynamicPool() .threadFactory(MESSAGE_CONSUME) + .taskTraceBuilder(new TaskTraceBuilderHandler()) .build(); + return new DynamicThreadPoolWrapper(MESSAGE_CONSUME, customExecutor); } diff --git a/hippo4j-example/hippo4j-example-core/src/main/java/cn/hippo4j/example/core/handler/TaskTraceBuilderHandler.java b/hippo4j-example/hippo4j-example-core/src/main/java/cn/hippo4j/example/core/handler/TaskTraceBuilderHandler.java new file mode 100644 index 00000000..0455ca17 --- /dev/null +++ b/hippo4j-example/hippo4j-example-core/src/main/java/cn/hippo4j/example/core/handler/TaskTraceBuilderHandler.java @@ -0,0 +1,38 @@ +package cn.hippo4j.example.core.handler; + +import cn.hippo4j.common.notify.TaskTraceBuilder; +import org.slf4j.MDC; + +/** + * Task trace builder handler. + * + * @author chen.ma + * @date 2022/3/2 20:46 + */ +public class TaskTraceBuilderHandler implements TaskTraceBuilder { + + private final String TRACE_KEY = "traceId"; + + @Override + public void before() { + MDC.put(TRACE_KEY, "https://github.com/acmenlt/dynamic-threadpool 行行好, 点个 Star."); + } + + @Override + public String traceBuild() { + String traceStr; + try { + traceStr = MDC.get(TRACE_KEY); + } finally { + clear(); + } + + return traceStr; + } + + @Override + public void clear() { + MDC.remove(TRACE_KEY); + } + +}