|
|
@ -18,14 +18,18 @@
|
|
|
|
package cn.hippo4j.springboot.starter.core;
|
|
|
|
package cn.hippo4j.springboot.starter.core;
|
|
|
|
|
|
|
|
|
|
|
|
import cn.hippo4j.common.model.ThreadPoolParameterInfo;
|
|
|
|
import cn.hippo4j.common.model.ThreadPoolParameterInfo;
|
|
|
|
|
|
|
|
import cn.hippo4j.common.model.register.DynamicThreadPoolRegisterParameter;
|
|
|
|
|
|
|
|
import cn.hippo4j.common.model.register.DynamicThreadPoolRegisterWrapper;
|
|
|
|
import cn.hippo4j.common.toolkit.Assert;
|
|
|
|
import cn.hippo4j.common.toolkit.Assert;
|
|
|
|
import cn.hippo4j.common.toolkit.JSONUtil;
|
|
|
|
import cn.hippo4j.common.toolkit.JSONUtil;
|
|
|
|
import cn.hippo4j.common.web.base.Result;
|
|
|
|
import cn.hippo4j.common.web.base.Result;
|
|
|
|
|
|
|
|
import cn.hippo4j.common.web.exception.ServiceException;
|
|
|
|
import cn.hippo4j.core.executor.DynamicThreadPoolWrapper;
|
|
|
|
import cn.hippo4j.core.executor.DynamicThreadPoolWrapper;
|
|
|
|
import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage;
|
|
|
|
import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage;
|
|
|
|
import cn.hippo4j.common.model.register.DynamicThreadPoolRegisterParameter;
|
|
|
|
|
|
|
|
import cn.hippo4j.common.model.register.DynamicThreadPoolRegisterWrapper;
|
|
|
|
|
|
|
|
import cn.hippo4j.core.executor.support.DynamicThreadPoolService;
|
|
|
|
import cn.hippo4j.core.executor.support.DynamicThreadPoolService;
|
|
|
|
|
|
|
|
import cn.hippo4j.core.executor.support.QueueTypeEnum;
|
|
|
|
|
|
|
|
import cn.hippo4j.core.executor.support.RejectedTypeEnum;
|
|
|
|
|
|
|
|
import cn.hippo4j.core.executor.support.ThreadPoolBuilder;
|
|
|
|
import cn.hippo4j.springboot.starter.config.BootstrapProperties;
|
|
|
|
import cn.hippo4j.springboot.starter.config.BootstrapProperties;
|
|
|
|
import cn.hippo4j.springboot.starter.event.ApplicationCompleteEvent;
|
|
|
|
import cn.hippo4j.springboot.starter.event.ApplicationCompleteEvent;
|
|
|
|
import cn.hippo4j.springboot.starter.remote.HttpAgent;
|
|
|
|
import cn.hippo4j.springboot.starter.remote.HttpAgent;
|
|
|
@ -33,6 +37,8 @@ import lombok.RequiredArgsConstructor;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import org.springframework.context.ApplicationListener;
|
|
|
|
import org.springframework.context.ApplicationListener;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.concurrent.ThreadPoolExecutor;
|
|
|
|
|
|
|
|
|
|
|
|
import static cn.hippo4j.common.constant.Constants.REGISTER_DYNAMIC_THREAD_POOL_PATH;
|
|
|
|
import static cn.hippo4j.common.constant.Constants.REGISTER_DYNAMIC_THREAD_POOL_PATH;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -56,28 +62,44 @@ public class DynamicThreadPoolConfigService implements DynamicThreadPoolService,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void registerDynamicThreadPool(DynamicThreadPoolRegisterWrapper registerWrapper) {
|
|
|
|
public ThreadPoolExecutor registerDynamicThreadPool(DynamicThreadPoolRegisterWrapper registerWrapper) {
|
|
|
|
DynamicThreadPoolRegisterParameter registerParameter = registerWrapper.getDynamicThreadPoolRegisterParameter();
|
|
|
|
DynamicThreadPoolRegisterParameter registerParameter = registerWrapper.getDynamicThreadPoolRegisterParameter();
|
|
|
|
checkThreadPoolParameter(registerParameter);
|
|
|
|
checkThreadPoolParameter(registerParameter);
|
|
|
|
ThreadPoolParameterInfo parameter = JSONUtil.parseObject(JSONUtil.toJSONString(registerParameter), ThreadPoolParameterInfo.class);
|
|
|
|
ThreadPoolParameterInfo parameter = JSONUtil.parseObject(JSONUtil.toJSONString(registerParameter), ThreadPoolParameterInfo.class);
|
|
|
|
String threadPoolId = registerParameter.getThreadPoolId();
|
|
|
|
String threadPoolId = registerParameter.getThreadPoolId();
|
|
|
|
Result registerResult;
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
failDynamicThreadPoolRegisterWrapper(registerWrapper);
|
|
|
|
failDynamicThreadPoolRegisterWrapper(registerWrapper);
|
|
|
|
registerResult = httpAgent.httpPost(REGISTER_DYNAMIC_THREAD_POOL_PATH, registerWrapper);
|
|
|
|
Result registerResult = httpAgent.httpPost(REGISTER_DYNAMIC_THREAD_POOL_PATH, registerWrapper);
|
|
|
|
|
|
|
|
if (registerResult == null || !registerResult.isSuccess()) {
|
|
|
|
|
|
|
|
throw new ServiceException("Dynamic thread pool registration returns error.");
|
|
|
|
|
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
log.error("Failed to dynamically register thread pool: {}", threadPoolId, ex);
|
|
|
|
log.error("Dynamic thread pool registration execution error: {}", threadPoolId, ex);
|
|
|
|
throw ex;
|
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (registerResult.isSuccess()) {
|
|
|
|
ThreadPoolExecutor dynamicThreadPoolExecutor = buildDynamicThreadPoolExecutor(registerParameter);
|
|
|
|
DynamicThreadPoolWrapper dynamicThreadPoolWrapper = DynamicThreadPoolWrapper.builder()
|
|
|
|
DynamicThreadPoolWrapper dynamicThreadPoolWrapper = DynamicThreadPoolWrapper.builder()
|
|
|
|
.threadPoolId(threadPoolId)
|
|
|
|
.threadPoolId(threadPoolId)
|
|
|
|
.executor(registerWrapper.getDynamicThreadPoolExecutor())
|
|
|
|
.executor(dynamicThreadPoolExecutor)
|
|
|
|
.build();
|
|
|
|
.build();
|
|
|
|
GlobalThreadPoolManage.register(threadPoolId, parameter, dynamicThreadPoolWrapper);
|
|
|
|
GlobalThreadPoolManage.register(threadPoolId, parameter, dynamicThreadPoolWrapper);
|
|
|
|
dynamicThreadPoolSubscribeConfig.subscribeConfig(threadPoolId);
|
|
|
|
dynamicThreadPoolSubscribeConfig.subscribeConfig(threadPoolId);
|
|
|
|
clientWorker.addCacheDataIfAbsent(properties.getNamespace(), properties.getItemId(), threadPoolId);
|
|
|
|
clientWorker.addCacheDataIfAbsent(properties.getNamespace(), properties.getItemId(), threadPoolId);
|
|
|
|
}
|
|
|
|
return dynamicThreadPoolExecutor;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private ThreadPoolExecutor buildDynamicThreadPoolExecutor(DynamicThreadPoolRegisterParameter registerParameter) {
|
|
|
|
|
|
|
|
ThreadPoolExecutor dynamicThreadPoolExecutor = ThreadPoolBuilder.builder()
|
|
|
|
|
|
|
|
.threadPoolId(registerParameter.getThreadPoolId())
|
|
|
|
|
|
|
|
.corePoolSize(registerParameter.getCorePoolSize())
|
|
|
|
|
|
|
|
.maxPoolNum(registerParameter.getMaximumPoolSize())
|
|
|
|
|
|
|
|
.workQueue(QueueTypeEnum.createBlockingQueue(registerParameter.getQueueType(), registerParameter.getCapacity()))
|
|
|
|
|
|
|
|
.threadFactory(registerParameter.getThreadPoolId())
|
|
|
|
|
|
|
|
.keepAliveTime(registerParameter.getKeepAliveTime())
|
|
|
|
|
|
|
|
.rejected(RejectedTypeEnum.createPolicy(registerParameter.getRejectedType()))
|
|
|
|
|
|
|
|
.dynamicPool()
|
|
|
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
return dynamicThreadPoolExecutor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void checkThreadPoolParameter(DynamicThreadPoolRegisterParameter registerParameter) {
|
|
|
|
private void checkThreadPoolParameter(DynamicThreadPoolRegisterParameter registerParameter) {
|
|
|
|