Hippo4j core add framework thread pool adaptation

pull/235/head
chen.ma 3 years ago
parent 9b4cfb75e1
commit dd4e2336bf

@ -12,7 +12,8 @@
<packaging>jar</packaging> <packaging>jar</packaging>
<name>${project.artifactId}</name> <name>${project.artifactId}</name>
<description>Thread pool dynamic parameter adjustment, alarming, status viewing and monitoring functions</description> <description>Thread pool dynamic parameter adjustment, alarming, status viewing and monitoring functions
</description>
<dependencies> <dependencies>
<dependency> <dependency>
@ -85,6 +86,11 @@
<version>1.8.4</version> <version>1.8.4</version>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>cn.hippo4j</groupId>
<artifactId>hippo4j-adapter-base</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>

@ -0,0 +1,30 @@
package cn.hippo4j.core.springboot.starter.config;
import lombok.Data;
/**
* Adapter executor properties.
*/
@Data
public class AdapterExecutorProperties {
/**
* Mark
*/
private String mark;
/**
* Thread-pool key
*/
private String threadPoolKey;
/**
* Core pool size
*/
private Integer corePoolSize;
/**
* Maximum pool size
*/
private Integer maximumPoolSize;
}

@ -146,4 +146,9 @@ public class BootstrapCoreProperties implements BootstrapPropertiesInterface {
* Executors. * Executors.
*/ */
private List<ExecutorProperties> executors; private List<ExecutorProperties> executors;
/**
* Adapter executors
*/
private List<AdapterExecutorProperties> adapterExecutors;
} }

