From 4f36a3d9e3b79fbf08d8d37c0270a9978ddcf43e Mon Sep 17 00:00:00 2001 From: weihu Date: Wed, 9 Nov 2022 09:47:27 +0800 Subject: [PATCH] Simplify the code --- .../monitor/ThreadPoolMonitorExecutor.java | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/monitor/ThreadPoolMonitorExecutor.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/monitor/ThreadPoolMonitorExecutor.java index 286db868..04f2d938 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/monitor/ThreadPoolMonitorExecutor.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/monitor/ThreadPoolMonitorExecutor.java @@ -17,29 +17,30 @@ package cn.hippo4j.config.springboot.starter.monitor; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.Optional; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + import cn.hippo4j.common.config.ApplicationContextHolder; +import cn.hippo4j.common.design.builder.ThreadFactoryBuilder; import cn.hippo4j.common.spi.DynamicThreadPoolServiceLoader; import cn.hippo4j.common.toolkit.StringUtil; import cn.hippo4j.config.springboot.starter.config.BootstrapConfigProperties; import cn.hippo4j.config.springboot.starter.config.MonitorProperties; import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage; -import cn.hippo4j.common.design.builder.ThreadFactoryBuilder; import cn.hippo4j.monitor.base.DynamicThreadPoolMonitor; import cn.hippo4j.monitor.base.ThreadPoolMonitor; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; + import org.springframework.beans.factory.DisposableBean; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.List; -import java.util.Optional; -import java.util.concurrent.ScheduledThreadPoolExecutor; -import java.util.concurrent.TimeUnit; - import static cn.hippo4j.core.executor.manage.GlobalThreadPoolManage.getThreadPoolNum; /** @@ -67,7 +68,7 @@ public class ThreadPoolMonitorExecutor implements ApplicationRunner, DisposableB log.info("Start monitoring the running status of dynamic thread pool."); threadPoolMonitors = new ArrayList<>(); collectScheduledExecutor = new ScheduledThreadPoolExecutor( - new Integer(1), + 1, ThreadFactoryBuilder.builder().daemon(true).prefix("client.scheduled.collect.data").build()); // Get dynamic thread pool monitoring component. List collectTypes = Arrays.asList(monitor.getCollectTypes().split(",")); @@ -77,7 +78,7 @@ public class ThreadPoolMonitorExecutor implements ApplicationRunner, DisposableB dynamicThreadPoolMonitors.stream().filter(each -> collectTypes.contains(each.getType())).forEach(each -> threadPoolMonitors.add(each)); // Execute dynamic thread pool monitoring component. collectScheduledExecutor.scheduleWithFixedDelay( - () -> scheduleRunnable(), + this::scheduleRunnable, properties.getInitialDelay(), properties.getCollectInterval(), TimeUnit.MILLISECONDS); @@ -98,6 +99,6 @@ public class ThreadPoolMonitorExecutor implements ApplicationRunner, DisposableB @Override public void destroy() throws Exception { - Optional.ofNullable(collectScheduledExecutor).ifPresent(each -> each.shutdown()); + Optional.ofNullable(collectScheduledExecutor).ifPresent(ScheduledThreadPoolExecutor::shutdown); } }