From 7bd785f76cd53b7160963ab2e1dcc0836792f31f Mon Sep 17 00:00:00 2001 From: "chen.ma" Date: Mon, 1 Nov 2021 21:15:41 +0800 Subject: [PATCH] =?UTF-8?q?feature:=20=E4=BF=AE=E6=94=B9=E5=8A=A8=E6=80=81?= =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E6=B1=A0=E5=91=BD=E5=90=8D.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../starter/alarm/BaseSendMessageService.java | 4 +- .../starter/alarm/DingSendMessageHandler.java | 12 +++--- .../starter/alarm/SendMessageHandler.java | 4 +- .../starter/alarm/SendMessageService.java | 4 +- .../starter/alarm/ThreadPoolAlarmManage.java | 10 ++--- ...Pool.java => CommonDynamicThreadPool.java} | 13 +++--- .../DynamicThreadPoolExecutor.java} | 24 +++++------ .../core/DynamicThreadPoolPostProcessor.java | 40 +++++++++---------- .../starter/core/GlobalThreadPoolManage.java | 10 ++--- .../handler/ThreadPoolRunStateHandler.java | 10 ++--- .../AbstractBuildThreadPoolTemplate.java | 9 +++-- .../toolkit/thread/ThreadPoolBuilder.java | 20 +++++----- ...rap.java => DynamicThreadPoolWrapper.java} | 16 ++++---- 13 files changed, 88 insertions(+), 88 deletions(-) rename dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/common/{CommonThreadPool.java => CommonDynamicThreadPool.java} (65%) rename dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/{toolkit/thread/CustomThreadPoolExecutor.java => core/DynamicThreadPoolExecutor.java} (97%) rename dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/wrap/{DynamicThreadPoolWrap.java => DynamicThreadPoolWrapper.java} (69%) diff --git a/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/alarm/BaseSendMessageService.java b/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/alarm/BaseSendMessageService.java index fd97161a..4cad614b 100644 --- a/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/alarm/BaseSendMessageService.java +++ b/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/alarm/BaseSendMessageService.java @@ -2,7 +2,7 @@ package com.github.dynamic.threadpool.starter.alarm; import com.github.dynamic.threadpool.common.config.ApplicationContextHolder; import com.github.dynamic.threadpool.common.model.PoolParameterInfo; -import com.github.dynamic.threadpool.starter.toolkit.thread.CustomThreadPoolExecutor; +import com.github.dynamic.threadpool.starter.core.DynamicThreadPoolExecutor; import lombok.NonNull; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -28,7 +28,7 @@ public class BaseSendMessageService implements InitializingBean, SendMessageServ private final List sendMessageHandlers = new ArrayList(4); @Override - public void sendAlarmMessage(CustomThreadPoolExecutor threadPoolExecutor) { + public void sendAlarmMessage(DynamicThreadPoolExecutor threadPoolExecutor) { for (SendMessageHandler messageHandler : sendMessageHandlers) { try { messageHandler.sendAlarmMessage(notifyConfigs, threadPoolExecutor); diff --git a/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/alarm/DingSendMessageHandler.java b/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/alarm/DingSendMessageHandler.java index dddd52c5..c7d488bf 100644 --- a/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/alarm/DingSendMessageHandler.java +++ b/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/alarm/DingSendMessageHandler.java @@ -8,10 +8,10 @@ import com.dingtalk.api.request.OapiRobotSendRequest; import com.github.dynamic.threadpool.common.model.InstanceInfo; import com.github.dynamic.threadpool.common.model.PoolParameterInfo; import com.github.dynamic.threadpool.starter.core.GlobalThreadPoolManage; -import com.github.dynamic.threadpool.starter.toolkit.thread.CustomThreadPoolExecutor; +import com.github.dynamic.threadpool.starter.core.DynamicThreadPoolExecutor; import com.github.dynamic.threadpool.starter.toolkit.thread.QueueTypeEnum; import com.github.dynamic.threadpool.starter.toolkit.thread.RejectedTypeEnum; -import com.github.dynamic.threadpool.starter.wrap.DynamicThreadPoolWrap; +import com.github.dynamic.threadpool.starter.wrap.DynamicThreadPoolWrapper; import com.google.common.base.Joiner; import com.taobao.api.ApiException; import lombok.AllArgsConstructor; @@ -45,7 +45,7 @@ public class DingSendMessageHandler implements SendMessageHandler { } @Override - public void sendAlarmMessage(List notifyConfigs, CustomThreadPoolExecutor pool) { + public void sendAlarmMessage(List notifyConfigs, DynamicThreadPoolExecutor pool) { Optional notifyConfigOptional = notifyConfigs.stream() .filter(each -> Objects.equals(each.getType(), getType())) .findFirst(); @@ -60,7 +60,7 @@ public class DingSendMessageHandler implements SendMessageHandler { changeConfigOptional.ifPresent(each -> dingChangeSendMessage(each, parameter)); } - private void dingAlarmSendMessage(NotifyConfig notifyConfig, CustomThreadPoolExecutor pool) { + private void dingAlarmSendMessage(NotifyConfig notifyConfig, DynamicThreadPoolExecutor pool) { List receives = StrUtil.split(notifyConfig.getReceives(), ','); String afterReceives = Joiner.on(", @").join(receives); @@ -133,7 +133,7 @@ public class DingSendMessageHandler implements SendMessageHandler { private void dingChangeSendMessage(NotifyConfig notifyConfig, PoolParameterInfo parameter) { String threadPoolId = parameter.getTpId(); - DynamicThreadPoolWrap poolWrap = GlobalThreadPoolManage.getExecutorService(threadPoolId); + DynamicThreadPoolWrapper poolWrap = GlobalThreadPoolManage.getExecutorService(threadPoolId); if (poolWrap == null) { log.warn("Thread pool is empty when sending change notification, threadPoolId :: {}", threadPoolId); return; @@ -142,7 +142,7 @@ public class DingSendMessageHandler implements SendMessageHandler { List receives = StrUtil.split(notifyConfig.getReceives(), ','); String afterReceives = Joiner.on(", @").join(receives); - CustomThreadPoolExecutor customPool = poolWrap.getPool(); + DynamicThreadPoolExecutor customPool = poolWrap.getPool(); /** * hesitant e.g. ➲ ➜ ⇨ ➪ */ diff --git a/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/alarm/SendMessageHandler.java b/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/alarm/SendMessageHandler.java index a056d4c0..07fbbd7a 100644 --- a/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/alarm/SendMessageHandler.java +++ b/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/alarm/SendMessageHandler.java @@ -1,7 +1,7 @@ package com.github.dynamic.threadpool.starter.alarm; import com.github.dynamic.threadpool.common.model.PoolParameterInfo; -import com.github.dynamic.threadpool.starter.toolkit.thread.CustomThreadPoolExecutor; +import com.github.dynamic.threadpool.starter.core.DynamicThreadPoolExecutor; import java.util.List; @@ -26,7 +26,7 @@ public interface SendMessageHandler { * @param notifyConfigs * @param threadPoolExecutor */ - void sendAlarmMessage(List notifyConfigs, CustomThreadPoolExecutor threadPoolExecutor); + void sendAlarmMessage(List notifyConfigs, DynamicThreadPoolExecutor threadPoolExecutor); /** * Send change message. diff --git a/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/alarm/SendMessageService.java b/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/alarm/SendMessageService.java index 71462d0d..cfce09d8 100644 --- a/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/alarm/SendMessageService.java +++ b/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/alarm/SendMessageService.java @@ -1,7 +1,7 @@ package com.github.dynamic.threadpool.starter.alarm; import com.github.dynamic.threadpool.common.model.PoolParameterInfo; -import com.github.dynamic.threadpool.starter.toolkit.thread.CustomThreadPoolExecutor; +import com.github.dynamic.threadpool.starter.core.DynamicThreadPoolExecutor; /** * Send msg. @@ -16,7 +16,7 @@ public interface SendMessageService { * * @param threadPoolExecutor */ - void sendAlarmMessage(CustomThreadPoolExecutor threadPoolExecutor); + void sendAlarmMessage(DynamicThreadPoolExecutor threadPoolExecutor); /** * Send change message. diff --git a/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/alarm/ThreadPoolAlarmManage.java b/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/alarm/ThreadPoolAlarmManage.java index 5b7950ad..f4fb37dc 100644 --- a/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/alarm/ThreadPoolAlarmManage.java +++ b/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/alarm/ThreadPoolAlarmManage.java @@ -4,7 +4,7 @@ import com.github.dynamic.threadpool.common.config.ApplicationContextHolder; import com.github.dynamic.threadpool.common.model.PoolParameterInfo; import com.github.dynamic.threadpool.starter.config.MessageAlarmConfig; import com.github.dynamic.threadpool.starter.toolkit.CalculateUtil; -import com.github.dynamic.threadpool.starter.toolkit.thread.CustomThreadPoolExecutor; +import com.github.dynamic.threadpool.starter.core.DynamicThreadPoolExecutor; import com.github.dynamic.threadpool.starter.toolkit.thread.ResizableCapacityLinkedBlockIngQueue; import lombok.extern.slf4j.Slf4j; @@ -44,7 +44,7 @@ public class ThreadPoolAlarmManage { * * @param threadPoolExecutor */ - public static void checkPoolCapacityAlarm(CustomThreadPoolExecutor threadPoolExecutor) { + public static void checkPoolCapacityAlarm(DynamicThreadPoolExecutor threadPoolExecutor) { if (SEND_MESSAGE_SERVICE == null) { return; } @@ -68,7 +68,7 @@ public class ThreadPoolAlarmManage { * @param isCore * @param threadPoolExecutor */ - public static void checkPoolLivenessAlarm(boolean isCore, CustomThreadPoolExecutor threadPoolExecutor) { + public static void checkPoolLivenessAlarm(boolean isCore, DynamicThreadPoolExecutor threadPoolExecutor) { if (isCore || SEND_MESSAGE_SERVICE == null || !isSendMessage(threadPoolExecutor, MessageTypeEnum.LIVENESS)) { return; } @@ -88,7 +88,7 @@ public class ThreadPoolAlarmManage { * * @param threadPoolExecutor */ - public static void checkPoolRejectAlarm(CustomThreadPoolExecutor threadPoolExecutor) { + public static void checkPoolRejectAlarm(DynamicThreadPoolExecutor threadPoolExecutor) { if (SEND_MESSAGE_SERVICE == null) { return; } @@ -118,7 +118,7 @@ public class ThreadPoolAlarmManage { * @param typeEnum * @return */ - private static boolean isSendMessage(CustomThreadPoolExecutor threadPoolExecutor, MessageTypeEnum typeEnum) { + private static boolean isSendMessage(DynamicThreadPoolExecutor threadPoolExecutor, MessageTypeEnum typeEnum) { AlarmControlDTO alarmControl = AlarmControlDTO.builder() .threadPool(threadPoolExecutor.getThreadPoolId()) .typeEnum(typeEnum) diff --git a/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/common/CommonThreadPool.java b/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/common/CommonDynamicThreadPool.java similarity index 65% rename from dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/common/CommonThreadPool.java rename to dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/common/CommonDynamicThreadPool.java index 76a80e34..c6445606 100644 --- a/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/common/CommonThreadPool.java +++ b/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/common/CommonDynamicThreadPool.java @@ -1,6 +1,6 @@ package com.github.dynamic.threadpool.starter.common; -import com.github.dynamic.threadpool.starter.toolkit.thread.CustomThreadPoolExecutor; +import com.github.dynamic.threadpool.starter.core.DynamicThreadPoolExecutor; import com.github.dynamic.threadpool.starter.toolkit.thread.QueueTypeEnum; import com.github.dynamic.threadpool.starter.toolkit.thread.RejectedPolicies; import com.github.dynamic.threadpool.starter.toolkit.thread.ThreadPoolBuilder; @@ -8,17 +8,16 @@ import com.github.dynamic.threadpool.starter.toolkit.thread.ThreadPoolBuilder; import java.util.concurrent.TimeUnit; /** - * Common threadPool. + * Common dynamic threadPool. * * @author chen.ma * @date 2021/6/16 22:35 */ -public class CommonThreadPool { +public class CommonDynamicThreadPool { - public static CustomThreadPoolExecutor getInstance(String threadPoolId) { - CustomThreadPoolExecutor poolExecutor = (CustomThreadPoolExecutor) ThreadPoolBuilder.builder() - .isCustomPool(true) - .threadPoolId(threadPoolId) + public static DynamicThreadPoolExecutor getInstance(String threadPoolId) { + DynamicThreadPoolExecutor poolExecutor = (DynamicThreadPoolExecutor) ThreadPoolBuilder.builder() + .dynamicPool() .threadFactory(threadPoolId) .poolThreadSize(3, 5) .keepAliveTime(1000L, TimeUnit.SECONDS) diff --git a/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/toolkit/thread/CustomThreadPoolExecutor.java b/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/core/DynamicThreadPoolExecutor.java similarity index 97% rename from dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/toolkit/thread/CustomThreadPoolExecutor.java rename to dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/core/DynamicThreadPoolExecutor.java index a770c361..7a518f36 100644 --- a/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/toolkit/thread/CustomThreadPoolExecutor.java +++ b/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/core/DynamicThreadPoolExecutor.java @@ -1,4 +1,4 @@ -package com.github.dynamic.threadpool.starter.toolkit.thread; +package com.github.dynamic.threadpool.starter.core; import com.github.dynamic.threadpool.starter.alarm.ThreadPoolAlarm; import com.github.dynamic.threadpool.starter.alarm.ThreadPoolAlarmManage; @@ -18,12 +18,12 @@ import java.util.concurrent.locks.ReentrantLock; import static com.github.dynamic.threadpool.common.constant.Constants.MAP_INITIAL_CAPACITY; /** - * Custom threadPool wrap. + * Dynamic threadPool wrap. * * @author chen.ma * @date 2021/7/8 21:47 */ -public final class CustomThreadPoolExecutor extends ThreadPoolExecutor { +public final class DynamicThreadPoolExecutor extends ThreadPoolExecutor { private final AtomicInteger rejectCount = new AtomicInteger(); private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0)); @@ -58,15 +58,15 @@ public final class CustomThreadPoolExecutor extends ThreadPoolExecutor { private static final RejectedExecutionHandler DEFAULT_HANDLER = new ThreadPoolExecutor.AbortPolicy(); private static final RuntimePermission SHUTDOWN_PERM = new RuntimePermission("modifyThread"); - public CustomThreadPoolExecutor(int corePoolSize, - int maximumPoolSize, - long keepAliveTime, - TimeUnit unit, - @NonNull BlockingQueue workQueue, - @NonNull String threadPoolId, - @NonNull ThreadFactory threadFactory, - @NonNull ThreadPoolAlarm threadPoolAlarm, - @NonNull RejectedExecutionHandler handler) { + public DynamicThreadPoolExecutor(int corePoolSize, + int maximumPoolSize, + long keepAliveTime, + TimeUnit unit, + @NonNull BlockingQueue workQueue, + @NonNull String threadPoolId, + @NonNull ThreadFactory threadFactory, + @NonNull ThreadPoolAlarm threadPoolAlarm, + @NonNull RejectedExecutionHandler handler) { super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler); if (corePoolSize < 0 || diff --git a/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/core/DynamicThreadPoolPostProcessor.java b/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/core/DynamicThreadPoolPostProcessor.java index 9b3a416c..26626f4e 100644 --- a/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/core/DynamicThreadPoolPostProcessor.java +++ b/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/core/DynamicThreadPoolPostProcessor.java @@ -5,14 +5,13 @@ import com.github.dynamic.threadpool.common.config.ApplicationContextHolder; import com.github.dynamic.threadpool.common.constant.Constants; import com.github.dynamic.threadpool.common.model.PoolParameterInfo; import com.github.dynamic.threadpool.common.web.base.Result; -import com.github.dynamic.threadpool.starter.common.CommonThreadPool; +import com.github.dynamic.threadpool.starter.common.CommonDynamicThreadPool; import com.github.dynamic.threadpool.starter.config.BootstrapProperties; import com.github.dynamic.threadpool.starter.remote.HttpAgent; -import com.github.dynamic.threadpool.starter.toolkit.thread.CustomThreadPoolExecutor; import com.github.dynamic.threadpool.starter.toolkit.thread.QueueTypeEnum; import com.github.dynamic.threadpool.starter.toolkit.thread.RejectedTypeEnum; import com.github.dynamic.threadpool.starter.toolkit.thread.ThreadPoolBuilder; -import com.github.dynamic.threadpool.starter.wrap.DynamicThreadPoolWrap; +import com.github.dynamic.threadpool.starter.wrap.DynamicThreadPoolWrapper; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import lombok.var; @@ -55,18 +54,18 @@ public final class DynamicThreadPoolPostProcessor implements BeanPostProcessor { @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { - if (bean instanceof CustomThreadPoolExecutor) { + if (bean instanceof DynamicThreadPoolExecutor) { var dynamicThreadPool = ApplicationContextHolder.findAnnotationOnBean(beanName, DynamicThreadPool.class); if (Objects.isNull(dynamicThreadPool)) { return bean; } - var customExecutor = (CustomThreadPoolExecutor) bean; - var wrap = new DynamicThreadPoolWrap(customExecutor.getThreadPoolId(), customExecutor); - CustomThreadPoolExecutor remoteExecutor = fillPoolAndRegister(wrap); + var dynamicExecutor = (DynamicThreadPoolExecutor) bean; + var wrap = new DynamicThreadPoolWrapper(dynamicExecutor.getThreadPoolId(), dynamicExecutor); + var remoteExecutor = fillPoolAndRegister(wrap); subscribeConfig(wrap); return remoteExecutor; - } else if (bean instanceof DynamicThreadPoolWrap) { - var wrap = (DynamicThreadPoolWrap) bean; + } else if (bean instanceof DynamicThreadPoolWrapper) { + var wrap = (DynamicThreadPoolWrapper) bean; registerAndSubscribe(wrap); } @@ -78,9 +77,11 @@ public final class DynamicThreadPoolPostProcessor implements BeanPostProcessor { * * @param dynamicThreadPoolWrap */ - protected void registerAndSubscribe(DynamicThreadPoolWrap dynamicThreadPoolWrap) { - fillPoolAndRegister(dynamicThreadPoolWrap); - subscribeConfig(dynamicThreadPoolWrap); + protected void registerAndSubscribe(DynamicThreadPoolWrapper dynamicThreadPoolWrap) { + executorService.execute(() -> { + fillPoolAndRegister(dynamicThreadPoolWrap); + subscribeConfig(dynamicThreadPoolWrap); + }); } /** @@ -88,7 +89,7 @@ public final class DynamicThreadPoolPostProcessor implements BeanPostProcessor { * * @param dynamicThreadPoolWrap */ - protected CustomThreadPoolExecutor fillPoolAndRegister(DynamicThreadPoolWrap dynamicThreadPoolWrap) { + protected DynamicThreadPoolExecutor fillPoolAndRegister(DynamicThreadPoolWrapper dynamicThreadPoolWrap) { String tpId = dynamicThreadPoolWrap.getTpId(); Map queryStrMap = new HashMap(3); queryStrMap.put(TP_ID, tpId); @@ -97,7 +98,7 @@ public final class DynamicThreadPoolPostProcessor implements BeanPostProcessor { Result result; boolean isSubscribe = false; - CustomThreadPoolExecutor poolExecutor = null; + DynamicThreadPoolExecutor poolExecutor = null; PoolParameterInfo ppi = new PoolParameterInfo(); try { @@ -105,10 +106,9 @@ public final class DynamicThreadPoolPostProcessor implements BeanPostProcessor { if (result.isSuccess() && result.getData() != null && (ppi = JSON.toJavaObject((JSON) result.getData(), PoolParameterInfo.class)) != null) { // 使用相关参数创建线程池 BlockingQueue workQueue = QueueTypeEnum.createBlockingQueue(ppi.getQueueType(), ppi.getCapacity()); - poolExecutor = (CustomThreadPoolExecutor) ThreadPoolBuilder.builder() - .isCustomPool(true) + poolExecutor = (DynamicThreadPoolExecutor) ThreadPoolBuilder.builder() + .dynamicPool() .workQueue(workQueue) - .threadPoolId(tpId) .threadFactory(tpId) .poolThreadSize(ppi.getCoreSize(), ppi.getMaxSize()) .keepAliveTime(ppi.getKeepAliveTime(), TimeUnit.SECONDS) @@ -120,13 +120,13 @@ public final class DynamicThreadPoolPostProcessor implements BeanPostProcessor { isSubscribe = true; } } catch (Exception ex) { - poolExecutor = dynamicThreadPoolWrap.getPool() != null ? dynamicThreadPoolWrap.getPool() : CommonThreadPool.getInstance(tpId); + poolExecutor = dynamicThreadPoolWrap.getPool() != null ? dynamicThreadPoolWrap.getPool() : CommonDynamicThreadPool.getInstance(tpId); dynamicThreadPoolWrap.setPool(poolExecutor); log.error("[Init pool] Failed to initialize thread pool configuration. error message :: {}", ex.getMessage()); } finally { if (Objects.isNull(dynamicThreadPoolWrap.getPool())) { - dynamicThreadPoolWrap.setPool(CommonThreadPool.getInstance(tpId)); + dynamicThreadPoolWrap.setPool(CommonDynamicThreadPool.getInstance(tpId)); } // 设置是否订阅远端线程池配置 @@ -137,7 +137,7 @@ public final class DynamicThreadPoolPostProcessor implements BeanPostProcessor { return poolExecutor; } - protected void subscribeConfig(DynamicThreadPoolWrap dynamicThreadPoolWrap) { + protected void subscribeConfig(DynamicThreadPoolWrapper dynamicThreadPoolWrap) { if (dynamicThreadPoolWrap.isSubscribeFlag()) { threadPoolOperation.subscribeConfig(dynamicThreadPoolWrap.getTpId(), executorService, config -> ThreadPoolDynamicRefresh.refreshDynamicPool(config)); } diff --git a/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/core/GlobalThreadPoolManage.java b/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/core/GlobalThreadPoolManage.java index 9f3670ba..a51ae9d8 100644 --- a/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/core/GlobalThreadPoolManage.java +++ b/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/core/GlobalThreadPoolManage.java @@ -1,6 +1,6 @@ package com.github.dynamic.threadpool.starter.core; -import com.github.dynamic.threadpool.starter.wrap.DynamicThreadPoolWrap; +import com.github.dynamic.threadpool.starter.wrap.DynamicThreadPoolWrapper; import com.github.dynamic.threadpool.common.model.PoolParameter; import java.util.Map; @@ -16,9 +16,9 @@ public class GlobalThreadPoolManage { private static final Map POOL_PARAMETER = new ConcurrentHashMap(); - private static final Map EXECUTOR_MAP = new ConcurrentHashMap(); + private static final Map EXECUTOR_MAP = new ConcurrentHashMap(); - public static DynamicThreadPoolWrap getExecutorService(String tpId) { + public static DynamicThreadPoolWrapper getExecutorService(String tpId) { return EXECUTOR_MAP.get(tpId); } @@ -26,12 +26,12 @@ public class GlobalThreadPoolManage { return POOL_PARAMETER.get(tpId); } - public static void register(String tpId, PoolParameter poolParameter, DynamicThreadPoolWrap executor) { + public static void register(String tpId, PoolParameter poolParameter, DynamicThreadPoolWrapper executor) { registerPool(tpId, executor); registerPoolParameter(tpId, poolParameter); } - public static void registerPool(String tpId, DynamicThreadPoolWrap executor) { + public static void registerPool(String tpId, DynamicThreadPoolWrapper executor) { EXECUTOR_MAP.put(tpId, executor); } diff --git a/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/handler/ThreadPoolRunStateHandler.java b/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/handler/ThreadPoolRunStateHandler.java index 09637ffc..128073eb 100644 --- a/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/handler/ThreadPoolRunStateHandler.java +++ b/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/handler/ThreadPoolRunStateHandler.java @@ -3,8 +3,8 @@ package com.github.dynamic.threadpool.starter.handler; import com.github.dynamic.threadpool.common.model.PoolRunStateInfo; import com.github.dynamic.threadpool.starter.core.GlobalThreadPoolManage; import com.github.dynamic.threadpool.starter.toolkit.CalculateUtil; -import com.github.dynamic.threadpool.starter.toolkit.thread.CustomThreadPoolExecutor; -import com.github.dynamic.threadpool.starter.wrap.DynamicThreadPoolWrap; +import com.github.dynamic.threadpool.starter.core.DynamicThreadPoolExecutor; +import com.github.dynamic.threadpool.starter.wrap.DynamicThreadPoolWrapper; import lombok.extern.slf4j.Slf4j; import java.net.InetAddress; @@ -32,7 +32,7 @@ public class ThreadPoolRunStateHandler { } public static PoolRunStateInfo getPoolRunState(String tpId) { - DynamicThreadPoolWrap executorService = GlobalThreadPoolManage.getExecutorService(tpId); + DynamicThreadPoolWrapper executorService = GlobalThreadPoolManage.getExecutorService(tpId); ThreadPoolExecutor pool = executorService.getPool(); // 核心线程数 @@ -78,8 +78,8 @@ public class ThreadPoolRunStateHandler { stateInfo.setHost(INET_ADDRESS.getHostAddress()); stateInfo.setTpId(tpId); - int rejectCount = pool instanceof CustomThreadPoolExecutor - ? ((CustomThreadPoolExecutor) pool).getRejectCount() + int rejectCount = pool instanceof DynamicThreadPoolExecutor + ? ((DynamicThreadPoolExecutor) pool).getRejectCount() : -1; stateInfo.setRejectCount(rejectCount); diff --git a/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/toolkit/thread/AbstractBuildThreadPoolTemplate.java b/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/toolkit/thread/AbstractBuildThreadPoolTemplate.java index 38a775ff..ef2192e8 100644 --- a/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/toolkit/thread/AbstractBuildThreadPoolTemplate.java +++ b/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/toolkit/thread/AbstractBuildThreadPoolTemplate.java @@ -2,6 +2,7 @@ package com.github.dynamic.threadpool.starter.toolkit.thread; import com.github.dynamic.threadpool.common.toolkit.Assert; import com.github.dynamic.threadpool.starter.alarm.ThreadPoolAlarm; +import com.github.dynamic.threadpool.starter.core.DynamicThreadPoolExecutor; import lombok.Data; import lombok.experimental.Accessors; import lombok.extern.slf4j.Slf4j; @@ -88,15 +89,15 @@ public class AbstractBuildThreadPoolTemplate { } /** - * 构建自定义线程池 + * 构建动态线程池 * * @param initParam * @return */ - public static CustomThreadPoolExecutor buildCustomPool(ThreadPoolInitParam initParam) { + public static DynamicThreadPoolExecutor buildDynamicPool(ThreadPoolInitParam initParam) { Assert.notNull(initParam); - CustomThreadPoolExecutor executorService = - new CustomThreadPoolExecutor(initParam.getCorePoolNum(), + DynamicThreadPoolExecutor executorService = + new DynamicThreadPoolExecutor(initParam.getCorePoolNum(), initParam.getMaxPoolNum(), initParam.getKeepAliveTime(), initParam.getTimeUnit(), diff --git a/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/toolkit/thread/ThreadPoolBuilder.java b/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/toolkit/thread/ThreadPoolBuilder.java index 134f662a..08abee34 100644 --- a/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/toolkit/thread/ThreadPoolBuilder.java +++ b/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/toolkit/thread/ThreadPoolBuilder.java @@ -22,9 +22,9 @@ public class ThreadPoolBuilder implements Builder { private boolean isFastPool; /** - * 是否自定义线程池 + * 是否动态线程池 */ - private boolean isCustomPool; + private boolean isDynamicPool; /** * 核心线程数量 @@ -111,8 +111,8 @@ public class ThreadPoolBuilder implements Builder { return this; } - public ThreadPoolBuilder isCustomPool(Boolean isCustomPool) { - this.isCustomPool = isCustomPool; + public ThreadPoolBuilder dynamicPool() { + this.isDynamicPool = true; return this; } @@ -204,8 +204,8 @@ public class ThreadPoolBuilder implements Builder { */ @Override public ThreadPoolExecutor build() { - if (isCustomPool) { - return buildCustomPool(this); + if (isDynamicPool) { + return buildDynamicPool(this); } return isFastPool ? buildFastPool(this) : buildPool(this); } @@ -240,13 +240,13 @@ public class ThreadPoolBuilder implements Builder { } /** - * 构建自定义线程池 + * 构建动态线程池 * * @param builder * @return */ - private static ThreadPoolExecutor buildCustomPool(ThreadPoolBuilder builder) { - return AbstractBuildThreadPoolTemplate.buildCustomPool(buildInitParam(builder)); + private static ThreadPoolExecutor buildDynamicPool(ThreadPoolBuilder builder) { + return AbstractBuildThreadPoolTemplate.buildDynamicPool(buildInitParam(builder)); } /** @@ -267,7 +267,7 @@ public class ThreadPoolBuilder implements Builder { .setRejectedExecutionHandler(builder.rejectedExecutionHandler) .setTimeUnit(builder.timeUnit); - if (builder.isCustomPool) { + if (builder.isDynamicPool) { String threadPoolId = Optional.ofNullable(builder.threadPoolId).orElse(builder.threadNamePrefix); initParam.setThreadPoolId(threadPoolId); ThreadPoolAlarm threadPoolAlarm = new ThreadPoolAlarm(builder.isAlarm, builder.capacityAlarm, builder.livenessAlarm); diff --git a/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/wrap/DynamicThreadPoolWrap.java b/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/wrap/DynamicThreadPoolWrapper.java similarity index 69% rename from dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/wrap/DynamicThreadPoolWrap.java rename to dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/wrap/DynamicThreadPoolWrapper.java index 571b4679..152706c0 100644 --- a/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/wrap/DynamicThreadPoolWrap.java +++ b/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/wrap/DynamicThreadPoolWrapper.java @@ -1,7 +1,7 @@ package com.github.dynamic.threadpool.starter.wrap; -import com.github.dynamic.threadpool.starter.common.CommonThreadPool; -import com.github.dynamic.threadpool.starter.toolkit.thread.CustomThreadPoolExecutor; +import com.github.dynamic.threadpool.starter.common.CommonDynamicThreadPool; +import com.github.dynamic.threadpool.starter.core.DynamicThreadPoolExecutor; import lombok.Data; import java.util.concurrent.Callable; @@ -14,7 +14,7 @@ import java.util.concurrent.Future; * @date 2021/6/20 16:55 */ @Data -public class DynamicThreadPoolWrap { +public class DynamicThreadPoolWrapper { private String tenantId; @@ -24,15 +24,15 @@ public class DynamicThreadPoolWrap { private boolean subscribeFlag; - private CustomThreadPoolExecutor pool; + private DynamicThreadPoolExecutor pool; /** - * 首选服务端线程池, 为空使用默认线程池 {@link CommonThreadPool#getInstance(String)} + * 首选服务端线程池, 为空使用默认线程池 {@link CommonDynamicThreadPool#getInstance(String)} * * @param threadPoolId */ - public DynamicThreadPoolWrap(String threadPoolId) { - this(threadPoolId, CommonThreadPool.getInstance(threadPoolId)); + public DynamicThreadPoolWrapper(String threadPoolId) { + this(threadPoolId, CommonDynamicThreadPool.getInstance(threadPoolId)); } /** @@ -41,7 +41,7 @@ public class DynamicThreadPoolWrap { * @param threadPoolId * @param threadPoolExecutor */ - public DynamicThreadPoolWrap(String threadPoolId, CustomThreadPoolExecutor threadPoolExecutor) { + public DynamicThreadPoolWrapper(String threadPoolId, DynamicThreadPoolExecutor threadPoolExecutor) { this.tpId = threadPoolId; this.pool = threadPoolExecutor; }