From c2c06899e9a8a95b75ba92206e7bbf75eb5c5a69 Mon Sep 17 00:00:00 2001 From: "chen.ma" Date: Sun, 13 Mar 2022 18:54:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=8A=A8=E6=80=81=E7=BA=BF?= =?UTF-8?q?=E7=A8=8B=E6=B1=A0=E7=A4=BA=E4=BE=8B=E9=A1=B9=E7=9B=AE.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/core/config/ThreadPoolConfig.java | 40 +++++++++---------- .../core/inittest/RunStateHandlerTest.java | 9 ++--- .../core/inittest/TaskDecoratorTest.java | 2 +- 3 files changed, 23 insertions(+), 28 deletions(-) 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 cde7155e..f369f417 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 @@ -1,8 +1,6 @@ package cn.hippo4j.example.core.config; 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; @@ -25,44 +23,42 @@ import static cn.hippo4j.example.core.constant.GlobalTestConstant.MESSAGE_PRODUC @Configuration public class ThreadPoolConfig { - /** - * {@link DynamicThreadPoolWrapper} 完成 Server 端订阅配置功能. - * - * @return - */ @Bean - public DynamicThreadPoolWrapper messageCenterDynamicThreadPool() { + @DynamicThreadPool + public ThreadPoolExecutor messageConsumeDynamicThreadPool() { + String threadPoolId = MESSAGE_CONSUME; ThreadPoolExecutor customExecutor = ThreadPoolBuilder.builder() .dynamicPool() + .threadFactory(threadPoolId) + .threadPoolId(threadPoolId) .executeTimeOut(800L) + .waitForTasksToCompleteOnShutdown(true) + .awaitTerminationMillis(5000L) .taskDecorator(new TaskTraceBuilderHandler()) - .threadFactory(MESSAGE_CONSUME) .build(); - return new DynamicThreadPoolWrapper(MESSAGE_CONSUME, customExecutor); + return customExecutor; } - /** - * 通过 {@link DynamicThreadPool} 修饰 {@link DynamicThreadPoolExecutor} 完成 Server 端订阅配置功能. - *

- * 由动态线程池注解修饰后, IOC 容器中保存的是 {@link DynamicThreadPoolExecutor} - * - * @return - */ @Bean @DynamicThreadPool - public ThreadPoolExecutor dynamicThreadPoolExecutor() { - return ThreadPoolBuilder.builder() - .threadFactory(MESSAGE_PRODUCE) + public ThreadPoolExecutor messageProduceDynamicThreadPool() { + String threadPoolId = MESSAGE_PRODUCE; + ThreadPoolExecutor produceExecutor = ThreadPoolBuilder.builder() .dynamicPool() + .threadFactory(threadPoolId) + .threadPoolId(threadPoolId) + .executeTimeOut(900L) + .waitForTasksToCompleteOnShutdown(true) + .awaitTerminationMillis(5000L) /** * 测试线程任务装饰器. * 如果需要查看详情, 跳转 {@link TaskDecoratorTest} */ - .waitForTasksToCompleteOnShutdown(true) - .awaitTerminationMillis(5000) .taskDecorator(new TaskDecoratorTest.ContextCopyingDecorator()) .build(); + + return produceExecutor; } } diff --git a/hippo4j-example/hippo4j-example-core/src/main/java/cn/hippo4j/example/core/inittest/RunStateHandlerTest.java b/hippo4j-example/hippo4j-example-core/src/main/java/cn/hippo4j/example/core/inittest/RunStateHandlerTest.java index b14ba917..979d5052 100644 --- a/hippo4j-example/hippo4j-example-core/src/main/java/cn/hippo4j/example/core/inittest/RunStateHandlerTest.java +++ b/hippo4j-example/hippo4j-example-core/src/main/java/cn/hippo4j/example/core/inittest/RunStateHandlerTest.java @@ -1,6 +1,5 @@ package cn.hippo4j.example.core.inittest; -import cn.hippo4j.core.executor.DynamicThreadPoolWrapper; import cn.hutool.core.thread.ThreadUtil; import lombok.extern.slf4j.Slf4j; import org.slf4j.MDC; @@ -25,10 +24,10 @@ import static cn.hippo4j.common.constant.Constants.EXECUTE_TIMEOUT_TRACE; public class RunStateHandlerTest { @Resource - private DynamicThreadPoolWrapper messageCenterDynamicThreadPool; + private ThreadPoolExecutor messageConsumeDynamicThreadPool; @Resource - private ThreadPoolExecutor dynamicThreadPoolExecutor; + private ThreadPoolExecutor messageProduceDynamicThreadPool; @PostConstruct @SuppressWarnings("all") @@ -36,10 +35,10 @@ public class RunStateHandlerTest { log.info("Test thread pool runtime state interface..."); // 启动动态线程池模拟运行任务 - runTask(messageCenterDynamicThreadPool.getExecutor()); + runTask(messageConsumeDynamicThreadPool); // 启动动态线程池模拟运行任务 - runTask(dynamicThreadPoolExecutor); + runTask(messageProduceDynamicThreadPool); } private void runTask(ExecutorService executorService) { diff --git a/hippo4j-example/hippo4j-example-core/src/main/java/cn/hippo4j/example/core/inittest/TaskDecoratorTest.java b/hippo4j-example/hippo4j-example-core/src/main/java/cn/hippo4j/example/core/inittest/TaskDecoratorTest.java index 3db0e8af..b762475e 100644 --- a/hippo4j-example/hippo4j-example-core/src/main/java/cn/hippo4j/example/core/inittest/TaskDecoratorTest.java +++ b/hippo4j-example/hippo4j-example-core/src/main/java/cn/hippo4j/example/core/inittest/TaskDecoratorTest.java @@ -36,7 +36,7 @@ public class TaskDecoratorTest { threadPoolExecutor.execute(() -> { /** * 此处打印不为空, taskDecorator 即为生效. - * taskDecorator 配置查看 {@link ThreadPoolConfig#messageCenterDynamicThreadPool()} + * taskDecorator 配置查看 {@link ThreadPoolConfig#messageConsumeDynamicThreadPool()} */ log.info("通过 taskDecorator MDC 传递上下文 :: {}", MDC.get(PLACEHOLDER)); });