添加线程池核心线程超时, 重构代码.

pull/39/head
chen.ma 3 years ago
parent f49e41d370
commit a047a25466

@ -18,7 +18,7 @@ public class PermissionInfo {
/**
* id
*/
@TableId
@TableId(type = IdType.AUTO)
private Long id;
/**

@ -18,7 +18,7 @@ public class RoleInfo {
/**
* id
*/
@TableId
@TableId(type = IdType.AUTO)
private Long id;
/**

@ -18,7 +18,7 @@ public class UserInfo {
/**
* id
*/
@TableId
@TableId(type = IdType.AUTO)
private Long id;
/**

@ -0,0 +1,46 @@
package cn.hippo4j.common.enums;
import java.util.Objects;
/**
* Enable enum.
*
* @author chen.ma
* @date 2021/12/21 20:34
*/
public enum EnableEnum {
/**
* True.
*/
YES("1"),
/**
* False.
*/
NO("0");
private final String statusCode;
EnableEnum(String statusCode) {
this.statusCode = statusCode;
}
public String getCode() {
return this.statusCode;
}
public Integer getIntCode() {
return Integer.parseInt(this.statusCode);
}
@Override
public String toString() {
return statusCode;
}
public static boolean getBool(Integer intStatusCode) {
return Objects.equals(intStatusCode, EnableEnum.YES.getIntCode()) ? true : false;
}
}

@ -92,4 +92,11 @@ public interface PoolParameter {
*/
Integer getLivenessAlarm();
/**
* allowCoreThreadTimeOut
*
* @return
*/
Integer getAllowCoreThreadTimeOut();
}

@ -82,4 +82,9 @@ public class PoolParameterInfo implements PoolParameter, Serializable {
*/
private Integer livenessAlarm;
/**
* allowCoreThreadTimeOut
*/
private Integer allowCoreThreadTimeOut;
}

@ -25,6 +25,7 @@ public class ContentUtil {
.setIsAlarm(parameter.getIsAlarm())
.setCapacityAlarm(parameter.getCapacityAlarm())
.setLivenessAlarm(parameter.getLivenessAlarm())
.setAllowCoreThreadTimeOut(parameter.getAllowCoreThreadTimeOut())
.setRejectedType(parameter.getRejectedType());
return JSONUtil.toJSONString(poolInfo);
}

@ -1,6 +1,6 @@
package cn.hippo4j.config.config;
import cn.hippo4j.config.enums.DelEnum;
import cn.hippo4j.common.enums.DelEnum;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;

@ -84,6 +84,11 @@ public class ConfigInfoBase implements Serializable {
*/
private Integer livenessAlarm;
/**
* allowCoreThreadTimeOut
*/
private Integer allowCoreThreadTimeOut;
/**
* MD5
*/

@ -1,8 +1,6 @@
package cn.hippo4j.config.model;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import java.util.Date;
@ -20,6 +18,7 @@ public class NotifyInfo {
/**
* id
*/
@TableId(type = IdType.AUTO)
private Long id;
/**

@ -84,6 +84,11 @@ public class ThreadPoolRespDTO {
*/
private Integer rejectedType;
/**
* allowCoreThreadTimeOut
*/
private Integer allowCoreThreadTimeOut;
/**
* gmtCreate
*/

@ -71,4 +71,9 @@ public class ThreadPoolSaveOrUpdateReqDTO {
*/
private Integer rejectedType;
/**
* allowCoreThreadTimeOut
*/
private Integer allowCoreThreadTimeOut;
}

@ -1,7 +1,7 @@
package cn.hippo4j.config.service.biz.impl;
import cn.hippo4j.common.toolkit.Assert;
import cn.hippo4j.config.enums.DelEnum;
import cn.hippo4j.common.enums.DelEnum;
import cn.hippo4j.config.mapper.ItemInfoMapper;
import cn.hippo4j.config.model.ItemInfo;
import cn.hippo4j.config.model.biz.item.ItemQueryReqDTO;
@ -107,7 +107,7 @@ public class ItemServiceImpl implements ItemService {
public void deleteItem(String namespace, String itemId) {
List<ThreadPoolRespDTO> itemList = threadPoolService.getThreadPoolByItemId(itemId);
if (CollectionUtils.isNotEmpty(itemList)) {
throw new RuntimeException("The project contains a thread pool reference, and the deletion failed.");
throw new RuntimeException("项目包含线程池引用, 删除失败.");
}
int updateResult = itemInfoMapper.update(new ItemInfo(),

@ -2,7 +2,7 @@ package cn.hippo4j.config.service.biz.impl;
import cn.hippo4j.common.toolkit.GroupKey;
import cn.hippo4j.common.web.exception.ServiceException;
import cn.hippo4j.config.enums.DelEnum;
import cn.hippo4j.common.enums.EnableEnum;
import cn.hippo4j.config.mapper.NotifyInfoMapper;
import cn.hippo4j.config.model.NotifyInfo;
import cn.hippo4j.config.model.biz.notify.NotifyListRespDTO;
@ -110,7 +110,7 @@ public class NotifyServiceImpl implements NotifyService {
.eq(NotifyInfo::getTenantId, parseKey[2])
.eq(NotifyInfo::getItemId, parseKey[1])
.eq(NotifyInfo::getTpId, parseKey[0])
.eq(NotifyInfo::getEnable, DelEnum.NORMAL)
.eq(NotifyInfo::getEnable, EnableEnum.YES.getIntCode())
.eq(NotifyInfo::getType, type);
List<NotifyInfo> notifyInfos = notifyInfoMapper.selectList(queryWrapper);
return notifyInfos;

@ -1,7 +1,7 @@
package cn.hippo4j.config.service.biz.impl;
import cn.hippo4j.common.toolkit.Assert;
import cn.hippo4j.config.enums.DelEnum;
import cn.hippo4j.common.enums.DelEnum;
import cn.hippo4j.config.mapper.TenantInfoMapper;
import cn.hippo4j.config.model.TenantInfo;
import cn.hippo4j.config.model.biz.item.ItemQueryReqDTO;
@ -108,7 +108,7 @@ public class TenantServiceImpl implements TenantService {
reqDTO.setTenantId(tenantId);
List<ItemRespDTO> itemList = itemService.queryItem(reqDTO);
if (CollectionUtils.isNotEmpty(itemList)) {
throw new RuntimeException("The line of business contains project references, and the deletion failed.");
throw new RuntimeException("租户包含项目引用, 删除失败.");
}
int updateResult = tenantInfoMapper.update(new TenantInfo(),

@ -1,6 +1,6 @@
package cn.hippo4j.config.service.biz.impl;
import cn.hippo4j.config.enums.DelEnum;
import cn.hippo4j.common.enums.DelEnum;
import cn.hippo4j.config.mapper.ConfigInfoMapper;
import cn.hippo4j.config.model.ConfigAllInfo;
import cn.hippo4j.config.model.biz.threadpool.ThreadPoolDelReqDTO;

@ -2,7 +2,7 @@ package cn.hippo4j.console.service.impl;
import cn.hippo4j.common.model.InstanceInfo;
import cn.hippo4j.common.toolkit.GroupKey;
import cn.hippo4j.config.enums.DelEnum;
import cn.hippo4j.common.enums.DelEnum;
import cn.hippo4j.config.mapper.ConfigInfoMapper;
import cn.hippo4j.config.mapper.HisRunDataMapper;
import cn.hippo4j.config.mapper.ItemInfoMapper;

@ -58,9 +58,10 @@ CREATE TABLE `config` (
`capacity` int(11) DEFAULT NULL COMMENT '队列大小',
`rejected_type` int(11) DEFAULT NULL COMMENT '拒绝策略',
`keep_alive_time` int(11) DEFAULT NULL COMMENT '线程存活时间',
`allow_core_thread_time_out` tinyint(1) DEFAULT NULL COMMENT '允许核心线程超时',
`content` longtext COMMENT '线程池内容',
`md5` varchar(32) NOT NULL COMMENT 'MD5',
`is_alarm` tinyint(1) DEFAULT NULL COMMENT '是否报警 0报警 1:不报警',
`is_alarm` tinyint(1) DEFAULT NULL COMMENT '是否报警',
`capacity_alarm` int(11) DEFAULT NULL COMMENT '容量报警',
`liveness_alarm` int(11) DEFAULT NULL COMMENT '活跃度报警',
`gmt_create` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
@ -145,7 +146,7 @@ CREATE TABLE `log_record_info` (
/******************************************/
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` bigint(20) NOT NULL COMMENT 'ID',
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`user_name` varchar(64) NOT NULL COMMENT '用户名',
`password` varchar(512) NOT NULL COMMENT '用户密码',
`role` varchar(50) NOT NULL COMMENT '角色',
@ -153,7 +154,7 @@ CREATE TABLE `user` (
`gmt_modified` datetime NOT NULL COMMENT '修改时间',
`del_flag` tinyint(1) NOT NULL COMMENT '是否删除',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户表';
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='用户表';
/******************************************/
/* 数据库全名 = hippo4j_manager */
@ -161,14 +162,14 @@ CREATE TABLE `user` (
/******************************************/
DROP TABLE IF EXISTS `role`;
CREATE TABLE `role` (
`id` bigint(20) NOT NULL COMMENT 'ID',
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`role` varchar(64) NOT NULL COMMENT '角色',
`user_name` varchar(64) NOT NULL COMMENT '用户名',
`gmt_create` datetime NOT NULL COMMENT '创建时间',
`gmt_modified` datetime NOT NULL COMMENT '修改时间',
`del_flag` tinyint(1) NOT NULL COMMENT '是否删除',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='角色表';
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='角色表';
/******************************************/
/* 数据库全名 = hippo4j_manager */
@ -176,7 +177,7 @@ CREATE TABLE `role` (
/******************************************/
DROP TABLE IF EXISTS `permission`;
CREATE TABLE `permission` (
`id` bigint(20) NOT NULL COMMENT 'ID',
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`role` varchar(512) NOT NULL COMMENT '角色',
`resource` varchar(512) NOT NULL COMMENT '资源',
`action` varchar(8) NOT NULL COMMENT '读写权限',
@ -184,7 +185,7 @@ CREATE TABLE `permission` (
`gmt_modified` datetime NOT NULL COMMENT '修改时间',
`del_flag` tinyint(1) NOT NULL COMMENT '是否删除',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='权限表';
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='权限表';
/******************************************/
/* 数据库全名 = hippo4j_manager */
@ -207,7 +208,7 @@ CREATE TABLE `notify` (
`del_flag` tinyint(1) NOT NULL COMMENT '是否删除',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_notify_biz_key` (`tenant_id`,`item_id`,`tp_id`,`platform`,`type`,`del_flag`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='通知表';
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='通知表';
/* 租户 */
INSERT INTO `tenant` (`id`, `tenant_id`, `tenant_name`, `tenant_desc`, `owner`, `gmt_create`, `gmt_modified`, `del_flag`) VALUES ('1', 'prescription', '处方组', '负责维护处方服务, 包括不限于电子处方等业务', '谢良辰', '2021-10-24 13:42:11', '2021-10-24 13:42:11', '0');
@ -223,5 +224,5 @@ INSERT INTO `config` (`id`, `tenant_id`, `item_id`, `tp_id`, `tp_name`, `core_si
INSERT INTO `user` (`id`, `user_name`, `password`, `role`, `gmt_create`, `gmt_modified`, `del_flag`) VALUES ('1', 'admin', '$2a$10$2KCqRbra0Yn2TwvkZxtfLuWuUP5KyCWsljO/ci5pLD27pqR3TV1vy', 'ROLE_ADMIN', '2021-11-04 21:35:17', '2021-11-15 23:04:59', '0');
/* 通知表 */
INSERT INTO `notify` (`id`, `tenant_id`, `item_id`, `tp_id`, `platform`, `type`, `secret_key`, `interval`, `receives`, `enable`, `gmt_create`, `gmt_modified`, `del_flag`) VALUES ('1461345908531671042', 'prescription', 'dynamic-threadpool-example', 'message-produce', 'DING', 'CONFIG', '4a582a588a161d6e3a1bd1de7eea9ee9f562cdfcbe56b6e72029e7fd512b2eae', NULL, '15601166691', '0', '2021-11-18 22:49:50', '2021-11-18 22:49:50', '0'),
('1461345976047382530', 'prescription', 'dynamic-threadpool-example', 'message-produce', 'DING', 'ALARM', '4a582a588a161d6e3a1bd1de7eea9ee9f562cdfcbe56b6e72029e7fd512b2eae', '30', '15601166691', '0', '2021-11-18 22:50:06', '2021-11-18 22:50:06', '0');
INSERT INTO `notify` (`id`, `tenant_id`, `item_id`, `tp_id`, `platform`, `type`, `secret_key`, `interval`, `receives`, `enable`, `gmt_create`, `gmt_modified`, `del_flag`) VALUES ('1', 'prescription', 'dynamic-threadpool-example', 'message-produce', 'DING', 'CONFIG', '4a582a588a161d6e3a1bd1de7eea9ee9f562cdfcbe56b6e72029e7fd512b2eae', NULL, '15601166691', '0', '2021-11-18 22:49:50', '2021-11-18 22:49:50', '0'),
('2', 'prescription', 'dynamic-threadpool-example', 'message-produce', 'DING', 'ALARM', '4a582a588a161d6e3a1bd1de7eea9ee9f562cdfcbe56b6e72029e7fd512b2eae', '30', '15601166691', '0', '2021-11-18 22:50:06', '2021-11-18 22:50:06', '0');

@ -2,6 +2,7 @@ package cn.hippo4j.starter.core;
import cn.hippo4j.common.config.ApplicationContextHolder;
import cn.hippo4j.common.constant.Constants;
import cn.hippo4j.common.enums.EnableEnum;
import cn.hippo4j.common.model.PoolParameterInfo;
import cn.hippo4j.common.toolkit.JSONUtil;
import cn.hippo4j.common.web.base.Result;
@ -121,6 +122,7 @@ public final class DynamicThreadPoolPostProcessor implements BeanPostProcessor {
.keepAliveTime(ppi.getKeepAliveTime(), TimeUnit.SECONDS)
.rejected(RejectedTypeEnum.createPolicy(ppi.getRejectedType()))
.alarmConfig(ppi.getIsAlarm(), ppi.getCapacityAlarm(), ppi.getLivenessAlarm())
.allowCoreThreadTimeOut(EnableEnum.getBool(ppi.getAllowCoreThreadTimeOut()))
.build();
if (poolExecutor instanceof DynamicExecutorConfigurationSupport) {

@ -1,5 +1,6 @@
package cn.hippo4j.starter.core;
import cn.hippo4j.common.enums.EnableEnum;
import cn.hippo4j.common.model.PoolParameterInfo;
import cn.hippo4j.common.toolkit.JSONUtil;
import cn.hippo4j.starter.alarm.ThreadPoolAlarmManage;
@ -37,11 +38,13 @@ public class ThreadPoolDynamicRefresh {
int originalCapacity = executor.getQueue().remainingCapacity() + executor.getQueue().size();
long originalKeepAliveTime = executor.getKeepAliveTime(TimeUnit.SECONDS);
String originalRejected = executor.getRejectedExecutionHandler().getClass().getSimpleName();
boolean originalAllowCoreThreadTimeOut = executor.allowsCoreThreadTimeOut();
changePoolInfo(executor, parameter);
ThreadPoolExecutor afterExecutor = GlobalThreadPoolManage.getExecutorService(threadPoolId).getExecutor();
log.info("[🔥 {}] Changed thread pool. \ncoreSize :: [{}], maxSize :: [{}], queueType :: [{}], capacity :: [{}], keepAliveTime :: [{}], rejectedType :: [{}]",
log.info(
"[🔥 {}] Changed thread pool. \ncoreSize :: [{}], maxSize :: [{}], queueType :: [{}], capacity :: [{}], keepAliveTime :: [{}], rejectedType :: [{}], allowCoreThreadTimeOut :: [{}]",
threadPoolId.toUpperCase(),
String.format("%s => %s", originalCoreSize, afterExecutor.getCorePoolSize()),
String.format("%s => %s", originalMaximumPoolSize, afterExecutor.getMaximumPoolSize()),
@ -49,7 +52,9 @@ public class ThreadPoolDynamicRefresh {
String.format("%s => %s", originalCapacity,
(afterExecutor.getQueue().remainingCapacity() + afterExecutor.getQueue().size())),
String.format("%s => %s", originalKeepAliveTime, afterExecutor.getKeepAliveTime(TimeUnit.SECONDS)),
String.format("%s => %s", originalRejected, RejectedTypeEnum.getRejectedNameByType(parameter.getRejectedType())));
String.format("%s => %s", originalRejected, RejectedTypeEnum.getRejectedNameByType(parameter.getRejectedType())),
String.format("%s => %s", originalAllowCoreThreadTimeOut, EnableEnum.getBool(parameter.getAllowCoreThreadTimeOut()))
);
}
public static void changePoolInfo(ThreadPoolExecutor executor, PoolParameterInfo parameter) {
@ -78,6 +83,10 @@ public class ThreadPoolDynamicRefresh {
if (parameter.getRejectedType() != null) {
executor.setRejectedExecutionHandler(RejectedTypeEnum.createPolicy(parameter.getRejectedType()));
}
if (parameter.getAllowCoreThreadTimeOut() != null) {
executor.allowCoreThreadTimeOut(EnableEnum.getBool(parameter.getAllowCoreThreadTimeOut()));
}
}
}

@ -1,6 +1,6 @@
package cn.hippo4j.starter.toolkit.thread;
import cn.hippo4j.common.enums.EnableEnum;
import cn.hippo4j.common.toolkit.Assert;
import cn.hippo4j.starter.alarm.ThreadPoolAlarm;
import org.springframework.core.task.TaskDecorator;
@ -212,7 +212,7 @@ public class ThreadPoolBuilder implements Builder<ThreadPoolExecutor> {
}
public ThreadPoolBuilder alarmConfig(int isAlarm, int capacityAlarm, int livenessAlarm) {
this.isAlarm = isAlarm == 0 ? true : false;
this.isAlarm = isAlarm == EnableEnum.YES.getIntCode() ? true : false;
this.capacityAlarm = capacityAlarm;
this.livenessAlarm = livenessAlarm;
return this;

Loading…
Cancel
Save