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.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);
}
/**
* <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
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);
}
/**

@ -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

@ -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);
}
}
}

Loading…
Cancel
Save