From fbbb1765221a7b169795f8c08a12857972bbf560 Mon Sep 17 00:00:00 2001 From: airoger Date: Fri, 2 Sep 2022 23:50:10 +0800 Subject: [PATCH] fix issue #558 --- .../support/BlockingQueueTypeEnum.java | 7 ++++++ .../DynamicThreadPoolPostProcessor.java | 22 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/executor/support/BlockingQueueTypeEnum.java b/hippo4j-common/src/main/java/cn/hippo4j/common/executor/support/BlockingQueueTypeEnum.java index a2c29693..414f0c11 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/executor/support/BlockingQueueTypeEnum.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/executor/support/BlockingQueueTypeEnum.java @@ -145,4 +145,11 @@ public enum BlockingQueueTypeEnum { .findFirst(); return queueTypeEnum.map(each -> each.name).orElse(""); } + + public static BlockingQueueTypeEnum getBlockingQueueTypeEnumByName(String name){ + Optional queueTypeEnum = Arrays.stream(BlockingQueueTypeEnum.values()) + .filter(each -> each.name.equals(name)) + .findFirst(); + return queueTypeEnum.get(); + } } diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/support/DynamicThreadPoolPostProcessor.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/support/DynamicThreadPoolPostProcessor.java index 8c7e73e1..f3b33ba9 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/support/DynamicThreadPoolPostProcessor.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/support/DynamicThreadPoolPostProcessor.java @@ -23,6 +23,8 @@ import cn.hippo4j.common.enums.EnableEnum; import cn.hippo4j.common.executor.support.BlockingQueueTypeEnum; import cn.hippo4j.common.executor.support.RejectedPolicyTypeEnum; import cn.hippo4j.common.model.ThreadPoolParameterInfo; +import cn.hippo4j.common.model.register.DynamicThreadPoolRegisterParameter; +import cn.hippo4j.common.model.register.DynamicThreadPoolRegisterWrapper; import cn.hippo4j.common.toolkit.BooleanUtil; import cn.hippo4j.common.toolkit.JSONUtil; import cn.hippo4j.common.web.base.Result; @@ -43,12 +45,14 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.core.task.TaskDecorator; +import org.springframework.util.ClassUtils; import java.util.HashMap; import java.util.Map; import java.util.Objects; import java.util.Optional; import java.util.concurrent.BlockingQueue; +import java.util.concurrent.RejectedExecutionHandler; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; @@ -168,6 +172,24 @@ public final class DynamicThreadPoolPostProcessor implements BeanPostProcessor { dynamicThreadPoolWrapper.setExecutor(newDynamicThreadPoolExecutor); isSubscribe = true; } + }else { + // DynamicThreadPool configuration undefined in server + DynamicThreadPoolRegisterParameter parameterInfo = DynamicThreadPoolRegisterParameter.builder() + .threadPoolId(threadPoolId) + .threadNamePrefix(threadPoolId) + .corePoolSize(executor.getCorePoolSize()) + .maximumPoolSize(executor.getMaximumPoolSize()) + .blockingQueueType(BlockingQueueTypeEnum.getBlockingQueueTypeEnumByName(executor.getQueue().getClass().getSimpleName())) + .capacity(executor.getQueue().remainingCapacity()) + .allowCoreThreadTimeOut(executor.allowsCoreThreadTimeOut()) + .isAlarm(false) + //todo 如何根据拒绝策略的代理类获取到对应的枚举 + .rejectedPolicyType(RejectedPolicyTypeEnum.ABORT_POLICY) + .build(); + DynamicThreadPoolRegisterWrapper registerWrapper = DynamicThreadPoolRegisterWrapper.builder() + .dynamicThreadPoolRegisterParameter(parameterInfo) + .build(); + GlobalThreadPoolManage.dynamicRegister(registerWrapper); } } catch (Exception ex) { newDynamicThreadPoolExecutor = executor != null ? executor : CommonDynamicThreadPool.getInstance(threadPoolId);