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 8d87c0c3..286db868 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 @@ -28,6 +28,7 @@ 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; @@ -35,6 +36,7 @@ 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; @@ -45,11 +47,11 @@ import static cn.hippo4j.core.executor.manage.GlobalThreadPoolManage.getThreadPo */ @Slf4j @RequiredArgsConstructor -public class ThreadPoolMonitorExecutor implements ApplicationRunner { +public class ThreadPoolMonitorExecutor implements ApplicationRunner, DisposableBean { private final BootstrapConfigProperties properties; - private ScheduledThreadPoolExecutor collectExecutor; + private ScheduledThreadPoolExecutor collectScheduledExecutor; private List threadPoolMonitors; @@ -64,7 +66,7 @@ public class ThreadPoolMonitorExecutor implements ApplicationRunner { } log.info("Start monitoring the running status of dynamic thread pool."); threadPoolMonitors = new ArrayList<>(); - collectExecutor = new ScheduledThreadPoolExecutor( + collectScheduledExecutor = new ScheduledThreadPoolExecutor( new Integer(1), ThreadFactoryBuilder.builder().daemon(true).prefix("client.scheduled.collect.data").build()); // Get dynamic thread pool monitoring component. @@ -74,7 +76,7 @@ public class ThreadPoolMonitorExecutor implements ApplicationRunner { DynamicThreadPoolServiceLoader.getSingletonServiceInstances(DynamicThreadPoolMonitor.class); dynamicThreadPoolMonitors.stream().filter(each -> collectTypes.contains(each.getType())).forEach(each -> threadPoolMonitors.add(each)); // Execute dynamic thread pool monitoring component. - collectExecutor.scheduleWithFixedDelay( + collectScheduledExecutor.scheduleWithFixedDelay( () -> scheduleRunnable(), properties.getInitialDelay(), properties.getCollectInterval(), @@ -93,4 +95,9 @@ public class ThreadPoolMonitorExecutor implements ApplicationRunner { } } } + + @Override + public void destroy() throws Exception { + Optional.ofNullable(collectScheduledExecutor).ifPresent(each -> each.shutdown()); + } }