From f09f76f34e3d5fdc141fa4314143035bc031ef56 Mon Sep 17 00:00:00 2001 From: huangchengxing <841396397@qq.com> Date: Tue, 22 Nov 2022 18:27:38 +0800 Subject: [PATCH] optimize code and comments --- .../executor/DynamicThreadPoolExecutor.java | 25 +++++++++++++------ .../DynamicThreadPoolAutoConfiguration.java | 5 ++-- ...AdaptedThreadPoolDestroyPostProcessor.java | 17 +++++++++---- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/hippo4j-core/src/main/java/cn/hippo4j/core/executor/DynamicThreadPoolExecutor.java b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/DynamicThreadPoolExecutor.java index d2a69252..a33c6b83 100644 --- a/hippo4j-core/src/main/java/cn/hippo4j/core/executor/DynamicThreadPoolExecutor.java +++ b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/DynamicThreadPoolExecutor.java @@ -37,6 +37,7 @@ import java.util.concurrent.BlockingQueue; import java.util.concurrent.RejectedExecutionHandler; import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicLong; /** @@ -46,10 +47,10 @@ import java.util.concurrent.atomic.AtomicLong; public class DynamicThreadPoolExecutor extends ExtensibleThreadPoolExecutor implements DisposableBean { /** - * Is active, it will become false only when destroy() is called. + * A flag used to indicate whether destroy() method has been called, + * after the flag is set to false, calling destroy() method again will not take effect */ - @Getter - private boolean isActive; + private final AtomicBoolean active; /** * Wait for tasks to complete on shutdown @@ -98,12 +99,22 @@ public class DynamicThreadPoolExecutor extends ExtensibleThreadPoolExecutor impl threadPoolId, new DefaultThreadPoolPluginManager().setPluginComparator(AnnotationAwareOrderComparator.INSTANCE), corePoolSize, maximumPoolSize, keepAliveTime, unit, blockingQueue, threadFactory, rejectedExecutionHandler); - log.info("Initializing ExecutorService {}", threadPoolId); + log.info("Initializing ExecutorService '{}'", threadPoolId); this.waitForTasksToCompleteOnShutdown = waitForTasksToCompleteOnShutdown; // Init default plugins. new DefaultThreadPoolPluginRegistrar(executeTimeOut, awaitTerminationMillis) .doRegister(this); - this.isActive = true; + this.active = new AtomicBoolean(true); + } + + /** + *

Whether the current instance is in the active state.
+ * It returns false when the xx method is called at least once. + * + * @return true if current instance is in the active state, false otherwise + */ + public boolean isActive() { + return active.get(); } /** @@ -112,7 +123,7 @@ public class DynamicThreadPoolExecutor extends ExtensibleThreadPoolExecutor impl @Override public void destroy() { // instance has been destroyed, not need to call this method again - if (!isActive) { + if (!isActive()) { log.warn("Failed to destroy ExecutorService '{}' because it has already been destroyed", getThreadPoolId()); return; } @@ -125,7 +136,7 @@ public class DynamicThreadPoolExecutor extends ExtensibleThreadPoolExecutor impl log.info("ExecutorService '{}' has been destroyed", getThreadPoolId()); // modify the flag to false avoid the method being called repeatedly - isActive = false; + active.set(false); } /** diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/config/DynamicThreadPoolAutoConfiguration.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/config/DynamicThreadPoolAutoConfiguration.java index 163b9bd5..c271fac4 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/config/DynamicThreadPoolAutoConfiguration.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/config/DynamicThreadPoolAutoConfiguration.java @@ -69,6 +69,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.info.BuildProperties; +import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.Ordered; @@ -122,8 +123,8 @@ public class DynamicThreadPoolAutoConfiguration { } @Bean - public AdaptedThreadPoolDestroyPostProcessor adaptedThreadPoolDestroyPostProcessor() { - return new AdaptedThreadPoolDestroyPostProcessor(); + public AdaptedThreadPoolDestroyPostProcessor adaptedThreadPoolDestroyPostProcessor(ApplicationContext applicationContext) { + return new AdaptedThreadPoolDestroyPostProcessor(applicationContext); } @Bean diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/support/AdaptedThreadPoolDestroyPostProcessor.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/support/AdaptedThreadPoolDestroyPostProcessor.java index 9aac3e36..61b8418d 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/support/AdaptedThreadPoolDestroyPostProcessor.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/support/AdaptedThreadPoolDestroyPostProcessor.java @@ -17,15 +17,16 @@ package cn.hippo4j.springboot.starter.support; -import cn.hippo4j.common.config.ApplicationContextHolder; import cn.hippo4j.core.executor.DynamicThreadPoolExecutor; import cn.hippo4j.core.executor.DynamicThreadPoolWrapper; import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage; import cn.hippo4j.core.executor.support.adpter.DynamicThreadPoolAdapter; import cn.hippo4j.core.executor.support.adpter.DynamicThreadPoolAdapterChoose; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor; +import org.springframework.context.ApplicationContext; import java.util.Optional; @@ -36,9 +37,15 @@ import java.util.Optional; * * @see DynamicThreadPoolAdapter */ +@RequiredArgsConstructor @Slf4j public class AdaptedThreadPoolDestroyPostProcessor implements DestructionAwareBeanPostProcessor { + /** + * Application context. + */ + private final ApplicationContext applicationContext; + /** * If {@link DynamicThreadPoolAdapterChoose#match} method returns true, * try to destroy its internal {@link DynamicThreadPoolExecutor} instance. @@ -66,19 +73,19 @@ public class AdaptedThreadPoolDestroyPostProcessor implements DestructionAwareBe Optional.ofNullable(DynamicThreadPoolAdapterChoose.unwrap(bean)) .map(DynamicThreadPoolExecutor::getThreadPoolId) // the internal thread pool is also managed by spring, no manual destruction required - .filter(id -> !ApplicationContextHolder.getInstance().containsBeanDefinition(id)) + .filter(applicationContext::containsBeanDefinition) .map(GlobalThreadPoolManage::getExecutorService) .ifPresent(executor -> destroyAdaptedThreadPoolExecutor(beanName, executor)); } - private static void destroyAdaptedThreadPoolExecutor(String beanName, DynamicThreadPoolWrapper executor) { + private void destroyAdaptedThreadPoolExecutor(String beanName, DynamicThreadPoolWrapper executor) { try { if (log.isDebugEnabled()) { - log.info("Destroy adapted dynamic thread pool '{}'", executor.getThreadPoolId()); + log.debug("Destroy internal dynamic thread pool '{}' for bean '{}'", executor.getThreadPoolId(), beanName); } executor.destroy(); } catch (Exception e) { - log.warn("Failed to destroy dynamic thread pool '{}'", beanName); + log.warn("Failed to destroy internal dynamic thread pool '{}' for bean '{}'", executor.getThreadPoolId(), beanName); } } }