Refactor ThreadPoolInstanceRegistry to ThreadPoolRegistry. (#1339)

* Refactor ThreadPoolRegistry.

* Refactor ThreadPoolInstance Registry to ThreadPoolRegistry.

---------

Co-authored-by: lucca <luccasuen.dev@gmail.com>
pull/1342/head
lucca suen 2 years ago committed by GitHub
parent 618b382401
commit f46da2b3d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -18,7 +18,7 @@
package cn.hippo4j.agent.plugin.spring.common.support; package cn.hippo4j.agent.plugin.spring.common.support;
import cn.hippo4j.agent.core.util.ReflectUtil; import cn.hippo4j.agent.core.util.ReflectUtil;
import cn.hippo4j.common.executor.ThreadPoolInstanceRegistry; import cn.hippo4j.common.executor.ThreadPoolRegistry;
import cn.hippo4j.common.executor.support.BlockingQueueTypeEnum; import cn.hippo4j.common.executor.support.BlockingQueueTypeEnum;
import cn.hippo4j.common.executor.support.RejectedPolicyTypeEnum; import cn.hippo4j.common.executor.support.RejectedPolicyTypeEnum;
import cn.hippo4j.common.model.executor.ExecutorProperties; import cn.hippo4j.common.model.executor.ExecutorProperties;
@ -41,8 +41,8 @@ public class SpringThreadPoolRegisterSupport {
private static final Logger LOGGER = LoggerFactory.getLogger(SpringThreadPoolRegisterSupport.class); private static final Logger LOGGER = LoggerFactory.getLogger(SpringThreadPoolRegisterSupport.class);
public static void registerThreadPoolInstances(ApplicationContext context) { public static void registerThreadPoolInstances(ApplicationContext context) {
Map<ThreadPoolExecutor, Class<?>> earlyConstructMap = ThreadPoolInstanceRegistry.getInstance().earlyConstructMap; Map<ThreadPoolExecutor, Class<?>> referencedClassMap = ThreadPoolRegistry.REFERENCED_CLASS_MAP;
for (Map.Entry<ThreadPoolExecutor, Class<?>> entry : earlyConstructMap.entrySet()) { for (Map.Entry<ThreadPoolExecutor, Class<?>> entry : referencedClassMap.entrySet()) {
ThreadPoolExecutor enhancedInstance = entry.getKey(); ThreadPoolExecutor enhancedInstance = entry.getKey();
Class<?> declaredClass = entry.getValue(); Class<?> declaredClass = entry.getValue();
List<Field> declaredFields = ReflectUtil.getStaticFieldsFromType(declaredClass, ThreadPoolExecutor.class); List<Field> declaredFields = ReflectUtil.getStaticFieldsFromType(declaredClass, ThreadPoolExecutor.class);
@ -93,6 +93,6 @@ public class SpringThreadPoolRegisterSupport {
.queueCapacity(executor.getQueue().remainingCapacity()) .queueCapacity(executor.getQueue().remainingCapacity())
.rejectedHandler(RejectedPolicyTypeEnum.getRejectedPolicyTypeEnumByName(executor.getRejectedExecutionHandler().getClass().getSimpleName()).getName()) .rejectedHandler(RejectedPolicyTypeEnum.getRejectedPolicyTypeEnumByName(executor.getRejectedExecutionHandler().getClass().getSimpleName()).getName())
.build(); .build();
ThreadPoolInstanceRegistry.getInstance().putHolder(threadPoolId, executor, executorProperties); ThreadPoolRegistry.putHolder(threadPoolId, executor, executorProperties);
} }
} }

@ -22,7 +22,7 @@ import cn.hippo4j.agent.core.logging.api.ILog;
import cn.hippo4j.agent.core.logging.api.LogManager; import cn.hippo4j.agent.core.logging.api.LogManager;
import cn.hippo4j.agent.core.plugin.interceptor.enhance.EnhancedInstance; import cn.hippo4j.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import cn.hippo4j.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor; import cn.hippo4j.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor;
import cn.hippo4j.common.executor.ThreadPoolInstanceRegistry; import cn.hippo4j.common.executor.ThreadPoolRegistry;
import cn.hippo4j.agent.core.util.CollectionUtil; import cn.hippo4j.agent.core.util.CollectionUtil;
import cn.hippo4j.agent.core.util.StringUtil; import cn.hippo4j.agent.core.util.StringUtil;
import java.util.ArrayList; import java.util.ArrayList;
@ -52,7 +52,7 @@ public class ThreadPoolExecutorConstructorMethodInterceptor implements InstanceC
StackTraceElement declaredClassStackTraceElement = stackTraceElements.get(0); StackTraceElement declaredClassStackTraceElement = stackTraceElements.get(0);
String declaredClassName = declaredClassStackTraceElement.getClassName(); String declaredClassName = declaredClassStackTraceElement.getClassName();
Class<?> declaredClass = Thread.currentThread().getContextClassLoader().loadClass(declaredClassName); Class<?> declaredClass = Thread.currentThread().getContextClassLoader().loadClass(declaredClassName);
ThreadPoolInstanceRegistry.getInstance().earlyConstructMap.put((ThreadPoolExecutor) objInst, declaredClass); ThreadPoolRegistry.REFERENCED_CLASS_MAP.put((ThreadPoolExecutor) objInst, declaredClass);
} }
private List<StackTraceElement> getStackTraceElements() { private List<StackTraceElement> getStackTraceElements() {

@ -27,35 +27,26 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor;
@NoArgsConstructor(access = AccessLevel.PRIVATE) @NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ThreadPoolInstanceRegistry { public class ThreadPoolRegistry {
private final Map<String, ThreadPoolExecutorHolder> holderMap = new ConcurrentHashMap<>(); private static final Map<String, ThreadPoolExecutorHolder> HOLDER_MAP = new ConcurrentHashMap<>();
public final Map<ThreadPoolExecutor, Class<?>> earlyConstructMap = new ConcurrentHashMap<>(); public static final Map<ThreadPoolExecutor, Class<?>> REFERENCED_CLASS_MAP = new ConcurrentHashMap<>();
private volatile static ThreadPoolInstanceRegistry INSTANCE; public static Map<String, ThreadPoolExecutorHolder> getHolderMap() {
return HOLDER_MAP;
public static ThreadPoolInstanceRegistry getInstance() {
if (INSTANCE == null) {
synchronized (ThreadPoolInstanceRegistry.class) {
if (INSTANCE == null) {
INSTANCE = new ThreadPoolInstanceRegistry();
}
}
}
return INSTANCE;
} }
public Map<String, ThreadPoolExecutorHolder> getHolderMap() { public static void putHolder(String executorName, ThreadPoolExecutor executor, ExecutorProperties executorProperties) {
return holderMap; ThreadPoolExecutorHolder holder = new ThreadPoolExecutorHolder(executorName, executor, executorProperties);
HOLDER_MAP.put(executorName, holder);
} }
public void putHolder(String executorName, ThreadPoolExecutor executor, ExecutorProperties executorProperties) { public static ThreadPoolExecutorHolder getHolder(String executorName) {
ThreadPoolExecutorHolder holder = new ThreadPoolExecutorHolder(executorName, executor, executorProperties); return Optional.ofNullable(HOLDER_MAP.get(executorName)).orElse(ThreadPoolExecutorHolder.EMPTY);
holderMap.put(executorName, holder);
} }
public ThreadPoolExecutorHolder getHolder(String executorName) { public static Map<ThreadPoolExecutor, Class<?>> getReferencedClassMap() {
return Optional.ofNullable(holderMap.get(executorName)).orElse(ThreadPoolExecutorHolder.EMPTY); return REFERENCED_CLASS_MAP;
} }
} }

@ -18,7 +18,7 @@
package cn.hippo4j.threadpool.dynamic.mode.config.refresher.event; package cn.hippo4j.threadpool.dynamic.mode.config.refresher.event;
import cn.hippo4j.common.executor.ThreadPoolExecutorHolder; import cn.hippo4j.common.executor.ThreadPoolExecutorHolder;
import cn.hippo4j.common.executor.ThreadPoolInstanceRegistry; import cn.hippo4j.common.executor.ThreadPoolRegistry;
import cn.hippo4j.common.executor.support.BlockingQueueTypeEnum; import cn.hippo4j.common.executor.support.BlockingQueueTypeEnum;
import cn.hippo4j.common.executor.support.RejectedPolicyTypeEnum; import cn.hippo4j.common.executor.support.RejectedPolicyTypeEnum;
import cn.hippo4j.common.executor.support.ResizableCapacityLinkedBlockingQueue; import cn.hippo4j.common.executor.support.ResizableCapacityLinkedBlockingQueue;
@ -55,7 +55,7 @@ public class DynamicThreadPoolRefreshListener implements Observer<BootstrapConfi
for (ExecutorProperties properties : executors) { for (ExecutorProperties properties : executors) {
String threadPoolId = properties.getThreadPoolId(); String threadPoolId = properties.getThreadPoolId();
// Check whether the thread pool configuration is empty and whether the parameters have changed // Check whether the thread pool configuration is empty and whether the parameters have changed
ThreadPoolExecutorHolder executorHolder = ThreadPoolInstanceRegistry.getInstance().getHolder(threadPoolId); ThreadPoolExecutorHolder executorHolder = ThreadPoolRegistry.getHolder(threadPoolId);
if (executorHolder.isEmpty() || !checkPropertiesConsistency(executorHolder, properties)) { if (executorHolder.isEmpty() || !checkPropertiesConsistency(executorHolder, properties)) {
continue; continue;
} }

Loading…
Cancel
Save