diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/DynamicThreadPoolAutoConfiguration.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/DynamicThreadPoolAutoConfiguration.java index 0d748f7ecce1b3dd53c8ad19d7e299e9183299e1..e37b4389e874c8743de4b044f9525fbaf931d62d 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/DynamicThreadPoolAutoConfiguration.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/DynamicThreadPoolAutoConfiguration.java @@ -112,7 +112,7 @@ public class DynamicThreadPoolAutoConfiguration { } @Bean - public DynamicThreadPoolPostProcessor dynamicThreadPoolPostProcessor(ApplicationContextHolder hippo4jApplicationContextHolder) { + public DynamicThreadPoolPostProcessor dynamicThreadPoolPostProcessor() { return new DynamicThreadPoolPostProcessor(bootstrapConfigProperties); } diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/support/DynamicThreadPoolPostProcessor.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/support/DynamicThreadPoolPostProcessor.java index 70137141824f1c0679d179967dad5463e9767293..bfe3af256365a3a45c781bb8a39b01ac07e31618 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/support/DynamicThreadPoolPostProcessor.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/support/DynamicThreadPoolPostProcessor.java @@ -62,15 +62,14 @@ public final class DynamicThreadPoolPostProcessor implements BeanPostProcessor { @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof DynamicThreadPoolExecutor || DynamicThreadPoolAdapterChoose.match(bean)) { - DynamicThreadPool dynamicThreadPool; try { - dynamicThreadPool = ApplicationContextHolder.findAnnotationOnBean(beanName, DynamicThreadPool.class); + DynamicThreadPool dynamicThreadPool = + Optional.ofNullable(ApplicationContextHolder.findAnnotationOnBean(beanName, + DynamicThreadPool.class)) + .orElse(DynamicThreadPoolAnnotationUtil.findAnnotationOnBean(beanName, + DynamicThreadPool.class)); if (Objects.isNull(dynamicThreadPool)) { - // Adapt to lower versions of SpringBoot. - dynamicThreadPool = DynamicThreadPoolAnnotationUtil.findAnnotationOnBean(beanName, DynamicThreadPool.class); - if (Objects.isNull(dynamicThreadPool)) { - return bean; - } + return bean; } } catch (Exception ex) { log.error("Failed to create dynamic thread pool in annotation mode.", ex); @@ -80,7 +79,8 @@ public final class DynamicThreadPoolPostProcessor implements BeanPostProcessor { if ((dynamicThreadPoolExecutor = DynamicThreadPoolAdapterChoose.unwrap(bean)) == null) { dynamicThreadPoolExecutor = (DynamicThreadPoolExecutor) bean; } - DynamicThreadPoolWrapper wrap = new DynamicThreadPoolWrapper(dynamicThreadPoolExecutor.getThreadPoolId(), dynamicThreadPoolExecutor); + DynamicThreadPoolWrapper wrap = new DynamicThreadPoolWrapper(dynamicThreadPoolExecutor.getThreadPoolId(), + dynamicThreadPoolExecutor); ThreadPoolExecutor remoteThreadPoolExecutor = fillPoolAndRegister(wrap); DynamicThreadPoolAdapterChoose.replace(bean, remoteThreadPoolExecutor); return DynamicThreadPoolAdapterChoose.match(bean) ? bean : remoteThreadPoolExecutor; @@ -170,10 +170,12 @@ public final class DynamicThreadPoolPostProcessor implements BeanPostProcessor { * @param executorProperties executor properties */ private void threadPoolParamReplace(ThreadPoolExecutor executor, ExecutorProperties executorProperties) { - BlockingQueue workQueue = BlockingQueueTypeEnum.createBlockingQueue(executorProperties.getBlockingQueue(), executorProperties.getQueueCapacity()); + BlockingQueue workQueue = BlockingQueueTypeEnum.createBlockingQueue(executorProperties.getBlockingQueue(), + executorProperties.getQueueCapacity()); ReflectUtil.setFieldValue(executor, "workQueue", workQueue); // fix https://github.com/opengoofy/hippo4j/issues/1063 - ThreadPoolExecutorUtil.safeSetPoolSize(executor, executorProperties.getCorePoolSize(), executorProperties.getMaximumPoolSize()); + ThreadPoolExecutorUtil.safeSetPoolSize(executor, executorProperties.getCorePoolSize(), + executorProperties.getMaximumPoolSize()); executor.setKeepAliveTime(executorProperties.getKeepAliveTime(), TimeUnit.SECONDS); executor.allowCoreThreadTimeOut(executorProperties.getAllowCoreThreadTimeOut()); executor.setRejectedExecutionHandler(RejectedPolicyTypeEnum.createPolicy(executorProperties.getRejectedHandler())); @@ -207,7 +209,8 @@ public final class DynamicThreadPoolPostProcessor implements BeanPostProcessor { .orElseGet(() -> Optional.ofNullable(configProperties.getDefaultExecutor()).map(ExecutorProperties::getQueueCapacity).get())) .rejectedHandler(Optional.ofNullable(executorProperties.getRejectedHandler()) .orElseGet(() -> Optional.ofNullable(configProperties.getDefaultExecutor()).map(ExecutorProperties::getRejectedHandler).get())) - .threadNamePrefix(StringUtil.isBlank(executorProperties.getThreadNamePrefix()) ? executorProperties.getThreadPoolId() : executorProperties.getThreadNamePrefix()) + .threadNamePrefix(StringUtil.isBlank(executorProperties.getThreadNamePrefix()) ? + executorProperties.getThreadPoolId() : executorProperties.getThreadNamePrefix()) .threadPoolId(executorProperties.getThreadPoolId()) .alarm(Optional.ofNullable(executorProperties.getAlarm()) .orElseGet(() -> Optional.ofNullable(configProperties.getDefaultExecutor()).map(ExecutorProperties::getAlarm).orElse(null))) @@ -229,7 +232,8 @@ public final class DynamicThreadPoolPostProcessor implements BeanPostProcessor { * @return thread-pool notify alarm */ private ThreadPoolNotifyAlarm buildThreadPoolNotifyAlarm(ExecutorProperties executorProperties) { - ExecutorNotifyProperties notify = Optional.ofNullable(executorProperties).map(ExecutorProperties::getNotify).orElse(null); + ExecutorNotifyProperties notify = + Optional.ofNullable(executorProperties).map(ExecutorProperties::getNotify).orElse(null); boolean isAlarm = Optional.ofNullable(executorProperties.getAlarm()) .orElseGet(() -> Optional.ofNullable(configProperties.getDefaultExecutor()).map(ExecutorProperties::getAlarm).orElse(true)); int activeAlarm = Optional.ofNullable(executorProperties.getActiveAlarm())