diff --git a/dynamic-threadpool-example/src/main/java/com/github/dynamic/threadpool/example/config/ThreadPoolConfig.java b/dynamic-threadpool-example/src/main/java/com/github/dynamic/threadpool/example/config/ThreadPoolConfig.java index 7998faab..54171cbe 100644 --- a/dynamic-threadpool-example/src/main/java/com/github/dynamic/threadpool/example/config/ThreadPoolConfig.java +++ b/dynamic-threadpool-example/src/main/java/com/github/dynamic/threadpool/example/config/ThreadPoolConfig.java @@ -1,17 +1,10 @@ package com.github.dynamic.threadpool.example.config; -import com.github.dynamic.threadpool.starter.core.GlobalThreadPoolManage; 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.Random; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; - /** * 线程池配置 * @@ -22,57 +15,19 @@ import java.util.concurrent.TimeUnit; @Configuration public class ThreadPoolConfig { - private String messageConsumePrefix = "message-consume"; - - private String messageProducePrefix = "message-produce"; - - private String customPoolPrefix = "custom-pool"; - @Bean public DynamicThreadPoolWrap messageCenterConsumeThreadPool() { - return new DynamicThreadPoolWrap(messageConsumePrefix); + return new DynamicThreadPoolWrap("message-consume"); } @Bean public DynamicThreadPoolWrap messageCenterProduceThreadPool() { - return new DynamicThreadPoolWrap(messageProducePrefix); + return new DynamicThreadPoolWrap("message-produce"); } @Bean public DynamicThreadPoolWrap customPool() { - return new DynamicThreadPoolWrap(customPoolPrefix); + return new DynamicThreadPoolWrap("custom-pool"); } - // @PostConstruct - @SuppressWarnings("all") - public void testExecuteTask() { - log.info("测试线程池运行时状态接口, 30s 后开始触发拒绝策略..."); - - ScheduledExecutorService scheduledThreadPool = Executors.newSingleThreadScheduledExecutor(); - scheduledThreadPool.scheduleAtFixedRate(() -> { - DynamicThreadPoolWrap executorService = GlobalThreadPoolManage.getExecutorService(customPoolPrefix); - ThreadPoolExecutor pool = executorService.getPool(); - try { - pool.execute(() -> { - log.info("线程池名称 :: {}, 正在执行即将进入阻塞...", Thread.currentThread().getName()); - try { - int maxRandom = 10; - int temp = 2; - Random random = new Random(); - // 这里为了赋值线程池 completedTaskCount - if (random.nextInt(maxRandom) % temp == 0) { - Thread.sleep(10241024); - } else { - Thread.sleep(3000); - } - } catch (InterruptedException e) { - // ignore - } - }); - } catch (Exception ex) { - // ignore - } - - }, 5, 2, TimeUnit.SECONDS); - } } diff --git a/dynamic-threadpool-example/src/main/java/com/github/dynamic/threadpool/example/inittest/AlarmSendMessageTest.java b/dynamic-threadpool-example/src/main/java/com/github/dynamic/threadpool/example/inittest/AlarmSendMessageTest.java new file mode 100644 index 00000000..82b8178f --- /dev/null +++ b/dynamic-threadpool-example/src/main/java/com/github/dynamic/threadpool/example/inittest/AlarmSendMessageTest.java @@ -0,0 +1,41 @@ +package com.github.dynamic.threadpool.example.inittest; + +import cn.hutool.core.thread.ThreadUtil; +import com.github.dynamic.threadpool.starter.core.GlobalThreadPoolManage; +import com.github.dynamic.threadpool.starter.wrap.DynamicThreadPoolWrap; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import java.util.concurrent.Executors; +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; + +/** + * Test alarm send message. + * + * @author chen.ma + * @date 2021/8/15 21:03 + */ +@Slf4j +@Component +public class AlarmSendMessageTest { + + // @PostConstruct + @SuppressWarnings("all") + public void alarmSendMessageTest() { + ScheduledExecutorService scheduledThreadPool = Executors.newSingleThreadScheduledExecutor(); + scheduledThreadPool.scheduleWithFixedDelay(() -> { + DynamicThreadPoolWrap executorService = GlobalThreadPoolManage.getExecutorService(TEST_THREAD_POOL_ID); + ThreadPoolExecutor poolExecutor = executorService.getPool(); + try { + poolExecutor.execute(() -> ThreadUtil.sleep(10240124)); + } catch (Exception ex) { + log.error("抛出拒绝策略", ex.getMessage()); + } + }, 3, 1, TimeUnit.SECONDS); + } + +} diff --git a/dynamic-threadpool-example/src/main/java/com/github/dynamic/threadpool/example/inittest/GlobalTestConstant.java b/dynamic-threadpool-example/src/main/java/com/github/dynamic/threadpool/example/inittest/GlobalTestConstant.java new file mode 100644 index 00000000..c3a3dd49 --- /dev/null +++ b/dynamic-threadpool-example/src/main/java/com/github/dynamic/threadpool/example/inittest/GlobalTestConstant.java @@ -0,0 +1,13 @@ +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/dynamic-threadpool-example/src/main/java/com/github/dynamic/threadpool/example/inittest/RunStateHandlerTest.java b/dynamic-threadpool-example/src/main/java/com/github/dynamic/threadpool/example/inittest/RunStateHandlerTest.java new file mode 100644 index 00000000..4183c760 --- /dev/null +++ b/dynamic-threadpool-example/src/main/java/com/github/dynamic/threadpool/example/inittest/RunStateHandlerTest.java @@ -0,0 +1,58 @@ +package com.github.dynamic.threadpool.example.inittest; + +import com.github.dynamic.threadpool.starter.core.GlobalThreadPoolManage; +import com.github.dynamic.threadpool.starter.wrap.DynamicThreadPoolWrap; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import java.util.Random; +import java.util.concurrent.Executors; +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; + +/** + * Test run time metrics. + * + * @author chen.ma + * @date 2021/8/15 21:00 + */ +@Slf4j +@Component +public class RunStateHandlerTest { + + // @PostConstruct + @SuppressWarnings("all") + public void runStateHandlerTest() { + log.info("测试线程池运行时状态接口, 30s 后开始触发拒绝策略..."); + + ScheduledExecutorService scheduledThreadPool = Executors.newSingleThreadScheduledExecutor(); + scheduledThreadPool.scheduleAtFixedRate(() -> { + DynamicThreadPoolWrap executorService = GlobalThreadPoolManage.getExecutorService(TEST_THREAD_POOL_ID); + ThreadPoolExecutor pool = executorService.getPool(); + try { + pool.execute(() -> { + log.info("线程池名称 :: {}, 正在执行即将进入阻塞...", Thread.currentThread().getName()); + try { + int maxRandom = 10; + int temp = 2; + Random random = new Random(); + // 这里为了赋值线程池 completedTaskCount + if (random.nextInt(maxRandom) % temp == 0) { + Thread.sleep(10241024); + } else { + Thread.sleep(3000); + } + } catch (InterruptedException e) { + // ignore + } + }); + } catch (Exception ex) { + // ignore + } + + }, 5, 2, TimeUnit.SECONDS); + } +}