diff --git a/example/src/main/java/com/github/dynamic/threadpool/example/config/ThreadPoolConfig.java b/example/src/main/java/com/github/dynamic/threadpool/example/config/ThreadPoolConfig.java index b5cc17a7..c372f32a 100644 --- a/example/src/main/java/com/github/dynamic/threadpool/example/config/ThreadPoolConfig.java +++ b/example/src/main/java/com/github/dynamic/threadpool/example/config/ThreadPoolConfig.java @@ -1,10 +1,17 @@ package com.github.dynamic.threadpool.example.config; +import com.github.dynamic.threadpool.starter.core.DynamicThreadPool; +import com.github.dynamic.threadpool.starter.toolkit.thread.CustomThreadPoolExecutor; +import com.github.dynamic.threadpool.starter.toolkit.thread.ThreadPoolBuilder; import com.github.dynamic.threadpool.starter.wrap.DynamicThreadPoolWrap; import lombok.extern.slf4j.Slf4j; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import java.util.concurrent.ThreadPoolExecutor; + +import static com.github.dynamic.threadpool.example.constant.GlobalTestConstant.*; + /** * Thread pool config. * @@ -15,19 +22,27 @@ import org.springframework.context.annotation.Configuration; @Configuration public class ThreadPoolConfig { + /** + * {@link DynamicThreadPoolWrap} 完成 Server 端订阅配置功能. + * + * @return + */ @Bean public DynamicThreadPoolWrap messageCenterConsumeThreadPool() { - return new DynamicThreadPoolWrap("message-consume"); - } - - @Bean - public DynamicThreadPoolWrap messageCenterProduceThreadPool() { - return new DynamicThreadPoolWrap("message-produce"); + return new DynamicThreadPoolWrap(MESSAGE_CONSUME); } + /** + * 通过 {@link DynamicThreadPool} 修饰 {@link CustomThreadPoolExecutor} 完成 Server 端订阅配置功能. + *
+ * 由动态线程池注解修饰后, IOC 容器中保存的是 {@link CustomThreadPoolExecutor} + * + * @return + */ @Bean - public DynamicThreadPoolWrap customPool() { - return new DynamicThreadPoolWrap("custom-pool"); + @DynamicThreadPool + public ThreadPoolExecutor customThreadPoolExecutor() { + return ThreadPoolBuilder.builder().threadFactory(MESSAGE_PRODUCE).threadPoolId(MESSAGE_PRODUCE).isCustomPool(true).build(); } } diff --git a/example/src/main/java/com/github/dynamic/threadpool/example/constant/GlobalTestConstant.java b/example/src/main/java/com/github/dynamic/threadpool/example/constant/GlobalTestConstant.java new file mode 100644 index 00000000..bdb251d5 --- /dev/null +++ b/example/src/main/java/com/github/dynamic/threadpool/example/constant/GlobalTestConstant.java @@ -0,0 +1,16 @@ +package com.github.dynamic.threadpool.example.constant; + +/** + * Global test variables. + * + * @author chen.ma + * @date 2021/8/15 21:06 + */ +public class GlobalTestConstant { + + public static final String CUSTOM_POOL = "custom-pool"; + + public static final String MESSAGE_PRODUCE = "message-produce"; + + public static final String MESSAGE_CONSUME = "message-consume"; +} diff --git a/example/src/main/java/com/github/dynamic/threadpool/example/inittest/AlarmSendMessageTest.java b/example/src/main/java/com/github/dynamic/threadpool/example/inittest/AlarmSendMessageTest.java index 55b134ef..73243045 100644 --- a/example/src/main/java/com/github/dynamic/threadpool/example/inittest/AlarmSendMessageTest.java +++ b/example/src/main/java/com/github/dynamic/threadpool/example/inittest/AlarmSendMessageTest.java @@ -11,7 +11,7 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; -import static com.github.dynamic.threadpool.example.inittest.GlobalTestConstant.TEST_THREAD_POOL_ID; +import static com.github.dynamic.threadpool.example.constant.GlobalTestConstant.MESSAGE_PRODUCE; /** * Test alarm send message. @@ -28,7 +28,7 @@ public class AlarmSendMessageTest { public void alarmSendMessageTest() { ScheduledExecutorService scheduledThreadPool = Executors.newSingleThreadScheduledExecutor(); scheduledThreadPool.scheduleWithFixedDelay(() -> { - DynamicThreadPoolWrap executorService = GlobalThreadPoolManage.getExecutorService(TEST_THREAD_POOL_ID); + DynamicThreadPoolWrap executorService = GlobalThreadPoolManage.getExecutorService(MESSAGE_PRODUCE); ThreadPoolExecutor poolExecutor = executorService.getPool(); try { poolExecutor.execute(() -> ThreadUtil.sleep(10240124)); diff --git a/example/src/main/java/com/github/dynamic/threadpool/example/inittest/GlobalTestConstant.java b/example/src/main/java/com/github/dynamic/threadpool/example/inittest/GlobalTestConstant.java deleted file mode 100644 index c3a3dd49..00000000 --- a/example/src/main/java/com/github/dynamic/threadpool/example/inittest/GlobalTestConstant.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.github.dynamic.threadpool.example.inittest; - -/** - * Global test variables. - * - * @author chen.ma - * @date 2021/8/15 21:06 - */ -public class GlobalTestConstant { - - public static final String TEST_THREAD_POOL_ID = "custom-pool"; - -} diff --git a/example/src/main/java/com/github/dynamic/threadpool/example/inittest/RunStateHandlerTest.java b/example/src/main/java/com/github/dynamic/threadpool/example/inittest/RunStateHandlerTest.java index 5e163831..85d49c9b 100644 --- a/example/src/main/java/com/github/dynamic/threadpool/example/inittest/RunStateHandlerTest.java +++ b/example/src/main/java/com/github/dynamic/threadpool/example/inittest/RunStateHandlerTest.java @@ -11,7 +11,7 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; -import static com.github.dynamic.threadpool.example.inittest.GlobalTestConstant.TEST_THREAD_POOL_ID; +import static com.github.dynamic.threadpool.example.constant.GlobalTestConstant.MESSAGE_PRODUCE; /** * Test run time metrics. @@ -30,7 +30,7 @@ public class RunStateHandlerTest { ScheduledExecutorService scheduledThreadPool = Executors.newSingleThreadScheduledExecutor(); scheduledThreadPool.scheduleAtFixedRate(() -> { - DynamicThreadPoolWrap executorService = GlobalThreadPoolManage.getExecutorService(TEST_THREAD_POOL_ID); + DynamicThreadPoolWrap executorService = GlobalThreadPoolManage.getExecutorService(MESSAGE_PRODUCE); ThreadPoolExecutor pool = executorService.getPool(); try { pool.execute(() -> {