From 4c6fa752b9c8cb80b99265dffa34f55ec1e9d3d2 Mon Sep 17 00:00:00 2001 From: "chen.ma" Date: Sun, 28 Nov 2021 18:11:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A8=E6=80=81=E7=BA=BF=E7=A8=8B=E6=B1=A0?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/config/ThreadPoolConfig.java | 11 +++- .../example/inittest/TaskDecoratorTest.java | 61 +++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 hippo4j-example/src/main/java/cn/hippo4j/example/inittest/TaskDecoratorTest.java diff --git a/hippo4j-example/src/main/java/cn/hippo4j/example/config/ThreadPoolConfig.java b/hippo4j-example/src/main/java/cn/hippo4j/example/config/ThreadPoolConfig.java index 50c13061..2fc21b7e 100644 --- a/hippo4j-example/src/main/java/cn/hippo4j/example/config/ThreadPoolConfig.java +++ b/hippo4j-example/src/main/java/cn/hippo4j/example/config/ThreadPoolConfig.java @@ -1,5 +1,6 @@ package cn.hippo4j.example.config; +import cn.hippo4j.example.inittest.TaskDecoratorTest; import cn.hippo4j.starter.core.DynamicThreadPool; import cn.hippo4j.starter.core.DynamicThreadPoolExecutor; import cn.hippo4j.starter.toolkit.thread.ThreadPoolBuilder; @@ -10,7 +11,8 @@ import org.springframework.context.annotation.Configuration; import java.util.concurrent.ThreadPoolExecutor; -import static cn.hippo4j.example.constant.GlobalTestConstant.*; +import static cn.hippo4j.example.constant.GlobalTestConstant.MESSAGE_CONSUME; +import static cn.hippo4j.example.constant.GlobalTestConstant.MESSAGE_PRODUCE; /** * Thread pool config. @@ -49,6 +51,13 @@ public class ThreadPoolConfig { return ThreadPoolBuilder.builder() .threadFactory(MESSAGE_PRODUCE) .dynamicPool() + /** + * 测试线程任务装饰器. + * 如果需要查看详情, 跳转 {@link TaskDecoratorTest} + */ + .waitForTasksToCompleteOnShutdown(true) + .awaitTerminationMillis(5000) + .taskDecorator(new TaskDecoratorTest.ContextCopyingDecorator()) .build(); } diff --git a/hippo4j-example/src/main/java/cn/hippo4j/example/inittest/TaskDecoratorTest.java b/hippo4j-example/src/main/java/cn/hippo4j/example/inittest/TaskDecoratorTest.java new file mode 100644 index 00000000..89ac860a --- /dev/null +++ b/hippo4j-example/src/main/java/cn/hippo4j/example/inittest/TaskDecoratorTest.java @@ -0,0 +1,61 @@ +package cn.hippo4j.example.inittest; + +import cn.hippo4j.example.constant.GlobalTestConstant; +import cn.hippo4j.starter.core.GlobalThreadPoolManage; +import cn.hippo4j.starter.wrapper.DynamicThreadPoolWrapper; +import cn.hutool.core.thread.ThreadUtil; +import lombok.extern.slf4j.Slf4j; +import org.slf4j.MDC; +import org.springframework.core.task.TaskDecorator; +import org.springframework.stereotype.Component; + +import java.util.concurrent.ThreadPoolExecutor; + +/** + * TaskDecorator test. + * + * @author chen.ma + * @date 2021/11/28 13:01 + */ +@Slf4j +@Component +public class TaskDecoratorTest { + + public static final String PLACEHOLDER = "site"; + + // @PostConstruct + public void taskDecoratorTest() { + new Thread(() -> { + MDC.put(PLACEHOLDER, "查看官网: https://www.hippox.cn"); + ThreadUtil.sleep(5000); + DynamicThreadPoolWrapper poolWrapper = GlobalThreadPoolManage.getExecutorService(GlobalTestConstant.MESSAGE_PRODUCE); + ThreadPoolExecutor threadPoolExecutor = poolWrapper.getExecutor(); + threadPoolExecutor.execute(() -> { + /** + * 此处打印不为空, taskDecorator 即为生效. + * taskDecorator 配置查看 {@link ThreadPoolConfig#messageCenterDynamicThreadPool()} + */ + log.info("通过 taskDecorator MDC 传递上下文 :: {}", MDC.get(PLACEHOLDER)); + }); + }).start(); + + } + + public static class ContextCopyingDecorator implements TaskDecorator { + + @Override + public Runnable decorate(Runnable runnable) { + String placeholderVal = MDC.get(PLACEHOLDER); + // other context... + return () -> { + try { + MDC.put(PLACEHOLDER, placeholderVal); + runnable.run(); + } finally { + MDC.clear(); + } + }; + } + } + +}