diff --git a/austin-common/src/main/java/com/java3y/austin/constant/AustinConstant.java b/austin-common/src/main/java/com/java3y/austin/constant/AustinConstant.java index 7888082..8f81425 100644 --- a/austin-common/src/main/java/com/java3y/austin/constant/AustinConstant.java +++ b/austin-common/src/main/java/com/java3y/austin/constant/AustinConstant.java @@ -20,6 +20,11 @@ public class AustinConstant { */ public final static String YYYY_MM_DD = "yyyyMMdd"; + /** + * cron时间格式 + */ + public final static String CRON_FORMAT = "ss mm HH dd MM ? yyyy"; + /** * apollo默认的值 diff --git a/austin-cron/src/main/java/com/java3y/austin/constants/XxlJobConstant.java b/austin-cron/src/main/java/com/java3y/austin/constants/XxlJobConstant.java index 6566ceb..4883d9e 100644 --- a/austin-cron/src/main/java/com/java3y/austin/constants/XxlJobConstant.java +++ b/austin-cron/src/main/java/com/java3y/austin/constants/XxlJobConstant.java @@ -9,7 +9,7 @@ package com.java3y.austin.constants; public class XxlJobConstant { /** - * 接口路径 + * 任务信息接口路径 */ public static final String LOGIN_URL = "/login"; public static final String INSERT_URL = "/jobinfo/add"; @@ -19,9 +19,16 @@ public class XxlJobConstant { public static final String STOP_URL = "/jobinfo/stop"; /** - * 执行器名称 + * 执行器组接口路径 */ - public static final String HANDLER_NAME = "austinJobHandler"; + public static final String JOB_GROUP_PAGE_LIST = "/jobgroup/pageList"; + public static final String JOB_GROUP_INSERT_URL = "/jobgroup/save"; + + + /** + * 执行任务名称 + */ + public static final String JOB_HANDLER_NAME = "austinJob"; /** * 超时时间 @@ -33,4 +40,9 @@ public class XxlJobConstant { */ public static final Integer RETRY_COUNT = 2; + /** + * 立即执行的任务 延迟时间(秒数) + */ + public static final Integer DELAY_TIME = 5; + } diff --git a/austin-cron/src/main/java/com/java3y/austin/entity/XxlJobGroup.java b/austin-cron/src/main/java/com/java3y/austin/entity/XxlJobGroup.java new file mode 100644 index 0000000..1783956 --- /dev/null +++ b/austin-cron/src/main/java/com/java3y/austin/entity/XxlJobGroup.java @@ -0,0 +1,54 @@ +package com.java3y.austin.entity; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.experimental.Accessors; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.List; + +/** + * 执行器组信息 + * + * @author 3y + */ +@Data +@Accessors(chain = true) +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class XxlJobGroup { + + private int id; + private String appname; + + private String title; + /** + * 执行器地址类型:0=自动注册、1=手动录入 + */ + private int addressType; + + /** + * 执行器地址列表,多地址逗号分隔(手动录入) + */ + private String addressList; + private Date updateTime; + + /** + * registry list 执行器地址列表(系统注册) + */ + private List registryList; + + public List getRegistryList() { + if (addressList != null && addressList.trim().length() > 0) { + registryList = new ArrayList(Arrays.asList(addressList.split(","))); + } + return registryList; + } + + +} diff --git a/austin-cron/src/main/java/com/java3y/austin/entity/XxlJobInfo.java b/austin-cron/src/main/java/com/java3y/austin/entity/XxlJobInfo.java index 95e1ef9..5993ebb 100644 --- a/austin-cron/src/main/java/com/java3y/austin/entity/XxlJobInfo.java +++ b/austin-cron/src/main/java/com/java3y/austin/entity/XxlJobInfo.java @@ -20,38 +20,104 @@ import java.util.Date; @Builder public class XxlJobInfo implements Serializable { - private Integer id; // 主键ID + /** + * 主键ID + */ + private Integer id; - private int jobGroup; // 执行器主键ID + /** + * 执行器主键ID + */ + private int jobGroup; private String jobDesc; private Date addTime; private Date updateTime; - private String author; // 负责人 - private String alarmEmail; // 报警邮件 + /** + * 负责人 + */ + private String author; - private String scheduleType; // 调度类型 - private String scheduleConf; // 调度配置,值含义取决于调度类型 + /** + * 报警邮件 + */ + private String alarmEmail; - private String misfireStrategy; // 调度过期策略 + /** + * 调度类型 + */ + private String scheduleType; + /** + * 调度配置,值含义取决于调度类型 + */ + private String scheduleConf; - private String executorRouteStrategy; // 执行器路由策略 - private String executorHandler; // 执行器,任务Handler名称 - private String executorParam; // 执行器,任务参数 - private String executorBlockStrategy; // 阻塞处理策略 - private int executorTimeout; // 任务执行超时时间,单位秒 - private int executorFailRetryCount; // 失败重试次数 + /** + * 调度过期策略 + */ + private String misfireStrategy; - private String glueType; // GLUE类型 #com.xxl.job.core.glue.GlueTypeEnum - private String glueSource; // GLUE源代码 - private String glueRemark; // GLUE备注 - private Date glueUpdatetime; // GLUE更新时间 + /** + * 执行器路由策略 + */ + private String executorRouteStrategy; - private String childJobId; // 子任务ID,多个逗号分隔 + /** + * 执行器,任务Handler名称 + */ + private String executorHandler; + /** + * 执行器,任务参数 + */ + private String executorParam; - private int triggerStatus; // 调度状态:0-停止,1-运行 - private long triggerLastTime; // 上次调度时间 - private long triggerNextTime; // 下次调度时间 + /** + * 阻塞处理策略 + */ + private String executorBlockStrategy; + /** + * 任务执行超时时间,单位秒 + */ + private int executorTimeout; + /** + * 失败重试次数 + */ + private int executorFailRetryCount; + + /** + * GLUE类型 #com.xxl.job.core.glue.GlueTypeEnum + */ + private String glueType; + /** + * GLUE源代码 + */ + private String glueSource; + /** + * GLUE备注 + */ + private String glueRemark; + /** + * GLUE更新时间 + */ + private Date glueUpdatetime; + + /** + * 子任务ID,多个逗号分隔 + */ + private String childJobId; + + /** + * 调度状态:0-停止,1-运行 + */ + private int triggerStatus; + /** + * 上次调度时间 + */ + private long triggerLastTime; + /** + * 下次调度时间 + */ + private long triggerNextTime; } diff --git a/austin-cron/src/main/java/com/java3y/austin/handler/CronTaskHandler.java b/austin-cron/src/main/java/com/java3y/austin/handler/CronTaskHandler.java index ff537a5..b296b5c 100644 --- a/austin-cron/src/main/java/com/java3y/austin/handler/CronTaskHandler.java +++ b/austin-cron/src/main/java/com/java3y/austin/handler/CronTaskHandler.java @@ -1,19 +1,28 @@ package com.java3y.austin.handler; +import com.alibaba.fastjson.JSON; +import com.java3y.austin.domain.MessageTemplate; +import com.xxl.job.core.context.XxlJobHelper; import com.xxl.job.core.handler.annotation.XxlJob; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; + +/** + * 定时任务处理类 + * @author 3y + */ @Service @Slf4j public class CronTaskHandler { /** - * 简单任务 + * 处理所有的 austin 定时任务消息 */ - @XxlJob("austinJobHandler") + @XxlJob("austinJob") public void execute() { log.info("XXL-JOB, Hello World."); + MessageTemplate messageTemplate = JSON.parseObject(XxlJobHelper.getJobParam(), MessageTemplate.class); } } diff --git a/austin-cron/src/main/java/com/java3y/austin/service/CronTaskService.java b/austin-cron/src/main/java/com/java3y/austin/service/CronTaskService.java index 01ccfc6..a9b155d 100644 --- a/austin-cron/src/main/java/com/java3y/austin/service/CronTaskService.java +++ b/austin-cron/src/main/java/com/java3y/austin/service/CronTaskService.java @@ -1,5 +1,6 @@ package com.java3y.austin.service; +import com.java3y.austin.entity.XxlJobGroup; import com.java3y.austin.entity.XxlJobInfo; import com.java3y.austin.vo.BasicResultVO; @@ -39,4 +40,16 @@ public interface CronTaskService { BasicResultVO stopCronTask(Integer taskId); + /** + * 得到执行器Id + * + * @return + */ + BasicResultVO getGroupId(String appName, String title); + + /** + * 创建执行器 + */ + BasicResultVO createGroup(XxlJobGroup xxlJobGroup); + } diff --git a/austin-cron/src/main/java/com/java3y/austin/service/impl/CronTaskServiceImpl.java b/austin-cron/src/main/java/com/java3y/austin/service/impl/CronTaskServiceImpl.java index a430622..0f19207 100644 --- a/austin-cron/src/main/java/com/java3y/austin/service/impl/CronTaskServiceImpl.java +++ b/austin-cron/src/main/java/com/java3y/austin/service/impl/CronTaskServiceImpl.java @@ -5,6 +5,7 @@ import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpResponse; import com.alibaba.fastjson.JSON; import com.java3y.austin.constants.XxlJobConstant; +import com.java3y.austin.entity.XxlJobGroup; import com.java3y.austin.entity.XxlJobInfo; import com.java3y.austin.enums.RespStatusEnum; import com.java3y.austin.service.CronTaskService; @@ -38,39 +39,25 @@ public class CronTaskServiceImpl implements CronTaskService { @Override public BasicResultVO saveCronTask(XxlJobInfo xxlJobInfo) { Map params = JSON.parseObject(JSON.toJSONString(xxlJobInfo), Map.class); - - String path; - if (xxlJobInfo.getId() == null) { - path = xxlAddresses + XxlJobConstant.INSERT_URL; - } else { - path = xxlAddresses + XxlJobConstant.UPDATE_URL; - } - //XxlJobInfo.builder().jobGroup(1).jobDesc() -// Map paramMap = new HashMap<>(); -// paramMap.put("jobGroup", 1); -// paramMap.put("jobDesc", "这是测试任务"); -// paramMap.put("executorRouteStrategy", "FIRST"); -// paramMap.put("cronGen_display", "* * * * * ? *"); -// paramMap.put("scheduleConf", "* * * * * ? *"); -// paramMap.put("year", "2"); -// paramMap.put("misfireStrategy", "DO_NOTHING"); -// paramMap.put("glueType", "BEAN"); -// paramMap.put("schedule_conf_CRON", "* * * * * ? *"); -// paramMap.put("executorHandler", "messageJob"); // 此处hander需提前在项目中定义 -// paramMap.put("executorBlockStrategy", "SERIAL_EXECUTION"); -// paramMap.put("executorTimeout", 0); -// paramMap.put("executorFailRetryCount", 1); -// paramMap.put("author", "admin"); -// paramMap.put("glueRemark", "GLUE代码初始化"); -// paramMap.put("triggerStatus", 1); -// paramMap.put("scheduleType", "CRON"); - - HttpResponse response = HttpRequest.post(path).form(params).cookie(getCookie()).execute(); - if (!response.isOk()) { - log.error("TaskService#saveTask fail:{}", JSON.toJSONString(response.body())); + String path = xxlJobInfo.getId() == null ? xxlAddresses + XxlJobConstant.INSERT_URL + : xxlAddresses + XxlJobConstant.UPDATE_URL; + + HttpResponse response = null; + try { + response = HttpRequest.post(path).form(params).cookie(getCookie()).execute(); + + // 插入时需要返回Id,而更新时不需要 + if (path.contains(XxlJobConstant.INSERT_URL) && response.isOk()) { + Integer taskId = Integer.parseInt(String.valueOf(JSON.parseObject(response.body()).get("content"))); + return BasicResultVO.success(taskId); + } else if (response.isOk()) { + return BasicResultVO.success(); + } + } catch (Exception e) { + log.error("CronTaskService#saveTask fail:{}", JSON.toJSONString(response.body())); return BasicResultVO.fail(RespStatusEnum.SERVICE_ERROR, JSON.toJSONString(response.body())); } - return BasicResultVO.success(JSON.parseObject(response.body())); + return BasicResultVO.fail(RespStatusEnum.SERVICE_ERROR, JSON.toJSONString(response.body())); } @Override @@ -80,7 +67,7 @@ public class CronTaskServiceImpl implements CronTaskService { String path = xxlAddresses + XxlJobConstant.DELETE_URL; HttpResponse response = HttpRequest.post(path).form(params).cookie(getCookie()).execute(); if (!response.isOk()) { - log.error("TaskService#deleteCronTask fail:{}", JSON.toJSONString(response.body())); + log.error("CronTaskService#deleteCronTask fail:{}", JSON.toJSONString(response.body())); return BasicResultVO.fail(RespStatusEnum.SERVICE_ERROR, JSON.toJSONString(response.body())); } return BasicResultVO.success(); @@ -90,10 +77,11 @@ public class CronTaskServiceImpl implements CronTaskService { public BasicResultVO startCronTask(Integer taskId) { HashMap params = new HashMap<>(); params.put("id", taskId); + String path = xxlAddresses + XxlJobConstant.RUN_URL; HttpResponse response = HttpRequest.post(path).form(params).cookie(getCookie()).execute(); if (!response.isOk()) { - log.error("TaskService#startCronTask fail:{}", JSON.toJSONString(response.body())); + log.error("CronTaskService#startCronTask fail:{}", JSON.toJSONString(response.body())); return BasicResultVO.fail(RespStatusEnum.SERVICE_ERROR, JSON.toJSONString(response.body())); } return BasicResultVO.success(); @@ -107,7 +95,41 @@ public class CronTaskServiceImpl implements CronTaskService { String path = xxlAddresses + XxlJobConstant.STOP_URL; HttpResponse response = HttpRequest.post(path).form(params).cookie(getCookie()).execute(); if (!response.isOk()) { - log.error("TaskService#stopCronTask fail:{}", JSON.parseObject(response.body())); + log.error("CronTaskService#stopCronTask fail:{}", JSON.toJSONString(response.body())); + return BasicResultVO.fail(RespStatusEnum.SERVICE_ERROR, JSON.toJSONString(response.body())); + } + return BasicResultVO.success(); + } + + @Override + public BasicResultVO getGroupId(String appName, String title) { + String path = xxlAddresses + XxlJobConstant.JOB_GROUP_PAGE_LIST; + + HashMap params = new HashMap<>(); + params.put("appname", appName); + params.put("title", title); + + HttpResponse response = null; + try { + response = HttpRequest.post(path).form(params).cookie(getCookie()).execute(); + Integer id = JSON.parseObject(response.body()).getJSONArray("data").getJSONObject(0).getInteger("id"); + if (response.isOk() && id != null) { + return BasicResultVO.success(id); + } + } catch (Exception e) { + log.error("CronTaskService#getGroupId fail:{}", JSON.toJSONString(response.body())); + return BasicResultVO.fail(RespStatusEnum.SERVICE_ERROR, JSON.toJSONString(response.body())); + } + return BasicResultVO.fail(RespStatusEnum.SERVICE_ERROR, JSON.toJSONString(response.body())); + } + + @Override + public BasicResultVO createGroup(XxlJobGroup xxlJobGroup) { + Map params = JSON.parseObject(JSON.toJSONString(xxlJobGroup), Map.class); + String path = xxlAddresses + XxlJobConstant.JOB_GROUP_INSERT_URL; + HttpResponse response = HttpRequest.post(path).form(params).cookie(getCookie()).execute(); + if (!response.isOk()) { + log.error("CronTaskService#createGroup fail:{}", JSON.toJSONString(response.body())); return BasicResultVO.fail(RespStatusEnum.SERVICE_ERROR, JSON.toJSONString(response.body())); } return BasicResultVO.success(); @@ -119,13 +141,12 @@ public class CronTaskServiceImpl implements CronTaskService { * @return String */ private String getCookie() { - String path = xxlAddresses + XxlJobConstant.LOGIN_URL; Map hashMap = new HashMap<>(); hashMap.put("userName", xxlUserName); hashMap.put("password", xxlPassword); hashMap.put("randomCode", IdUtil.fastSimpleUUID()); - log.info("TaskService#getCookie params:{}", hashMap); + String path = xxlAddresses + XxlJobConstant.LOGIN_URL; HttpResponse response = HttpRequest.post(path).form(hashMap).execute(); if (response.isOk()) { List cookies = response.getCookies(); @@ -135,7 +156,7 @@ public class CronTaskServiceImpl implements CronTaskService { } return sb.toString(); } - log.error("TaskService#getCookie fail:{}", JSON.parseObject(response.body())); + log.error("CronTaskService#getCookie fail:{}", JSON.parseObject(response.body())); return null; } } diff --git a/austin-cron/src/main/java/com/java3y/austin/utils/XxlJobUtils.java b/austin-cron/src/main/java/com/java3y/austin/utils/XxlJobUtils.java index 1e93ead..747cf73 100644 --- a/austin-cron/src/main/java/com/java3y/austin/utils/XxlJobUtils.java +++ b/austin-cron/src/main/java/com/java3y/austin/utils/XxlJobUtils.java @@ -1,43 +1,61 @@ package com.java3y.austin.utils; +import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSON; import com.java3y.austin.constant.AustinConstant; import com.java3y.austin.constants.XxlJobConstant; import com.java3y.austin.domain.MessageTemplate; +import com.java3y.austin.entity.XxlJobGroup; import com.java3y.austin.entity.XxlJobInfo; import com.java3y.austin.enums.*; +import com.java3y.austin.service.CronTaskService; +import com.java3y.austin.vo.BasicResultVO; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import java.util.Date; /** * xxlJob工具类 * * @author 3y */ +@Component public class XxlJobUtils { + @Value("${xxl.job.executor.appname}") + private String appName; + + @Value("${xxl.job.executor.jobHandlerName}") + private String jobHandlerName; + + @Autowired + private CronTaskService cronTaskService; + /** * 构建xxlJobInfo信息 * * @param messageTemplate * @return */ - public static XxlJobInfo buildXxlJobInfo(MessageTemplate messageTemplate) { - - // 判断是否为cron表达式 - String scheduleConf = StrUtil.EMPTY; - String scheduleType = ScheduleTypeEnum.NONE.name(); - if (!messageTemplate.getExpectPushTime().equals(String.valueOf(AustinConstant.FALSE))) { - scheduleType = ScheduleTypeEnum.CRON.name(); - scheduleConf = messageTemplate.getExpectPushTime(); + public XxlJobInfo buildXxlJobInfo(MessageTemplate messageTemplate) { + + String scheduleConf = messageTemplate.getExpectPushTime(); + // 如果没有指定cron表达式,说明立即执行(给到xxl-job延迟5秒的cron表达式) + if (messageTemplate.getExpectPushTime().equals(String.valueOf(AustinConstant.FALSE))) { + scheduleConf = DateUtil.format(DateUtil.offsetSecond(new Date(), XxlJobConstant.DELAY_TIME), AustinConstant.CRON_FORMAT); } - XxlJobInfo xxlJobInfo = XxlJobInfo.builder().jobGroup(1).jobDesc(messageTemplate.getName()) + XxlJobInfo xxlJobInfo = XxlJobInfo.builder() + .jobGroup(queryJobGroupId()).jobDesc(messageTemplate.getName()) .author(messageTemplate.getCreator()) .scheduleConf(scheduleConf) - .scheduleType(scheduleType) + .scheduleType(ScheduleTypeEnum.CRON.name()) .misfireStrategy(MisfireStrategyEnum.DO_NOTHING.name()) .executorRouteStrategy(ExecutorRouteStrategyEnum.CONSISTENT_HASH.name()) - .executorHandler(XxlJobConstant.HANDLER_NAME) + .executorHandler(XxlJobConstant.JOB_HANDLER_NAME) .executorParam(JSON.toJSONString(messageTemplate)) .executorBlockStrategy(ExecutorBlockStrategyEnum.SERIAL_EXECUTION.name()) .executorTimeout(XxlJobConstant.TIME_OUT) @@ -54,4 +72,20 @@ public class XxlJobUtils { } return xxlJobInfo; } + + /** + * 根据就配置文件的内容获取jobGroupId,没有则创建 + * @return + */ + private Integer queryJobGroupId() { + BasicResultVO basicResultVO = cronTaskService.getGroupId(appName, jobHandlerName); + if (basicResultVO.getData() == null) { + XxlJobGroup xxlJobGroup = XxlJobGroup.builder().appname(appName).title(jobHandlerName).addressType(AustinConstant.FALSE).build(); + if (RespStatusEnum.SUCCESS.getCode().equals(cronTaskService.createGroup(xxlJobGroup).getStatus())) { + return (int) cronTaskService.getGroupId(appName, jobHandlerName).getData(); + } + } + return (Integer) basicResultVO.getData(); + } + } diff --git a/austin-support/src/main/java/com/java3y/austin/domain/MessageTemplate.java b/austin-support/src/main/java/com/java3y/austin/domain/MessageTemplate.java index cae21f6..223530b 100644 --- a/austin-support/src/main/java/com/java3y/austin/domain/MessageTemplate.java +++ b/austin-support/src/main/java/com/java3y/austin/domain/MessageTemplate.java @@ -55,6 +55,12 @@ public class MessageTemplate implements Serializable { */ private Integer cronTaskId; + /** + * 定时发送的人群ID + * 1. 目前阶段直接填入Id测试 + * 2. 正常是需要通过ID获取文件遍历每个Id + */ + private String cronCrowdId; /** * 发送的Id类型 diff --git a/austin-web/pom.xml b/austin-web/pom.xml index 44715d5..2022d97 100644 --- a/austin-web/pom.xml +++ b/austin-web/pom.xml @@ -52,6 +52,7 @@ springfox-boot-starter + diff --git a/austin-web/src/main/java/com/java3y/austin/controller/MessageTemplateController.java b/austin-web/src/main/java/com/java3y/austin/controller/MessageTemplateController.java index 68a1d5c..8d5ef98 100644 --- a/austin-web/src/main/java/com/java3y/austin/controller/MessageTemplateController.java +++ b/austin-web/src/main/java/com/java3y/austin/controller/MessageTemplateController.java @@ -115,7 +115,6 @@ public class MessageTemplateController { Map variables = JSON.parseObject(messageTemplateParam.getMsgContent(), Map.class); MessageParam messageParam = MessageParam.builder().receiver(messageTemplateParam.getReceiver()).variables(variables).build(); SendRequest sendRequest = SendRequest.builder().code(BusinessCode.COMMON_SEND.getCode()).messageTemplateId(messageTemplateParam.getId()).messageParam(messageParam).build(); - SendResponse response = sendService.send(sendRequest); if (response.getCode() != RespStatusEnum.SUCCESS.getCode()) { return BasicResultVO.fail(response.getMsg()); @@ -130,9 +129,7 @@ public class MessageTemplateController { @PostMapping("start/{id}") @ApiOperation("/启动模板的定时任务") public BasicResultVO start(@RequestBody @PathVariable("id") Long id) { - messageTemplateService.startCronTask(id); - - return BasicResultVO.success(); + return messageTemplateService.startCronTask(id); } /** * 启动模板的定时任务 @@ -140,8 +137,6 @@ public class MessageTemplateController { @PostMapping("stop/{id}") @ApiOperation("/暂停模板的定时任务") public BasicResultVO stop(@RequestBody @PathVariable("id") Long id) { - messageTemplateService.stopCronTask(id); - - return BasicResultVO.success(); + return messageTemplateService.stopCronTask(id); } } diff --git a/austin-web/src/main/java/com/java3y/austin/service/impl/MessageTemplateServiceImpl.java b/austin-web/src/main/java/com/java3y/austin/service/impl/MessageTemplateServiceImpl.java index e8602dc..59c2ce0 100644 --- a/austin-web/src/main/java/com/java3y/austin/service/impl/MessageTemplateServiceImpl.java +++ b/austin-web/src/main/java/com/java3y/austin/service/impl/MessageTemplateServiceImpl.java @@ -3,13 +3,13 @@ package com.java3y.austin.service.impl; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; -import com.alibaba.fastjson.JSONObject; import com.java3y.austin.constant.AustinConstant; import com.java3y.austin.dao.MessageTemplateDao; import com.java3y.austin.domain.MessageTemplate; import com.java3y.austin.entity.XxlJobInfo; import com.java3y.austin.enums.AuditStatus; import com.java3y.austin.enums.MessageStatus; +import com.java3y.austin.enums.RespStatusEnum; import com.java3y.austin.enums.TemplateType; import com.java3y.austin.service.CronTaskService; import com.java3y.austin.service.MessageTemplateService; @@ -38,6 +38,9 @@ public class MessageTemplateServiceImpl implements MessageTemplateService { @Autowired private CronTaskService cronTaskService; + @Autowired + private XxlJobUtils xxlJobUtils; + @Override public List queryList(MessageTemplateParam param) { PageRequest pageRequest = PageRequest.of(param.getPage() - 1, param.getPerPage()); @@ -86,20 +89,24 @@ public class MessageTemplateServiceImpl implements MessageTemplateService { // 1.修改模板状态 MessageTemplate messageTemplate = messageTemplateDao.findById(id).get(); - // 2.动态创建定时任务并启动 - XxlJobInfo xxlJobInfo = XxlJobUtils.buildXxlJobInfo(messageTemplate); + // 2.动态创建或更新定时任务 + XxlJobInfo xxlJobInfo = xxlJobUtils.buildXxlJobInfo(messageTemplate); + // 3.获取taskId(如果本身存在则复用原有任务,如果不存在则得到新建后任务ID) + Integer taskId = messageTemplate.getCronTaskId(); BasicResultVO basicResultVO = cronTaskService.saveCronTask(xxlJobInfo); - JSONObject data = (JSONObject) basicResultVO.getData(); - if (data.get("content") != null) { - cronTaskService.startCronTask(Integer.valueOf(String.valueOf(data.get("content")))); - MessageTemplate clone = ObjectUtil.clone(messageTemplate).setMsgStatus(MessageStatus.RUN.getCode()).setUpdated(Math.toIntExact(DateUtil.currentSeconds())); + if (taskId == null && RespStatusEnum.SUCCESS.getCode().equals(basicResultVO.getStatus()) && basicResultVO.getData() != null) { + taskId = Integer.valueOf(String.valueOf(basicResultVO.getData())); + } + + // 4. 启动定时任务 + if (taskId != null) { + cronTaskService.startCronTask(taskId); + MessageTemplate clone = ObjectUtil.clone(messageTemplate).setMsgStatus(MessageStatus.RUN.getCode()).setCronTaskId(taskId).setUpdated(Math.toIntExact(DateUtil.currentSeconds())); messageTemplateDao.save(clone); return BasicResultVO.success(); - } else { - return BasicResultVO.fail(); } - + return BasicResultVO.fail(); } @Override @@ -132,7 +139,7 @@ public class MessageTemplateServiceImpl implements MessageTemplateService { /** * 1. 重置模板的状态 - * 2. 修改定时任务信息 + * 2. 修改定时任务信息(如果存在) * * @param messageTemplate */ @@ -141,7 +148,7 @@ public class MessageTemplateServiceImpl implements MessageTemplateService { .setMsgStatus(MessageStatus.INIT.getCode()).setAuditStatus(AuditStatus.WAIT_AUDIT.getCode()); if (messageTemplate.getCronTaskId() != null && TemplateType.CLOCKING.getCode().equals(messageTemplate.getTemplateType())) { - XxlJobInfo xxlJobInfo = XxlJobUtils.buildXxlJobInfo(messageTemplate); + XxlJobInfo xxlJobInfo = xxlJobUtils.buildXxlJobInfo(messageTemplate); cronTaskService.saveCronTask(xxlJobInfo); cronTaskService.stopCronTask(messageTemplate.getCronTaskId()); } diff --git a/sql/austin.sql b/sql/austin.sql index ef8c057..e03b0dd 100644 --- a/sql/austin.sql +++ b/sql/austin.sql @@ -1,7 +1,8 @@ create database austin; -use austin; +use +austin; CREATE TABLE `message_template` @@ -9,9 +10,10 @@ CREATE TABLE `message_template` `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '标题', `audit_status` tinyint(4) NOT NULL DEFAULT '0' COMMENT '当前消息审核状态: 10.待审核 20.审核成功 30.被拒绝', - `flow_id` varchar(50) COLLATE utf8mb4_unicode_ci COMMENT '工单ID', + `flow_id` varchar(50) COLLATE utf8mb4_unicode_ci COMMENT '工单ID', `msg_status` tinyint(4) NOT NULL DEFAULT '0' COMMENT '当前消息状态:10.新建 20.停用 30.启用 40.等待发送 50.发送中 60.发送成功 70.发送失败', `cron_task_id` bigint(20) COMMENT '定时任务Id (xxl-job-admin返回)', + `cron_crowd_id` varchar (50) COMMENT '定时发送人群ID', `id_type` tinyint(4) NOT NULL DEFAULT '0' COMMENT '消息的发送ID类型:10. userId 20.did 30.手机号 40.openId 50.email', `send_channel` tinyint(4) NOT NULL DEFAULT '0' COMMENT '消息发送渠道:10.IM 20.Push 30.短信 40.Email 50.公众号 60.小程序', `template_type` tinyint(4) NOT NULL DEFAULT '0' COMMENT '10.运营类 20.技术类接口调用',