Simplify the code

pull/957/head
weihu 3 years ago
parent c15011ed36
commit 4f36a3d9e3

@ -17,29 +17,30 @@
package cn.hippo4j.config.springboot.starter.monitor; 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.config.ApplicationContextHolder;
import cn.hippo4j.common.design.builder.ThreadFactoryBuilder;
import cn.hippo4j.common.spi.DynamicThreadPoolServiceLoader; import cn.hippo4j.common.spi.DynamicThreadPoolServiceLoader;
import cn.hippo4j.common.toolkit.StringUtil; import cn.hippo4j.common.toolkit.StringUtil;
import cn.hippo4j.config.springboot.starter.config.BootstrapConfigProperties; import cn.hippo4j.config.springboot.starter.config.BootstrapConfigProperties;
import cn.hippo4j.config.springboot.starter.config.MonitorProperties; import cn.hippo4j.config.springboot.starter.config.MonitorProperties;
import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage; 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.DynamicThreadPoolMonitor;
import cn.hippo4j.monitor.base.ThreadPoolMonitor; import cn.hippo4j.monitor.base.ThreadPoolMonitor;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.DisposableBean;
import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner; 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; 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."); log.info("Start monitoring the running status of dynamic thread pool.");
threadPoolMonitors = new ArrayList<>(); threadPoolMonitors = new ArrayList<>();
collectScheduledExecutor = new ScheduledThreadPoolExecutor( collectScheduledExecutor = new ScheduledThreadPoolExecutor(
new Integer(1), 1,
ThreadFactoryBuilder.builder().daemon(true).prefix("client.scheduled.collect.data").build()); ThreadFactoryBuilder.builder().daemon(true).prefix("client.scheduled.collect.data").build());
// Get dynamic thread pool monitoring component. // Get dynamic thread pool monitoring component.
List<String> collectTypes = Arrays.asList(monitor.getCollectTypes().split(",")); List<String> 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)); dynamicThreadPoolMonitors.stream().filter(each -> collectTypes.contains(each.getType())).forEach(each -> threadPoolMonitors.add(each));
// Execute dynamic thread pool monitoring component. // Execute dynamic thread pool monitoring component.
collectScheduledExecutor.scheduleWithFixedDelay( collectScheduledExecutor.scheduleWithFixedDelay(
() -> scheduleRunnable(), this::scheduleRunnable,
properties.getInitialDelay(), properties.getInitialDelay(),
properties.getCollectInterval(), properties.getCollectInterval(),
TimeUnit.MILLISECONDS); TimeUnit.MILLISECONDS);
@ -98,6 +99,6 @@ public class ThreadPoolMonitorExecutor implements ApplicationRunner, DisposableB
@Override @Override
public void destroy() throws Exception { public void destroy() throws Exception {
Optional.ofNullable(collectScheduledExecutor).ifPresent(each -> each.shutdown()); Optional.ofNullable(collectScheduledExecutor).ifPresent(ScheduledThreadPoolExecutor::shutdown);
} }
} }

Loading…
Cancel
Save