optimize code and comments

pull/1003/head
huangchengxing 3 years ago
parent 90aca00ba4
commit f09f76f34e

@ -37,6 +37,7 @@ import java.util.concurrent.BlockingQueue;
import java.util.concurrent.RejectedExecutionHandler; import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
/** /**
@ -46,10 +47,10 @@ import java.util.concurrent.atomic.AtomicLong;
public class DynamicThreadPoolExecutor extends ExtensibleThreadPoolExecutor implements DisposableBean { 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 final AtomicBoolean active;
private boolean isActive;
/** /**
* Wait for tasks to complete on shutdown * Wait for tasks to complete on shutdown
@ -98,12 +99,22 @@ public class DynamicThreadPoolExecutor extends ExtensibleThreadPoolExecutor impl
threadPoolId, new DefaultThreadPoolPluginManager().setPluginComparator(AnnotationAwareOrderComparator.INSTANCE), threadPoolId, new DefaultThreadPoolPluginManager().setPluginComparator(AnnotationAwareOrderComparator.INSTANCE),
corePoolSize, maximumPoolSize, keepAliveTime, unit, corePoolSize, maximumPoolSize, keepAliveTime, unit,
blockingQueue, threadFactory, rejectedExecutionHandler); blockingQueue, threadFactory, rejectedExecutionHandler);
log.info("Initializing ExecutorService {}", threadPoolId); log.info("Initializing ExecutorService '{}'", threadPoolId);
this.waitForTasksToCompleteOnShutdown = waitForTasksToCompleteOnShutdown; this.waitForTasksToCompleteOnShutdown = waitForTasksToCompleteOnShutdown;
// Init default plugins. // Init default plugins.
new DefaultThreadPoolPluginRegistrar(executeTimeOut, awaitTerminationMillis) new DefaultThreadPoolPluginRegistrar(executeTimeOut, awaitTerminationMillis)
.doRegister(this); .doRegister(this);
this.isActive = true; this.active = new AtomicBoolean(true);
}
/**
* <p>Whether the current instance is in the active state. <br />
* 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 @Override
public void destroy() { public void destroy() {
// instance has been destroyed, not need to call this method again // 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()); log.warn("Failed to destroy ExecutorService '{}' because it has already been destroyed", getThreadPoolId());
return; return;
} }
@ -125,7 +136,7 @@ public class DynamicThreadPoolExecutor extends ExtensibleThreadPoolExecutor impl
log.info("ExecutorService '{}' has been destroyed", getThreadPoolId()); log.info("ExecutorService '{}' has been destroyed", getThreadPoolId());
// modify the flag to false avoid the method being called repeatedly // modify the flag to false avoid the method being called repeatedly
isActive = false; active.set(false);
} }
/** /**

@ -69,6 +69,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.info.BuildProperties; import org.springframework.boot.info.BuildProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
@ -122,8 +123,8 @@ public class DynamicThreadPoolAutoConfiguration {
} }
@Bean @Bean
public AdaptedThreadPoolDestroyPostProcessor adaptedThreadPoolDestroyPostProcessor() { public AdaptedThreadPoolDestroyPostProcessor adaptedThreadPoolDestroyPostProcessor(ApplicationContext applicationContext) {
return new AdaptedThreadPoolDestroyPostProcessor(); return new AdaptedThreadPoolDestroyPostProcessor(applicationContext);
} }
@Bean @Bean

@ -17,15 +17,16 @@
package cn.hippo4j.springboot.starter.support; package cn.hippo4j.springboot.starter.support;
import cn.hippo4j.common.config.ApplicationContextHolder;
import cn.hippo4j.core.executor.DynamicThreadPoolExecutor; import cn.hippo4j.core.executor.DynamicThreadPoolExecutor;
import cn.hippo4j.core.executor.DynamicThreadPoolWrapper; import cn.hippo4j.core.executor.DynamicThreadPoolWrapper;
import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage; import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage;
import cn.hippo4j.core.executor.support.adpter.DynamicThreadPoolAdapter; import cn.hippo4j.core.executor.support.adpter.DynamicThreadPoolAdapter;
import cn.hippo4j.core.executor.support.adpter.DynamicThreadPoolAdapterChoose; import cn.hippo4j.core.executor.support.adpter.DynamicThreadPoolAdapterChoose;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor; import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
import org.springframework.context.ApplicationContext;
import java.util.Optional; import java.util.Optional;
@ -36,9 +37,15 @@ import java.util.Optional;
* *
* @see DynamicThreadPoolAdapter * @see DynamicThreadPoolAdapter
*/ */
@RequiredArgsConstructor
@Slf4j @Slf4j
public class AdaptedThreadPoolDestroyPostProcessor implements DestructionAwareBeanPostProcessor { public class AdaptedThreadPoolDestroyPostProcessor implements DestructionAwareBeanPostProcessor {
/**
* Application context.
*/
private final ApplicationContext applicationContext;
/** /**
* If {@link DynamicThreadPoolAdapterChoose#match} method returns true, * If {@link DynamicThreadPoolAdapterChoose#match} method returns true,
* try to destroy its internal {@link DynamicThreadPoolExecutor} instance. * try to destroy its internal {@link DynamicThreadPoolExecutor} instance.
@ -66,19 +73,19 @@ public class AdaptedThreadPoolDestroyPostProcessor implements DestructionAwareBe
Optional.ofNullable(DynamicThreadPoolAdapterChoose.unwrap(bean)) Optional.ofNullable(DynamicThreadPoolAdapterChoose.unwrap(bean))
.map(DynamicThreadPoolExecutor::getThreadPoolId) .map(DynamicThreadPoolExecutor::getThreadPoolId)
// the internal thread pool is also managed by spring, no manual destruction required // 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) .map(GlobalThreadPoolManage::getExecutorService)
.ifPresent(executor -> destroyAdaptedThreadPoolExecutor(beanName, executor)); .ifPresent(executor -> destroyAdaptedThreadPoolExecutor(beanName, executor));
} }
private static void destroyAdaptedThreadPoolExecutor(String beanName, DynamicThreadPoolWrapper executor) { private void destroyAdaptedThreadPoolExecutor(String beanName, DynamicThreadPoolWrapper executor) {
try { try {
if (log.isDebugEnabled()) { 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(); executor.destroy();
} catch (Exception e) { } 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);
} }
} }
} }

Loading…
Cancel
Save