diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/quartz/JobExecutor.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/quartz/JobExecutor.java index 08a35f4b..4cb24a5d 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/quartz/JobExecutor.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/quartz/JobExecutor.java @@ -106,6 +106,7 @@ public class JobExecutor implements ApplicationRunner, IJobExecutor { for (JobInfo jobInfo : jobInfoList) { add(jobInfo); } + log.info("Job初始化完成."); } @Override diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/config/JobConfig.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/config/JobConfig.java index 18ad334e..749de7c9 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/config/JobConfig.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/config/JobConfig.java @@ -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); diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/controller/JobInfoController.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/controller/JobInfoController.java index f3ef886b..3e6a79c4 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/controller/JobInfoController.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/controller/JobInfoController.java @@ -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); } } diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/dal/JobInfoMapper.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/dal/JobInfoMapper.java index abc22e99..085424f4 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/dal/JobInfoMapper.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/dal/JobInfoMapper.java @@ -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 findEnableList(); } diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/JobInfoService.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/JobInfoService.java index f2b7f9fc..f18079c9 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/JobInfoService.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/JobInfoService.java @@ -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 sourceList() { + List jobInfoList = Lists.newArrayList(); + List 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; + } } diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/LicenseService.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/LicenseService.java index 7a4c5215..0895ad95 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/LicenseService.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/LicenseService.java @@ -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(); diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/PortMappingService.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/PortMappingService.java index 25aaf1ad..fd9843f3 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/PortMappingService.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/PortMappingService.java @@ -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); diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/PortPoolService.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/PortPoolService.java index e4166fa0..5fef3d10 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/PortPoolService.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/PortPoolService.java @@ -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(); diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/util/ParamCheckUtil.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/util/ParamCheckUtil.java index a3e89174..f1980e10 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/util/ParamCheckUtil.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/util/ParamCheckUtil.java @@ -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);