@ -39,10 +39,12 @@ import cn.hippo4j.core.springboot.starter.refresher.ApolloRefresherHandler;
import cn.hippo4j.core.springboot.starter.refresher.NacosCloudRefresherHandler; import cn.hippo4j.core.springboot.starter.refresher.NacosCloudRefresherHandler;
import cn.hippo4j.core.springboot.starter.refresher.NacosRefresherHandler; import cn.hippo4j.core.springboot.starter.refresher.NacosRefresherHandler;
import cn.hippo4j.core.springboot.starter.refresher.ZookeeperRefresherHandler; import cn.hippo4j.core.springboot.starter.refresher.ZookeeperRefresherHandler;
import cn.hippo4j.core.springboot.starter.refresher.event.AdapterExecutorsListener;
import cn.hippo4j.core.springboot.starter.refresher.event.ExecutorsListener; import cn.hippo4j.core.springboot.starter.refresher.event.ExecutorsListener;
import cn.hippo4j.core.springboot.starter.refresher.event.PlatformsListener; import cn.hippo4j.core.springboot.starter.refresher.event.PlatformsListener;
import cn.hippo4j.core.springboot.starter.refresher.event.WebExecutorListener; import cn.hippo4j.core.springboot.starter.refresher.event.WebExecutorListener;
import cn.hippo4j.core.springboot.starter.support.DynamicThreadPoolPostProcessor; import cn.hippo4j.core.springboot.starter.support.DynamicThreadPoolPostProcessor;
import cn.hippo4j.core.springboot.starter.support.ThreadPoolAdapterRegister;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.*; import org.springframework.boot.autoconfigure.condition.*;
@ -173,6 +175,11 @@ public class DynamicThreadPoolCoreAutoConfiguration {
return new ExecutorsListener(threadPoolNotifyAlarmHandler); return new ExecutorsListener(threadPoolNotifyAlarmHandler);
} }
@Bean
public AdapterExecutorsListener hippo4jAdapterExecutorsListener() {
return new AdapterExecutorsListener();
}
@Bean @Bean
public PlatformsListener hippo4jPlatformsListener() { public PlatformsListener hippo4jPlatformsListener() {
return new PlatformsListener(); return new PlatformsListener();
@ -182,4 +189,9 @@ public class DynamicThreadPoolCoreAutoConfiguration {
public WebExecutorListener hippo4jWebExecutorListener() { public WebExecutorListener hippo4jWebExecutorListener() {
return new WebExecutorListener(); return new WebExecutorListener();
} }
@Bean
public ThreadPoolAdapterRegister threadPoolAdapterRegister() {
return new ThreadPoolAdapterRegister(bootstrapCoreProperties);
}
} }

@ -0,0 +1,52 @@
package cn.hippo4j.core.springboot.starter.refresher.event;
import cn.hippo4j.adapter.base.ThreadPoolAdapter;
import cn.hippo4j.adapter.base.ThreadPoolAdapterParameter;
import cn.hippo4j.common.config.ApplicationContextHolder;
import cn.hippo4j.common.toolkit.CollectionUtil;
import cn.hippo4j.core.springboot.starter.config.AdapterExecutorProperties;
import cn.hutool.core.bean.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.core.annotation.Order;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import static cn.hippo4j.common.constant.Constants.IDENTIFY_SLICER_SYMBOL;
import static cn.hippo4j.core.springboot.starter.refresher.event.Hippo4jCoreDynamicRefreshEventOrder.ADAPTER_EXECUTORS_LISTENER;
import static cn.hippo4j.core.springboot.starter.support.ThreadPoolAdapterRegister.ADAPTER_EXECUTORS_MAP;
/**
* Adapter executors listener.
*/
@Slf4j
@Order(ADAPTER_EXECUTORS_LISTENER)
public class AdapterExecutorsListener implements ApplicationListener<Hippo4jCoreDynamicRefreshEvent> {
@Override
public void onApplicationEvent(Hippo4jCoreDynamicRefreshEvent event) {
List<AdapterExecutorProperties> adapterExecutors;
Map<String, ThreadPoolAdapter> threadPoolAdapterMap = ApplicationContextHolder.getBeansOfType(ThreadPoolAdapter.class);
if (CollectionUtil.isEmpty(adapterExecutors = event.getBootstrapCoreProperties().getAdapterExecutors()) || CollectionUtil.isEmpty(threadPoolAdapterMap)) {
return;
}
for (AdapterExecutorProperties each : adapterExecutors) {
String buildKey = each.getMark() + IDENTIFY_SLICER_SYMBOL + each.getThreadPoolKey();
AdapterExecutorProperties adapterExecutorProperties = ADAPTER_EXECUTORS_MAP.get(buildKey);
if (adapterExecutorProperties == null) {
continue;
}
if (!Objects.equals(adapterExecutorProperties.getCorePoolSize(), each.getCorePoolSize())
|| !Objects.equals(adapterExecutorProperties.getMaximumPoolSize(), each.getMaximumPoolSize())) {
threadPoolAdapterMap.forEach((key, val) -> {
if (Objects.equals(val.mark(), each.getMark())) {
val.updateThreadPool(BeanUtil.toBean(each, ThreadPoolAdapterParameter.class));
ADAPTER_EXECUTORS_MAP.put(buildKey, each);
}
});
}
}
}
}

@ -27,4 +27,6 @@ public interface Hippo4jCoreDynamicRefreshEventOrder {
int PLATFORMS_LISTENER = 1; int PLATFORMS_LISTENER = 1;
int EXECUTORS_LISTENER = 2; int EXECUTORS_LISTENER = 2;
int ADAPTER_EXECUTORS_LISTENER = 3;
} }

@ -0,0 +1,39 @@
package cn.hippo4j.core.springboot.starter.support;
import cn.hippo4j.common.toolkit.CollectionUtil;
import cn.hippo4j.core.springboot.starter.config.AdapterExecutorProperties;
import cn.hippo4j.core.springboot.starter.config.BootstrapCoreProperties;
import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import java.util.List;
import java.util.Map;
import static cn.hippo4j.common.constant.Constants.IDENTIFY_SLICER_SYMBOL;
/**
* Thread-pool adapter register.
*/
@Slf4j
@AllArgsConstructor
public class ThreadPoolAdapterRegister implements ApplicationRunner {
private final BootstrapCoreProperties bootstrapCoreProperties;
public static final Map<String, AdapterExecutorProperties> ADAPTER_EXECUTORS_MAP = Maps.newConcurrentMap();
@Override
public void run(ApplicationArguments args) throws Exception {
List<AdapterExecutorProperties> adapterExecutors;
if (CollectionUtil.isEmpty(adapterExecutors = bootstrapCoreProperties.getAdapterExecutors())) {
return;
}
for (AdapterExecutorProperties each : adapterExecutors) {
String buildKey = each.getMark() + IDENTIFY_SLICER_SYMBOL + each.getThreadPoolKey();
ADAPTER_EXECUTORS_MAP.put(buildKey, each);
}
}
}
Loading…
Cancel
Save