From 74da2fbde95f9132ce8d843f0b60d102f1df3919 Mon Sep 17 00:00:00 2001 From: liuzhigang Date: Thu, 14 Sep 2023 15:58:55 +0800 Subject: [PATCH] fix(Optimizing Code Writing): Optimizing Code Writing Optimizing Code Writing --- .../DynamicThreadPoolPostProcessor.java | 25 +++++++++++-------- .../DynamicThreadPoolPostProcessor.java | 23 +++++++++-------- 2 files changed, 27 insertions(+), 21 deletions(-) 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 44264686..586daf16 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 @@ -61,15 +61,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); @@ -79,7 +78,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; @@ -168,7 +168,8 @@ 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); executor.setCorePoolSize(executorProperties.getCorePoolSize()); executor.setMaximumPoolSize(executorProperties.getMaximumPoolSize()); @@ -205,7 +206,8 @@ public final class DynamicThreadPoolPostProcessor implements BeanPostProcessor { .orElseGet(() -> Optional.ofNullable(configProperties.getDefaultExecutor()).map(each -> each.getQueueCapacity()).get())) .rejectedHandler(Optional.ofNullable(executorProperties.getRejectedHandler()) .orElseGet(() -> Optional.ofNullable(configProperties.getDefaultExecutor()).map(each -> each.getRejectedHandler()).get())) - .threadNamePrefix(StringUtil.isBlank(executorProperties.getThreadNamePrefix()) ? executorProperties.getThreadPoolId() : executorProperties.getThreadNamePrefix()) + .threadNamePrefix(StringUtil.isBlank(executorProperties.getThreadNamePrefix()) ? + executorProperties.getThreadPoolId() : executorProperties.getThreadNamePrefix()) .threadPoolId(executorProperties.getThreadPoolId()) .build(); return newExecutorProperties; @@ -218,7 +220,8 @@ public final class DynamicThreadPoolPostProcessor implements BeanPostProcessor { * @return thread-pool notify alarm */ private ThreadPoolNotifyAlarm buildThreadPoolNotifyAlarm(ExecutorProperties executorProperties) { - DynamicThreadPoolNotifyProperties notify = Optional.ofNullable(executorProperties).map(ExecutorProperties::getNotify).orElse(null); + DynamicThreadPoolNotifyProperties 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()) 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 3362deb0..08d52c7a 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 @@ -78,15 +78,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); @@ -96,7 +95,9 @@ public final class DynamicThreadPoolPostProcessor implements BeanPostProcessor { if ((dynamicThreadPoolExecutor = DynamicThreadPoolAdapterChoose.unwrap(bean)) == null) { dynamicThreadPoolExecutor = (DynamicThreadPoolExecutor) bean; } - DynamicThreadPoolWrapper dynamicThreadPoolWrapper = new DynamicThreadPoolWrapper(dynamicThreadPoolExecutor.getThreadPoolId(), dynamicThreadPoolExecutor); + DynamicThreadPoolWrapper dynamicThreadPoolWrapper = + new DynamicThreadPoolWrapper(dynamicThreadPoolExecutor.getThreadPoolId(), + dynamicThreadPoolExecutor); ThreadPoolExecutor remoteThreadPoolExecutor = fillPoolAndRegister(dynamicThreadPoolWrapper); DynamicThreadPoolAdapterChoose.replace(bean, remoteThreadPoolExecutor); subscribeConfig(dynamicThreadPoolWrapper); @@ -165,7 +166,8 @@ public final class DynamicThreadPoolPostProcessor implements BeanPostProcessor { } catch (Exception ex) { log.error("Failed to initialize thread pool configuration. error message: {}", ex.getMessage()); } - GlobalThreadPoolManage.register(dynamicThreadPoolWrapper.getThreadPoolId(), threadPoolParameterInfo, dynamicThreadPoolWrapper); + GlobalThreadPoolManage.register(dynamicThreadPoolWrapper.getThreadPoolId(), threadPoolParameterInfo, + dynamicThreadPoolWrapper); return executor; } @@ -176,7 +178,8 @@ public final class DynamicThreadPoolPostProcessor implements BeanPostProcessor { * @param threadPoolParameterInfo thread-pool parameter info */ private void threadPoolParamReplace(ThreadPoolExecutor executor, ThreadPoolParameterInfo threadPoolParameterInfo) { - BlockingQueue workQueue = BlockingQueueTypeEnum.createBlockingQueue(threadPoolParameterInfo.getQueueType(), threadPoolParameterInfo.getCapacity()); + BlockingQueue workQueue = BlockingQueueTypeEnum.createBlockingQueue(threadPoolParameterInfo.getQueueType(), + threadPoolParameterInfo.getCapacity()); ReflectUtil.setFieldValue(executor, "workQueue", workQueue); executor.setCorePoolSize(threadPoolParameterInfo.corePoolSizeAdapt()); executor.setMaximumPoolSize(threadPoolParameterInfo.maximumPoolSizeAdapt());