|
|
|
@ -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;
|
|
|
|
@ -51,8 +48,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,23 +60,18 @@ public class ThreadPoolAdapterRegister implements ApplicationRunner {
|
|
|
|
|
|
|
|
|
|
private final ThreadPoolAdapterScheduler threadPoolAdapterScheduler;
|
|
|
|
|
|
|
|
|
|
private List<ThreadPoolAdapterCacheConfig> 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<ThreadPoolAdapterCacheConfig> getThreadPoolAdapterCacheConfigs() {
|
|
|
|
|
Map<String, ThreadPoolAdapter> threadPoolAdapterMap = ApplicationContextHolder.getBeansOfType(ThreadPoolAdapter.class);
|
|
|
|
|
public List<ThreadPoolAdapterCacheConfig> getThreadPoolAdapterCacheConfigs(Map<String, ThreadPoolAdapter> threadPoolAdapterMap) {
|
|
|
|
|
List<ThreadPoolAdapterCacheConfig> cacheConfigList = Lists.newArrayList();
|
|
|
|
|
threadPoolAdapterMap.forEach((key, val) -> {
|
|
|
|
|
for (Map.Entry<String, ThreadPoolAdapter> threadPoolAdapterEntry : threadPoolAdapterMap.entrySet()) {
|
|
|
|
|
ThreadPoolAdapter val = threadPoolAdapterEntry.getValue();
|
|
|
|
|
List<ThreadPoolAdapterState> threadPoolStates = val.getThreadPoolStates();
|
|
|
|
|
if (CollectionUtil.isEmpty(threadPoolStates)) {
|
|
|
|
|
return;
|
|
|
|
|
if (CollectionUtil.isEmpty(threadPoolStates) || threadPoolStates.size() == 0) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
ThreadPoolAdapterCacheConfig cacheConfig = new ThreadPoolAdapterCacheConfig();
|
|
|
|
|
cacheConfig.setMark(val.mark());
|
|
|
|
@ -91,12 +82,12 @@ public class ThreadPoolAdapterRegister implements ApplicationRunner {
|
|
|
|
|
cacheConfig.setClientAddress(clientAddress);
|
|
|
|
|
cacheConfig.setThreadPoolAdapterStates(threadPoolStates);
|
|
|
|
|
cacheConfigList.add(cacheConfig);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return cacheConfigList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void doRegister(List<ThreadPoolAdapterCacheConfig> cacheConfigList) {
|
|
|
|
|
if (CollectionUtil.isNotEmpty(cacheConfigList)) {
|
|
|
|
|
if (CollectionUtil.isNotEmpty(cacheConfigList) && cacheConfigList.size() > 0) {
|
|
|
|
|
try {
|
|
|
|
|
Result result = httpAgent.httpPost(REGISTER_ADAPTER_PATH, cacheConfigList);
|
|
|
|
|
if (!result.isSuccess()) {
|
|
|
|
@ -109,25 +100,40 @@ public class ThreadPoolAdapterRegister implements ApplicationRunner {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void register() {
|
|
|
|
|
List<ThreadPoolAdapterCacheConfig> threadPoolAdapterCacheConfigs = getThreadPoolAdapterCacheConfigs();
|
|
|
|
|
Map<String, ThreadPoolAdapter> threadPoolAdapterMap = ApplicationContextHolder.getBeansOfType(ThreadPoolAdapter.class);
|
|
|
|
|
List<ThreadPoolAdapterCacheConfig> threadPoolAdapterCacheConfigs = getThreadPoolAdapterCacheConfigs(threadPoolAdapterMap);
|
|
|
|
|
doRegister(threadPoolAdapterCacheConfigs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void adapterRegister(Map<String, ThreadPoolAdapter> 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<String, ThreadPoolAdapter> threadPoolAdapterMap;
|
|
|
|
|
|
|
|
|
|
private List<ThreadPoolAdapterCacheConfig> cacheConfigList = Lists.newArrayList();
|
|
|
|
|
|
|
|
|
|
public ThreadPoolAdapterRegisterTask(ScheduledExecutorService scheduler, int taskIntervalSeconds,
|
|
|
|
|
Map<String, ThreadPoolAdapter> threadPoolAdapterMap) {
|
|
|
|
|
this.scheduler = scheduler;
|
|
|
|
|
this.taskIntervalSeconds = taskIntervalSeconds;
|
|
|
|
|
this.threadPoolAdapterMap = threadPoolAdapterMap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
try {
|
|
|
|
|
List<ThreadPoolAdapterCacheConfig> newThreadPoolAdapterCacheConfigs = getThreadPoolAdapterCacheConfigs();
|
|
|
|
|
List<ThreadPoolAdapterCacheConfig> newThreadPoolAdapterCacheConfigs = getThreadPoolAdapterCacheConfigs(threadPoolAdapterMap);
|
|
|
|
|
boolean registerFlag = compareThreadPoolAdapterCacheConfigs(newThreadPoolAdapterCacheConfigs, cacheConfigList);
|
|
|
|
|
cacheConfigList = newThreadPoolAdapterCacheConfigs;
|
|
|
|
|
if (registerFlag) {
|
|
|
|
|