job初始化逻辑优化,job数据源从注解改为从数据库获取

This commit is contained in:
aoshiguchen
2022-09-13 19:22:35 +08:00
parent e3cada7b29
commit 688296a501
9 changed files with 94 additions and 19 deletions
@@ -106,6 +106,7 @@ public class JobExecutor implements ApplicationRunner, IJobExecutor {
for (JobInfo jobInfo : jobInfoList) {
add(jobInfo);
}
log.info("Job初始化完成.");
}
@Override
@@ -25,8 +25,8 @@ import fun.asgc.neutrino.core.annotation.Autowired;
import fun.asgc.neutrino.core.annotation.Bean;
import fun.asgc.neutrino.core.annotation.Component;
import fun.asgc.neutrino.core.base.CustomThreadFactory;
import fun.asgc.neutrino.core.quartz.DefaultJobSource;
import fun.asgc.neutrino.core.quartz.JobExecutor;
import fun.asgc.neutrino.proxy.server.service.JobInfoService;
import fun.asgc.neutrino.proxy.server.service.JobLogService;
import java.util.concurrent.LinkedBlockingQueue;
@@ -42,11 +42,13 @@ import java.util.concurrent.TimeUnit;
public class JobConfig {
@Autowired
private JobLogService jobLogService;
@Autowired
private JobInfoService jobInfoService;
@Bean
public JobExecutor jobExecutor() {
JobExecutor executor = new JobExecutor();
executor.setJobSource(new DefaultJobSource());
executor.setJobSource(jobInfoService);
executor.setThreadPoolExecutor(new ThreadPoolExecutor(5, 20, 10L, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(), new CustomThreadFactory("JobPool")));
executor.setJobCallback(jobLogService);
@@ -70,8 +70,10 @@ public class JobInfoController {
@OnlyAdmin
@PostMapping("execute")
public JobInfoExecuteRes execute(@RequestBody JobInfoExecuteReq req) {
ParamCheckUtil.checkNotNull(req, "req");
ParamCheckUtil.checkNotNull(req.getId(), "id");
return new JobInfoExecuteRes();
return jobInfoService.execute(req);
}
}
@@ -34,6 +34,7 @@ import fun.asgc.neutrino.proxy.server.controller.res.JobInfoListRes;
import fun.asgc.neutrino.proxy.server.dal.entity.JobInfoDO;
import java.util.Date;
import java.util.List;
/**
*
@@ -53,4 +54,8 @@ public interface JobInfoMapper extends SqlMapper {
@Update("update `job_info` set enable = :enable,update_time = :updateTime where id = :id")
void updateEnableStatus(@Param("id") Integer id, @Param("enable") Integer enable, @Param("updateTime") Date updateTime);
@ResultType(JobInfoDO.class)
@Select("select * from job_info where enable = 1")
List<JobInfoDO> findEnableList();
}
@@ -21,14 +21,22 @@
*/
package fun.asgc.neutrino.proxy.server.service;
import com.google.common.collect.Lists;
import fun.asgc.neutrino.core.annotation.Autowired;
import fun.asgc.neutrino.core.annotation.Component;
import fun.asgc.neutrino.core.annotation.NonIntercept;
import fun.asgc.neutrino.core.db.page.Page;
import fun.asgc.neutrino.core.db.page.PageQuery;
import fun.asgc.neutrino.core.quartz.IJobSource;
import fun.asgc.neutrino.core.quartz.JobInfo;
import fun.asgc.neutrino.core.util.CollectionUtil;
import fun.asgc.neutrino.core.util.StringUtil;
import fun.asgc.neutrino.core.web.annotation.RequestBody;
import fun.asgc.neutrino.proxy.server.constant.ExceptionConstant;
import fun.asgc.neutrino.proxy.server.controller.req.JobInfoExecuteReq;
import fun.asgc.neutrino.proxy.server.controller.req.JobInfoListReq;
import fun.asgc.neutrino.proxy.server.controller.req.JobInfoUpdateEnableStatusReq;
import fun.asgc.neutrino.proxy.server.controller.res.JobInfoExecuteRes;
import fun.asgc.neutrino.proxy.server.controller.res.JobInfoListRes;
import fun.asgc.neutrino.proxy.server.controller.res.JobInfoUpdateEnableStatusRes;
import fun.asgc.neutrino.proxy.server.dal.JobInfoMapper;
@@ -37,6 +45,7 @@ import fun.asgc.neutrino.proxy.server.util.ParamCheckUtil;
import lombok.extern.slf4j.Slf4j;
import java.util.Date;
import java.util.List;
/**
*
@@ -46,7 +55,7 @@ import java.util.Date;
@Slf4j
@NonIntercept
@Component
public class JobInfoService {
public class JobInfoService implements IJobSource {
@Autowired
private JobInfoMapper jobInfoMapper;
@@ -59,8 +68,34 @@ public class JobInfoService {
public JobInfoUpdateEnableStatusRes updateEnableStatus(JobInfoUpdateEnableStatusReq req) {
JobInfoDO jobInfoDO = jobInfoMapper.findById(req.getId());
ParamCheckUtil.checkExpression(null != jobInfoDO, ExceptionConstant.JOB_INFO_NOT_EXIST);
ParamCheckUtil.checkNotNull(jobInfoDO, ExceptionConstant.JOB_INFO_NOT_EXIST);
jobInfoMapper.updateEnableStatus(req.getId(), req.getEnable(), new Date());
return new JobInfoUpdateEnableStatusRes();
}
public JobInfoExecuteRes execute(JobInfoExecuteReq req) {
return new JobInfoExecuteRes();
}
@Override
public List<JobInfo> sourceList() {
List<JobInfo> jobInfoList = Lists.newArrayList();
List<JobInfoDO> jobInfoDOList = jobInfoMapper.findEnableList();
if (CollectionUtil.isEmpty(jobInfoDOList)) {
return jobInfoList;
}
for (JobInfoDO item : jobInfoDOList) {
jobInfoList.add(new JobInfo()
.setId(String.valueOf(item.getId()))
.setName(item.getHandler())
.setDesc(item.getDesc())
.setCron(item.getCron())
.setParam(item.getParam())
);
}
return jobInfoList;
}
}
@@ -103,7 +103,7 @@ public class LicenseService {
*/
public LicenseCreateRes create(LicenseCreateReq req) {
LicenseDO licenseDO = licenseMapper.checkRepeat(req.getUserId(), req.getName());
ParamCheckUtil.checkExpression(null == licenseDO, ExceptionConstant.LICENSE_NAME_CANNOT_REPEAT);
ParamCheckUtil.checkNotNull(licenseDO, ExceptionConstant.LICENSE_NAME_CANNOT_REPEAT);
String key = UUID.randomUUID().toString().replaceAll("-", "");
Date now = new Date();
@@ -122,10 +122,10 @@ public class LicenseService {
public LicenseUpdateRes update(LicenseUpdateReq req) {
LicenseDO oldLicenseDO = licenseMapper.findById(req.getId());
ParamCheckUtil.checkExpression(null != oldLicenseDO, ExceptionConstant.LICENSE_NOT_EXIST);
ParamCheckUtil.checkNotNull(oldLicenseDO, ExceptionConstant.LICENSE_NOT_EXIST);
LicenseDO licenseCheck = licenseMapper.checkRepeat(oldLicenseDO.getUserId(), req.getName(), Sets.newHashSet(oldLicenseDO.getId()));
ParamCheckUtil.checkExpression(null == licenseCheck, ExceptionConstant.LICENSE_NAME_CANNOT_REPEAT);
ParamCheckUtil.checkNotNull(licenseCheck, ExceptionConstant.LICENSE_NAME_CANNOT_REPEAT);
licenseMapper.update(req.getId(), req.getName(), new Date());
return new LicenseUpdateRes();
@@ -100,14 +100,14 @@ public class PortMappingService {
public PortMappingCreateRes create(PortMappingCreateReq req) {
LicenseDO licenseDO = licenseMapper.findById(req.getLicenseId());
ParamCheckUtil.checkExpression(null != licenseDO, ExceptionConstant.LICENSE_NOT_EXIST);
ParamCheckUtil.checkNotNull(licenseDO, ExceptionConstant.LICENSE_NOT_EXIST);
if (!SystemContextHolder.isAdmin()) {
// 临时处理,如果当前用户不是管理院,则操作userId不能为1
ParamCheckUtil.checkExpression(!licenseDO.getUserId().equals(1), ExceptionConstant.NO_PERMISSION_VISIT);
}
PortPoolDO portPoolDO = portPoolMapper.findByPort(req.getServerPort());
ParamCheckUtil.checkExpression(null != portPoolDO, ExceptionConstant.PORT_NOT_EXIST);
ParamCheckUtil.checkExpression(null == portMappingMapper.findByPort(req.getServerPort()), ExceptionConstant.PORT_CANNOT_REPEAT_MAPPING, req.getServerPort());
ParamCheckUtil.checkNotNull(portPoolDO, ExceptionConstant.PORT_NOT_EXIST);
ParamCheckUtil.checkNotNull(portMappingMapper.findByPort(req.getServerPort()), ExceptionConstant.PORT_CANNOT_REPEAT_MAPPING, req.getServerPort());
Date now = new Date();
@@ -126,14 +126,14 @@ public class PortMappingService {
public PortMappingUpdateRes update(PortMappingUpdateReq req) {
LicenseDO licenseDO = licenseMapper.findById(req.getLicenseId());
ParamCheckUtil.checkExpression(null != licenseDO, ExceptionConstant.LICENSE_NOT_EXIST);
ParamCheckUtil.checkNotNull(licenseDO, ExceptionConstant.LICENSE_NOT_EXIST);
if (!SystemContextHolder.isAdmin()) {
// 临时处理,如果当前用户不是管理员,则操作userId不能为1
ParamCheckUtil.checkExpression(!licenseDO.getUserId().equals(1), ExceptionConstant.NO_PERMISSION_VISIT);
}
PortPoolDO portPoolDO = portPoolMapper.findByPort(req.getServerPort());
ParamCheckUtil.checkExpression(null != portPoolDO, ExceptionConstant.PORT_NOT_EXIST);
ParamCheckUtil.checkExpression(null == portMappingMapper.findByPort(req.getServerPort(), Sets.newHashSet(req.getId())), ExceptionConstant.PORT_CANNOT_REPEAT_MAPPING, req.getServerPort());
ParamCheckUtil.checkNotNull(portPoolDO, ExceptionConstant.PORT_NOT_EXIST);
ParamCheckUtil.checkNotNull(portMappingMapper.findByPort(req.getServerPort(), Sets.newHashSet(req.getId())), ExceptionConstant.PORT_CANNOT_REPEAT_MAPPING, req.getServerPort());
PortMappingDO portMappingDO = new PortMappingDO();
portMappingDO.setId(req.getId());
@@ -177,10 +177,10 @@ public class PortMappingService {
public PortMappingUpdateEnableStatusRes updateEnableStatus(PortMappingUpdateEnableStatusReq req) {
PortMappingDO portMappingDO = portMappingMapper.findById(req.getId());
ParamCheckUtil.checkExpression(null != portMappingDO, ExceptionConstant.PORT_MAPPING_NOT_EXIST);
ParamCheckUtil.checkNotNull(portMappingDO, ExceptionConstant.PORT_MAPPING_NOT_EXIST);
LicenseDO licenseDO = licenseMapper.findById(portMappingDO.getLicenseId());
ParamCheckUtil.checkExpression(null != licenseDO, ExceptionConstant.LICENSE_NOT_EXIST);
ParamCheckUtil.checkNotNull(licenseDO, ExceptionConstant.LICENSE_NOT_EXIST);
if (!SystemContextHolder.isAdmin()) {
// 临时处理,如果当前用户不是管理员,则操作userId不能为1
ParamCheckUtil.checkExpression(!licenseDO.getUserId().equals(1), ExceptionConstant.NO_PERMISSION_VISIT);
@@ -193,10 +193,10 @@ public class PortMappingService {
public void delete(Integer id) {
PortMappingDO portMappingDO = portMappingMapper.findById(id);
ParamCheckUtil.checkExpression(null != portMappingDO, ExceptionConstant.PORT_MAPPING_NOT_EXIST);
ParamCheckUtil.checkNotNull(portMappingDO, ExceptionConstant.PORT_MAPPING_NOT_EXIST);
LicenseDO licenseDO = licenseMapper.findById(portMappingDO.getLicenseId());
ParamCheckUtil.checkExpression(null != licenseDO, ExceptionConstant.LICENSE_NOT_EXIST);
ParamCheckUtil.checkNotNull(licenseDO, ExceptionConstant.LICENSE_NOT_EXIST);
if (!SystemContextHolder.isAdmin()) {
// 临时处理,如果当前用户不是管理员,则操作userId不能为1
ParamCheckUtil.checkExpression(!licenseDO.getUserId().equals(1), ExceptionConstant.NO_PERMISSION_VISIT);
@@ -65,7 +65,7 @@ public class PortPoolService {
public PortPoolCreateRes create(PortPoolCreateReq req) {
PortPoolDO oldPortPoolDO = portPoolMapper.findByPort(req.getPort());
ParamCheckUtil.checkExpression(null == oldPortPoolDO, ExceptionConstant.PORT_CANNOT_REPEAT);
ParamCheckUtil.checkNotNull(oldPortPoolDO, ExceptionConstant.PORT_CANNOT_REPEAT);
Date now = new Date();
@@ -66,6 +66,36 @@ public class ParamCheckUtil {
}
}
public static void checkNotNull(Object obj, ExceptionConstant constant, Object... params) {
if (null == obj) {
throw ServiceException.create(constant, params);
}
}
public static void checkNotEmpty(String str, ExceptionConstant constant, Object... params) {
if (StringUtil.isEmpty(str)) {
throw ServiceException.create(constant, params);
}
}
public static void checkNotEmpty(Collection collection, ExceptionConstant constant, Object... params) {
if (null == collection || collection.isEmpty()) {
throw ServiceException.create(constant, params);
}
}
public static void checkNotEmpty(Map map, ExceptionConstant constant, Object... params) {
if (null == map || map.isEmpty()) {
throw ServiceException.create(constant, params);
}
}
public static void checkNotEmpty(Set set, ExceptionConstant constant, Object... params) {
if (null == set || set.isEmpty()) {
throw ServiceException.create(constant, params);
}
}
public static void checkExpression(boolean expression, ExceptionConstant constant, Object... params) {
if (!expression) {
throw ServiceException.create(constant, params);