mirror of https://github.com/longtai-cn/hippo4j
Merge pull request #512 from mabaiwan/develop
Improve the dynamic registration thread pool logicpull/515/head
commit
f6eaecca75
@ -0,0 +1,41 @@
|
|||||||
|
package cn.hippo4j.common.model.register.notify;
|
||||||
|
|
||||||
|
import lombok.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dynamic thread-pool register core notify parameter.
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class DynamicThreadPoolRegisterCoreNotifyParameter {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether to enable thread pool running alarm
|
||||||
|
*/
|
||||||
|
@NonNull
|
||||||
|
private Boolean alarm;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Active alarm
|
||||||
|
*/
|
||||||
|
@NonNull
|
||||||
|
private Integer activeAlarm;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Capacity alarm
|
||||||
|
*/
|
||||||
|
@NonNull
|
||||||
|
private Integer capacityAlarm;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interval
|
||||||
|
*/
|
||||||
|
private Integer interval;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Receive
|
||||||
|
*/
|
||||||
|
private String receives;
|
||||||
|
}
|
@ -0,0 +1,46 @@
|
|||||||
|
package cn.hippo4j.common.model.register.notify;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dynamic thread-pool register server notify parameter.
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class DynamicThreadPoolRegisterServerNotifyParameter {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Thread-pool id
|
||||||
|
*/
|
||||||
|
private String threadPoolId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Platform
|
||||||
|
*/
|
||||||
|
private String platform;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Config type
|
||||||
|
*/
|
||||||
|
private String type;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Secret key
|
||||||
|
*/
|
||||||
|
private String secretKey;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interval
|
||||||
|
*/
|
||||||
|
private Integer interval;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Receives
|
||||||
|
*/
|
||||||
|
private String receives;
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
package cn.hippo4j.core.executor.support.service;
|
||||||
|
|
||||||
|
import cn.hippo4j.common.model.register.DynamicThreadPoolRegisterParameter;
|
||||||
|
import cn.hippo4j.core.executor.support.QueueTypeEnum;
|
||||||
|
import cn.hippo4j.core.executor.support.RejectedTypeEnum;
|
||||||
|
import cn.hippo4j.core.executor.support.ThreadPoolBuilder;
|
||||||
|
|
||||||
|
import java.util.concurrent.ThreadPoolExecutor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Abstract dynamic thread-pool service.
|
||||||
|
*/
|
||||||
|
public abstract class AbstractDynamicThreadPoolService implements DynamicThreadPoolService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build dynamic thread-pool executor.
|
||||||
|
*
|
||||||
|
* @param registerParameter
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public 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.getThreadNamePrefix())
|
||||||
|
.keepAliveTime(registerParameter.getKeepAliveTime())
|
||||||
|
.rejected(RejectedTypeEnum.createPolicy(registerParameter.getRejectedType()))
|
||||||
|
.dynamicPool()
|
||||||
|
.build();
|
||||||
|
return dynamicThreadPoolExecutor;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,60 @@
|
|||||||
|
package cn.hippo4j.core.springboot.starter.support;
|
||||||
|
|
||||||
|
import cn.hippo4j.common.model.register.DynamicThreadPoolRegisterParameter;
|
||||||
|
import cn.hippo4j.common.model.register.DynamicThreadPoolRegisterWrapper;
|
||||||
|
import cn.hippo4j.common.model.register.notify.DynamicThreadPoolRegisterCoreNotifyParameter;
|
||||||
|
import cn.hippo4j.core.executor.DynamicThreadPoolWrapper;
|
||||||
|
import cn.hippo4j.core.executor.manage.GlobalNotifyAlarmManage;
|
||||||
|
import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage;
|
||||||
|
import cn.hippo4j.core.executor.support.QueueTypeEnum;
|
||||||
|
import cn.hippo4j.core.executor.support.RejectedTypeEnum;
|
||||||
|
import cn.hippo4j.core.executor.support.service.AbstractDynamicThreadPoolService;
|
||||||
|
import cn.hippo4j.core.springboot.starter.config.ExecutorProperties;
|
||||||
|
import cn.hippo4j.message.service.ThreadPoolNotifyAlarm;
|
||||||
|
|
||||||
|
import java.util.concurrent.ThreadPoolExecutor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dynamic thread-pool config service.
|
||||||
|
*/
|
||||||
|
public class DynamicThreadPoolConfigService extends AbstractDynamicThreadPoolService {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ThreadPoolExecutor registerDynamicThreadPool(DynamicThreadPoolRegisterWrapper registerWrapper) {
|
||||||
|
DynamicThreadPoolRegisterParameter registerParameter = registerWrapper.getDynamicThreadPoolRegisterParameter();
|
||||||
|
String threadPoolId = registerParameter.getThreadPoolId();
|
||||||
|
ThreadPoolExecutor dynamicThreadPoolExecutor = buildDynamicThreadPoolExecutor(registerParameter);
|
||||||
|
DynamicThreadPoolWrapper dynamicThreadPoolWrapper = DynamicThreadPoolWrapper.builder()
|
||||||
|
.threadPoolId(threadPoolId)
|
||||||
|
.executor(dynamicThreadPoolExecutor)
|
||||||
|
.build();
|
||||||
|
// Register pool.
|
||||||
|
GlobalThreadPoolManage.registerPool(threadPoolId, dynamicThreadPoolWrapper);
|
||||||
|
ExecutorProperties executorProperties = buildExecutorProperties(registerWrapper);
|
||||||
|
// Register properties.
|
||||||
|
GlobalCoreThreadPoolManage.register(threadPoolId, executorProperties);
|
||||||
|
DynamicThreadPoolRegisterCoreNotifyParameter notifyParameter = registerWrapper.getDynamicThreadPoolRegisterCoreNotifyParameter();
|
||||||
|
ThreadPoolNotifyAlarm notifyAlarm = new ThreadPoolNotifyAlarm(true, notifyParameter.getActiveAlarm(), notifyParameter.getCapacityAlarm());
|
||||||
|
notifyAlarm.setReceives(notifyParameter.getReceives());
|
||||||
|
notifyAlarm.setInterval(notifyParameter.getInterval());
|
||||||
|
// Register notify.
|
||||||
|
GlobalNotifyAlarmManage.put(threadPoolId, notifyAlarm);
|
||||||
|
return dynamicThreadPoolExecutor;
|
||||||
|
}
|
||||||
|
|
||||||
|
private ExecutorProperties buildExecutorProperties(DynamicThreadPoolRegisterWrapper registerWrapper) {
|
||||||
|
DynamicThreadPoolRegisterParameter registerParameter = registerWrapper.getDynamicThreadPoolRegisterParameter();
|
||||||
|
ExecutorProperties executorProperties = ExecutorProperties.builder()
|
||||||
|
.corePoolSize(registerParameter.getCorePoolSize())
|
||||||
|
.maximumPoolSize(registerParameter.getMaximumPoolSize())
|
||||||
|
.allowCoreThreadTimeOut(registerParameter.getAllowCoreThreadTimeOut())
|
||||||
|
.keepAliveTime(registerParameter.getKeepAliveTime())
|
||||||
|
.blockingQueue(QueueTypeEnum.getBlockingQueueNameByType(registerParameter.getQueueType()))
|
||||||
|
.threadNamePrefix(registerParameter.getThreadNamePrefix())
|
||||||
|
.rejectedHandler(RejectedTypeEnum.getRejectedNameByType(registerParameter.getRejectedType()))
|
||||||
|
.executeTimeOut(registerParameter.getExecuteTimeOut())
|
||||||
|
.threadPoolId(registerParameter.getThreadPoolId())
|
||||||
|
.build();
|
||||||
|
return executorProperties;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue