diff --git a/hippo4j-adapter/hippo4j-adapter-base/src/main/java/cn/hippo4j/adapter/base/ThreadPoolAdapterRegisterAction.java b/hippo4j-adapter/hippo4j-adapter-base/src/main/java/cn/hippo4j/adapter/base/ThreadPoolAdapterRegisterAction.java new file mode 100644 index 00000000..0728e619 --- /dev/null +++ b/hippo4j-adapter/hippo4j-adapter-base/src/main/java/cn/hippo4j/adapter/base/ThreadPoolAdapterRegisterAction.java @@ -0,0 +1,12 @@ +package cn.hippo4j.adapter.base; + + +import java.util.Map; + +/** + * Provide registration for each adaptation + */ +public interface ThreadPoolAdapterRegisterAction { + + void adapterRegister(Map threadPoolAdapterMap); +} diff --git a/hippo4j-adapter/hippo4j-adapter-hystrix/src/main/java/cn/hippo4j/adapter/hystrix/HystrixThreadPoolAdapter.java b/hippo4j-adapter/hippo4j-adapter-hystrix/src/main/java/cn/hippo4j/adapter/hystrix/HystrixThreadPoolAdapter.java index 0b28b345..a4a422eb 100644 --- a/hippo4j-adapter/hippo4j-adapter-hystrix/src/main/java/cn/hippo4j/adapter/hystrix/HystrixThreadPoolAdapter.java +++ b/hippo4j-adapter/hippo4j-adapter-hystrix/src/main/java/cn/hippo4j/adapter/hystrix/HystrixThreadPoolAdapter.java @@ -17,10 +17,8 @@ package cn.hippo4j.adapter.hystrix; -import cn.hippo4j.adapter.base.ThreadPoolAdapter; -import cn.hippo4j.adapter.base.ThreadPoolAdapterScheduler; -import cn.hippo4j.adapter.base.ThreadPoolAdapterParameter; -import cn.hippo4j.adapter.base.ThreadPoolAdapterState; +import cn.hippo4j.adapter.base.*; +import cn.hippo4j.common.config.ApplicationContextHolder; import cn.hippo4j.common.toolkit.CollectionUtil; import com.google.common.collect.Maps; import com.netflix.hystrix.HystrixThreadPool; @@ -106,6 +104,15 @@ public class HystrixThreadPoolAdapter implements ThreadPoolAdapter, ApplicationL @Override public void onApplicationEvent(ApplicationStartedEvent event) { + //Periodically update the Hystrix thread pool + HystrixThreadPoolRefresh(); + //Periodically refresh registration + ThreadPoolAdapterRegisterAction threadPoolAdapterRegisterAction = ApplicationContextHolder.getBean(ThreadPoolAdapterRegisterAction.class); + Map map = (Map)ApplicationContextHolder.getBeansOfType(this.getClass()); + threadPoolAdapterRegisterAction.adapterRegister(map); + } + + public void HystrixThreadPoolRefresh(){ ScheduledExecutorService scheduler = threadPoolAdapterScheduler.getScheduler(); int taskIntervalSeconds = threadPoolAdapterScheduler.getTaskIntervalSeconds(); HystrixThreadPoolRefreshTask hystrixThreadPoolRefreshTask = new HystrixThreadPoolRefreshTask(scheduler, taskIntervalSeconds); diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/core/ThreadPoolAdapterRegister.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/core/ThreadPoolAdapterRegister.java index 21798532..650fcfee 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/core/ThreadPoolAdapterRegister.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/core/ThreadPoolAdapterRegister.java @@ -17,10 +17,7 @@ package cn.hippo4j.springboot.starter.core; -import cn.hippo4j.adapter.base.ThreadPoolAdapter; -import cn.hippo4j.adapter.base.ThreadPoolAdapterCacheConfig; -import cn.hippo4j.adapter.base.ThreadPoolAdapterScheduler; -import cn.hippo4j.adapter.base.ThreadPoolAdapterState; +import cn.hippo4j.adapter.base.*; import cn.hippo4j.common.config.ApplicationContextHolder; import cn.hippo4j.common.toolkit.CollectionUtil; import cn.hippo4j.common.web.base.Result; @@ -52,7 +49,7 @@ import static cn.hippo4j.common.constant.Constants.REGISTER_ADAPTER_PATH; @Slf4j @AllArgsConstructor @RequiredArgsConstructor -public class ThreadPoolAdapterRegister implements ApplicationRunner { +public class ThreadPoolAdapterRegister implements ApplicationRunner, ThreadPoolAdapterRegisterAction { private final HttpAgent httpAgent; @@ -64,19 +61,14 @@ public class ThreadPoolAdapterRegister implements ApplicationRunner { private final ThreadPoolAdapterScheduler threadPoolAdapterScheduler; - private List cacheConfigList = Lists.newArrayList(); + @Override public void run(ApplicationArguments args) throws Exception { - - ScheduledExecutorService scheduler = threadPoolAdapterScheduler.getScheduler(); - int taskIntervalSeconds = threadPoolAdapterScheduler.getTaskIntervalSeconds(); - ThreadPoolAdapterRegisterTask threadPoolAdapterRegisterTask = new ThreadPoolAdapterRegisterTask(scheduler, taskIntervalSeconds); - scheduler.schedule(threadPoolAdapterRegisterTask, threadPoolAdapterScheduler.getTaskIntervalSeconds(), TimeUnit.SECONDS); + register(); } - public List getThreadPoolAdapterCacheConfigs() { - Map threadPoolAdapterMap = ApplicationContextHolder.getBeansOfType(ThreadPoolAdapter.class); + public List getThreadPoolAdapterCacheConfigs(Map threadPoolAdapterMap) { List cacheConfigList = Lists.newArrayList(); threadPoolAdapterMap.forEach((key, val) -> { List threadPoolStates = val.getThreadPoolStates(); @@ -110,25 +102,40 @@ public class ThreadPoolAdapterRegister implements ApplicationRunner { } public void register() { - List threadPoolAdapterCacheConfigs = getThreadPoolAdapterCacheConfigs(); + Map threadPoolAdapterMap = ApplicationContextHolder.getBeansOfType(ThreadPoolAdapter.class); + List threadPoolAdapterCacheConfigs = getThreadPoolAdapterCacheConfigs(threadPoolAdapterMap); doRegister(threadPoolAdapterCacheConfigs); } + @Override + public void adapterRegister(Map threadPoolAdapterMap) { + ScheduledExecutorService scheduler = threadPoolAdapterScheduler.getScheduler(); + int taskIntervalSeconds = threadPoolAdapterScheduler.getTaskIntervalSeconds(); + ThreadPoolAdapterRegisterTask threadPoolAdapterRegisterTask = new ThreadPoolAdapterRegisterTask(scheduler, taskIntervalSeconds, threadPoolAdapterMap); + scheduler.schedule(threadPoolAdapterRegisterTask, threadPoolAdapterScheduler.getTaskIntervalSeconds(), TimeUnit.SECONDS); + } + class ThreadPoolAdapterRegisterTask implements Runnable { private ScheduledExecutorService scheduler; private int taskIntervalSeconds; - public ThreadPoolAdapterRegisterTask(ScheduledExecutorService scheduler, int taskIntervalSeconds) { + Map threadPoolAdapterMap; + + private List cacheConfigList = Lists.newArrayList(); + + public ThreadPoolAdapterRegisterTask(ScheduledExecutorService scheduler, int taskIntervalSeconds, + Map threadPoolAdapterMap) { this.scheduler = scheduler; this.taskIntervalSeconds = taskIntervalSeconds; + this.threadPoolAdapterMap = threadPoolAdapterMap; } @Override public void run() { try { - List newThreadPoolAdapterCacheConfigs = getThreadPoolAdapterCacheConfigs(); + List newThreadPoolAdapterCacheConfigs = getThreadPoolAdapterCacheConfigs(threadPoolAdapterMap); boolean registerFlag = compareThreadPoolAdapterCacheConfigs(newThreadPoolAdapterCacheConfigs, cacheConfigList);