From 7e2933f3dd96dfa858cf82b37101bb634a556c8f Mon Sep 17 00:00:00 2001 From: "chen.ma" Date: Fri, 5 Aug 2022 23:36:18 +0800 Subject: [PATCH] Optimize thread pool monitoring task assembly --- .../monitor/ReportingEventExecutor.java | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/monitor/ReportingEventExecutor.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/monitor/ReportingEventExecutor.java index 5027f070..7aa90c35 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/monitor/ReportingEventExecutor.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/monitor/ReportingEventExecutor.java @@ -19,6 +19,7 @@ package cn.hippo4j.springboot.starter.monitor; import cn.hippo4j.common.config.ApplicationContextHolder; import cn.hippo4j.common.monitor.Message; +import cn.hippo4j.common.toolkit.CollectionUtil; import cn.hippo4j.common.toolkit.ThreadUtil; import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage; import cn.hippo4j.core.executor.support.ThreadFactoryBuilder; @@ -40,6 +41,7 @@ import org.springframework.beans.factory.DisposableBean; import org.springframework.boot.CommandLineRunner; import java.util.Collection; +import java.util.List; import java.util.Map; import java.util.Optional; import java.util.concurrent.ArrayBlockingQueue; @@ -78,7 +80,7 @@ public class ReportingEventExecutor implements Runnable, CommandLineRunner, Disp /** * Thread pool monitoring collection. */ - private Map threadPoolMonitors; + private List threadPoolMonitors; /** * Buffer container for data collection, waiting @@ -114,14 +116,18 @@ public class ReportingEventExecutor implements Runnable, CommandLineRunner, Disp collectVesselExecutor = new ScheduledThreadPoolExecutor( new Integer(collectType.split(",").length), ThreadFactoryBuilder.builder().daemon(true).prefix("client.scheduled.collect.data").build()); - if (collectType.contains(MonitorTypeEnum.PROMETHEUS.name().toLowerCase()) - || collectType.contains(MonitorTypeEnum.LOG.name().toLowerCase()) - || collectType.contains(MonitorTypeEnum.ES.name().toLowerCase())) { + Collection dynamicThreadPoolMonitors = + DynamicThreadPoolServiceLoader.getSingletonServiceInstances(DynamicThreadPoolMonitor.class); + boolean customerDynamicThreadPoolMonitorFlag = CollectionUtil.isNotEmpty(dynamicThreadPoolMonitors) || ( + collectType.contains(MonitorTypeEnum.PROMETHEUS.name().toLowerCase()) + || collectType.contains(MonitorTypeEnum.LOG.name().toLowerCase()) + || collectType.contains(MonitorTypeEnum.ES.name().toLowerCase()) + ); + if (customerDynamicThreadPoolMonitorFlag) { // Get all dynamic thread pool monitoring components. - threadPoolMonitors = ApplicationContextHolder.getBeansOfType(ThreadPoolMonitor.class); - Collection dynamicThreadPoolMonitors = - DynamicThreadPoolServiceLoader.getSingletonServiceInstances(DynamicThreadPoolMonitor.class); - dynamicThreadPoolMonitors.stream().filter(each -> collectType.contains(each.getType())).forEach(each -> threadPoolMonitors.put(each.getType(), each)); + Map threadPoolMonitorMap = ApplicationContextHolder.getBeansOfType(ThreadPoolMonitor.class); + threadPoolMonitorMap.forEach((beanName, monitor) -> threadPoolMonitors.add(monitor)); + dynamicThreadPoolMonitors.stream().filter(each -> collectType.contains(each.getType())).forEach(each -> threadPoolMonitors.add(each)); collectVesselExecutor.scheduleWithFixedDelay( () -> dynamicThreadPoolMonitor(), properties.getInitialDelay(), @@ -151,8 +157,11 @@ public class ReportingEventExecutor implements Runnable, CommandLineRunner, Disp Optional.ofNullable(collectVesselExecutor).ifPresent((each) -> each.shutdown()); } + /** + * Running dynamic thread pool monitoring. + */ private void dynamicThreadPoolMonitor() { - threadPoolMonitors.forEach((beanName, monitor) -> monitor.collect()); + threadPoolMonitors.forEach(each -> each.collect()); } /**