1. 完成 quartz 的封装

This commit is contained in:
YunaiV
2021-02-19 20:36:07 +08:00
parent 3959aadae2
commit 014f91533c
33 changed files with 441 additions and 553 deletions

View File

@ -2,6 +2,11 @@ package cn.iocoder.dashboard.framework.quartz.core.util;
import org.quartz.CronExpression;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* Quartz Cron 表达式的工具类
*
@ -19,4 +24,31 @@ public class CronUtils {
return CronExpression.isValidExpression(cronExpression);
}
/**
* 基于 CRON 表达式,获得下 n 个满足执行的时间
*
* @param cronExpression CRON 表达式
* @param n 数量
* @return 满足条件的执行时间
*/
public static List<Date> getNextTimes(String cronExpression, int n) {
// 获得 CronExpression 对象
CronExpression cron;
try {
cron = new CronExpression(cronExpression);
} catch (ParseException e) {
throw new IllegalArgumentException(e.getMessage());
}
// 从当前开始计算n 个满足条件的
Date now = new Date();
List<Date> nextTimes = new ArrayList<>(n);
for (int i = 0; i < n; i++) {
Date nextTime = cron.getNextValidTimeAfter(now);
nextTimes.add(nextTime);
// 切换现在,为下一个触发时间;
now = nextTime;
}
return nextTimes;
}
}

View File

@ -4,6 +4,7 @@ import cn.iocoder.dashboard.common.pojo.CommonResult;
import cn.iocoder.dashboard.common.pojo.PageResult;
import cn.iocoder.dashboard.framework.excel.core.util.ExcelUtils;
import cn.iocoder.dashboard.framework.logger.operatelog.core.annotations.OperateLog;
import cn.iocoder.dashboard.framework.quartz.core.util.CronUtils;
import cn.iocoder.dashboard.modules.infra.controller.job.vo.job.*;
import cn.iocoder.dashboard.modules.infra.convert.job.InfJobConvert;
import cn.iocoder.dashboard.modules.infra.dal.dataobject.job.InfJobDO;
@ -22,6 +23,8 @@ import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import static cn.iocoder.dashboard.common.pojo.CommonResult.success;
@ -123,4 +126,20 @@ public class InfJobController {
ExcelUtils.write(response, "定时任务.xls", "数据", InfJobExcelVO.class, datas);
}
@GetMapping("/get_next_times")
@ApiOperation("获得定时任务的下 n 次执行时间")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class),
@ApiImplicitParam(name = "count", value = "数量", example = "5", dataTypeClass = Long.class)
})
@PreAuthorize("@ss.hasPermission('infra:job:query')")
public CommonResult<List<Date>> getJobNextTimes(@RequestParam("id") Long id,
@RequestParam(value = "count", required = false, defaultValue = "5") Integer count) {
InfJobDO job = jobService.getJob(id);
if (job == null) {
return success(Collections.emptyList());
}
return success(CronUtils.getNextTimes(job.getCronExpression(), count));
}
}

View File

@ -0,0 +1,81 @@
package cn.iocoder.dashboard.modules.infra.controller.job;
import cn.iocoder.dashboard.common.pojo.CommonResult;
import cn.iocoder.dashboard.common.pojo.PageResult;
import cn.iocoder.dashboard.framework.excel.core.util.ExcelUtils;
import cn.iocoder.dashboard.framework.logger.operatelog.core.annotations.OperateLog;
import cn.iocoder.dashboard.modules.infra.controller.job.vo.log.InfJobLogExcelVO;
import cn.iocoder.dashboard.modules.infra.controller.job.vo.log.InfJobLogExportReqVO;
import cn.iocoder.dashboard.modules.infra.controller.job.vo.log.InfJobLogPageReqVO;
import cn.iocoder.dashboard.modules.infra.controller.job.vo.log.InfJobLogRespVO;
import cn.iocoder.dashboard.modules.infra.convert.job.InfJobLogConvert;
import cn.iocoder.dashboard.modules.infra.dal.dataobject.job.InfJobLogDO;
import cn.iocoder.dashboard.modules.infra.service.job.InfJobLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import static cn.iocoder.dashboard.common.pojo.CommonResult.success;
import static cn.iocoder.dashboard.framework.logger.operatelog.core.enums.OperateTypeEnum.EXPORT;
@Api(tags = "定时任务日志")
@RestController
@RequestMapping("/infra/job-log")
@Validated
public class InfJobLogController {
@Resource
private InfJobLogService jobLogService;
@GetMapping("/get")
@ApiOperation("获得定时任务日志")
@ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
@PreAuthorize("@ss.hasPermission('infra:job:query')")
public CommonResult<InfJobLogRespVO> getJobLog(@RequestParam("id") Long id) {
InfJobLogDO jobLog = jobLogService.getJobLog(id);
return success(InfJobLogConvert.INSTANCE.convert(jobLog));
}
@GetMapping("/list")
@ApiOperation("获得定时任务日志列表")
@ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
@PreAuthorize("@ss.hasPermission('infra:job:query')")
public CommonResult<List<InfJobLogRespVO>> getJobLogList(@RequestParam("ids") Collection<Long> ids) {
List<InfJobLogDO> list = jobLogService.getJobLogList(ids);
return success(InfJobLogConvert.INSTANCE.convertList(list));
}
@GetMapping("/page")
@ApiOperation("获得定时任务日志分页")
@PreAuthorize("@ss.hasPermission('infra:job:query')")
public CommonResult<PageResult<InfJobLogRespVO>> getJobLogPage(@Valid InfJobLogPageReqVO pageVO) {
PageResult<InfJobLogDO> pageResult = jobLogService.getJobLogPage(pageVO);
return success(InfJobLogConvert.INSTANCE.convertPage(pageResult));
}
@GetMapping("/export-excel")
@ApiOperation("导出定时任务日志 Excel")
@PreAuthorize("@ss.hasPermission('infra:job:export')")
@OperateLog(type = EXPORT)
public void exportJobLogExcel(@Valid InfJobLogExportReqVO exportReqVO,
HttpServletResponse response) throws IOException {
List<InfJobLogDO> list = jobLogService.getJobLogList(exportReqVO);
// 导出 Excel
List<InfJobLogExcelVO> datas = InfJobLogConvert.INSTANCE.convertList02(list);
ExcelUtils.write(response, "定时任务.xls", "数据", InfJobLogExcelVO.class, datas);
}
}

View File

@ -23,14 +23,14 @@ public class InfJobBaseVO {
@NotNull(message = "CRON 表达式不能为空")
private String cronExpression;
@ApiModelProperty(value = "重试次数", required = true)
@ApiModelProperty(value = "重试次数", required = true, example = "3")
@NotNull(message = "重试次数不能为空")
private Integer retryCount;
@ApiModelProperty(value = "重试间隔", required = true)
@ApiModelProperty(value = "重试间隔", required = true, example = "1000")
@NotNull(message = "重试间隔不能为空")
private Integer retryInterval;
@ApiModelProperty(value = "监控超时时间", example = "1000")
private Integer monitorTimeout;

View File

@ -25,18 +25,6 @@ public class InfJobRespVO extends InfJobBaseVO {
@NotNull(message = "处理器的名字不能为空")
private String handlerName;
@ApiModelProperty(value = "最后一次执行的开始时间")
private Date executeBeginTime;
@ApiModelProperty(value = "最后一次执行的结束时间")
private Date executeEndTime;
@ApiModelProperty(value = "上一次触发时间")
private Date firePrevTime;
@ApiModelProperty(value = "下一次触发时间")
private Date fireNextTime;
@ApiModelProperty(value = "创建时间", required = true)
private Date createTime;

View File

@ -16,18 +16,18 @@ import static cn.iocoder.dashboard.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOU
@Data
public class InfJobLogBaseVO {
@ApiModelProperty(value = "任务编号", required = true)
@ApiModelProperty(value = "任务编号", required = true, example = "1024")
@NotNull(message = "任务编号不能为空")
private Long jobId;
@ApiModelProperty(value = "处理器的名字", required = true)
@ApiModelProperty(value = "处理器的名字", required = true, example = "sysUserSessionTimeoutJob")
@NotNull(message = "处理器的名字不能为空")
private String handlerName;
@ApiModelProperty(value = "处理器的参数")
@ApiModelProperty(value = "处理器的参数", example = "yudao")
private String handlerParam;
@ApiModelProperty(value = "第几次执行", required = true)
@ApiModelProperty(value = "第几次执行", required = true, example = "1")
@NotNull(message = "第几次执行不能为空")
private Integer executeIndex;
@ -40,14 +40,14 @@ public class InfJobLogBaseVO {
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
private Date endTime;
@ApiModelProperty(value = "执行时长")
@ApiModelProperty(value = "执行时长", example = "123")
private Integer duration;
@ApiModelProperty(value = "任务状态", required = true)
@ApiModelProperty(value = "任务状态", required = true, example = "1", notes = "参见 InfJobLogStatusEnum 枚举")
@NotNull(message = "任务状态不能为空")
private Integer status;
@ApiModelProperty(value = "结果数据")
@ApiModelProperty(value = "结果数据", example = "执行成功")
private String result;
}

View File

@ -1,5 +1,8 @@
package cn.iocoder.dashboard.modules.infra.controller.job.vo.log;
import cn.iocoder.dashboard.framework.excel.core.annotations.DictFormat;
import cn.iocoder.dashboard.framework.excel.core.convert.DictConvert;
import cn.iocoder.dashboard.modules.system.enums.dict.SysDictTypeEnum;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
@ -37,7 +40,8 @@ public class InfJobLogExcelVO {
@ExcelProperty("执行时长")
private Integer duration;
@ExcelProperty("任务状态")
@ExcelProperty(value = "任务状态", converter = DictConvert.class)
@DictFormat(SysDictTypeEnum.INF_JOB_STATUS)
private Integer status;
@ExcelProperty("结果数据")

View File

@ -5,6 +5,7 @@ import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import javax.validation.constraints.NotNull;
import java.util.Date;
import static cn.iocoder.dashboard.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
@ -13,7 +14,10 @@ import static cn.iocoder.dashboard.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOU
@Data
public class InfJobLogExportReqVO {
@ApiModelProperty(value = "处理器的名字")
@ApiModelProperty(value = "任务编号", example = "10")
private Long jobId;
@ApiModelProperty(value = "处理器的名字", notes = "模糊匹配")
private String handlerName;
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
@ -24,7 +28,7 @@ public class InfJobLogExportReqVO {
@ApiModelProperty(value = "结束执行时间")
private Date endTime;
@ApiModelProperty(value = "任务状态")
@ApiModelProperty(value = "任务状态", notes = "参见 InfJobLogStatusEnum 枚举")
private Integer status;
}

View File

@ -18,7 +18,10 @@ import static cn.iocoder.dashboard.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOU
@ToString(callSuper = true)
public class InfJobLogPageReqVO extends PageParam {
@ApiModelProperty(value = "处理器的名字")
@ApiModelProperty(value = "任务编号", example = "10")
private Long jobId;
@ApiModelProperty(value = "处理器的名字", notes = "模糊匹配")
private String handlerName;
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
@ -29,7 +32,7 @@ public class InfJobLogPageReqVO extends PageParam {
@ApiModelProperty(value = "结束执行时间")
private Date endTime;
@ApiModelProperty(value = "任务状态")
@ApiModelProperty(value = "任务状态", notes = "参见 InfJobLogStatusEnum 枚举")
private Integer status;
}

View File

@ -14,7 +14,7 @@ import java.util.Date;
@ToString(callSuper = true)
public class InfJobLogRespVO extends InfJobLogBaseVO {
@ApiModelProperty(value = "日志编号", required = true)
@ApiModelProperty(value = "日志编号", required = true, example = "1024")
private Long id;
@ApiModelProperty(value = "创建时间", required = true)

View File

@ -0,0 +1,30 @@
package cn.iocoder.dashboard.modules.infra.convert.job;
import cn.iocoder.dashboard.common.pojo.PageResult;
import cn.iocoder.dashboard.modules.infra.controller.job.vo.log.InfJobLogExcelVO;
import cn.iocoder.dashboard.modules.infra.controller.job.vo.log.InfJobLogRespVO;
import cn.iocoder.dashboard.modules.infra.dal.dataobject.job.InfJobLogDO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import java.util.List;
/**
* 定时任务日志 Convert
*
* @author 芋艿
*/
@Mapper
public interface InfJobLogConvert {
InfJobLogConvert INSTANCE = Mappers.getMapper(InfJobLogConvert.class);
InfJobLogRespVO convert(InfJobLogDO bean);
List<InfJobLogRespVO> convertList(List<InfJobLogDO> list);
PageResult<InfJobLogRespVO> convertPage(PageResult<InfJobLogDO> page);
List<InfJobLogExcelVO> convertList02(List<InfJobLogDO> list);
}

View File

@ -6,8 +6,6 @@ import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.*;
import java.util.Date;
/**
* 定时任务 DO
*
@ -45,35 +43,10 @@ public class InfJobDO extends BaseDO {
* 处理器的参数
*/
private String handlerParam;
// ========== 时间相关字段 ==========
/**
* CRON 表达式
*/
private String cronExpression;
/**
* 最后一次执行的开始时间
*
* 该字段在任务执行结束后,进行记录
*/
private Date executeBeginTime;
/**
* 最后一次执行的结束时间
*
* 该字段在任务执行结束后,进行记录
*/
private Date executeEndTime;
/**
* 上一次触发时间,来自 Quartz SCHE_TRIGGERS 表
*/
private Date firePrevTime;
/**
* 下一次触发时间,来自 Quartz SCHE_TRIGGERS 表
*
* 在触发器状态从 `ACQUIRED` 变成 `BLOCKED` 时,就会更新 PREV_FIRE_TIME、NEXT_FIRE_TIME然后定时任务才正式开始执行
*/
private Date fireNextTime;
// ========== 重试相关字段 ==========
/**
@ -96,8 +69,4 @@ public class InfJobDO extends BaseDO {
*/
private Integer monitorTimeout;
// TODO misfirePolicy
// TODO concurrent
// TODO 失败重试
}

View File

@ -1,9 +1,15 @@
package cn.iocoder.dashboard.modules.infra.dal.mysql.job;
import cn.iocoder.dashboard.common.pojo.PageResult;
import cn.iocoder.dashboard.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.dashboard.framework.mybatis.core.query.QueryWrapperX;
import cn.iocoder.dashboard.modules.infra.controller.job.vo.log.InfJobLogExportReqVO;
import cn.iocoder.dashboard.modules.infra.controller.job.vo.log.InfJobLogPageReqVO;
import cn.iocoder.dashboard.modules.infra.dal.dataobject.job.InfJobLogDO;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* 任务日志 Mapper
*
@ -11,4 +17,27 @@ import org.apache.ibatis.annotations.Mapper;
*/
@Mapper
public interface InfJobLogMapper extends BaseMapperX<InfJobLogDO> {
default PageResult<InfJobLogDO> selectPage(InfJobLogPageReqVO reqVO) {
return selectPage(reqVO, new QueryWrapperX<InfJobLogDO>()
.eqIfPresent("job_id", reqVO.getJobId())
.likeIfPresent("handler_name", reqVO.getHandlerName())
.geIfPresent("begin_time", reqVO.getBeginTime())
.leIfPresent("end_time", reqVO.getEndTime())
.eqIfPresent("status", reqVO.getStatus())
.orderByDesc("id") // ID 倒序
);
}
default List<InfJobLogDO> selectList(InfJobLogExportReqVO reqVO) {
return selectList(new QueryWrapperX<InfJobLogDO>()
.eqIfPresent("job_id", reqVO.getJobId())
.likeIfPresent("handler_name", reqVO.getHandlerName())
.geIfPresent("begin_time", reqVO.getBeginTime())
.leIfPresent("end_time", reqVO.getEndTime())
.eqIfPresent("status", reqVO.getStatus())
.orderByDesc("id") // ID 倒序
);
}
}

View File

@ -1,6 +1,13 @@
package cn.iocoder.dashboard.modules.infra.service.job;
import cn.iocoder.dashboard.common.pojo.PageResult;
import cn.iocoder.dashboard.framework.quartz.core.service.JobLogFrameworkService;
import cn.iocoder.dashboard.modules.infra.controller.job.vo.log.InfJobLogExportReqVO;
import cn.iocoder.dashboard.modules.infra.controller.job.vo.log.InfJobLogPageReqVO;
import cn.iocoder.dashboard.modules.infra.dal.dataobject.job.InfJobLogDO;
import java.util.Collection;
import java.util.List;
/**
* Job 日志 Service 接口
@ -8,4 +15,37 @@ import cn.iocoder.dashboard.framework.quartz.core.service.JobLogFrameworkService
* @author 芋道源码
*/
public interface InfJobLogService extends JobLogFrameworkService {
/**
* 获得定时任务
*
* @param id 编号
* @return 定时任务
*/
InfJobLogDO getJobLog(Long id);
/**
* 获得定时任务列表
*
* @param ids 编号
* @return 定时任务列表
*/
List<InfJobLogDO> getJobLogList(Collection<Long> ids);
/**
* 获得定时任务分页
*
* @param pageReqVO 分页查询
* @return 定时任务分页
*/
PageResult<InfJobLogDO> getJobLogPage(InfJobLogPageReqVO pageReqVO);
/**
* 获得定时任务列表, 用于 Excel 导出
*
* @param exportReqVO 查询条件
* @return 定时任务分页
*/
List<InfJobLogDO> getJobLogList(InfJobLogExportReqVO exportReqVO);
}

View File

@ -1,5 +1,8 @@
package cn.iocoder.dashboard.modules.infra.service.job.impl;
import cn.iocoder.dashboard.common.pojo.PageResult;
import cn.iocoder.dashboard.modules.infra.controller.job.vo.log.InfJobLogExportReqVO;
import cn.iocoder.dashboard.modules.infra.controller.job.vo.log.InfJobLogPageReqVO;
import cn.iocoder.dashboard.modules.infra.dal.dataobject.job.InfJobLogDO;
import cn.iocoder.dashboard.modules.infra.dal.mysql.job.InfJobLogMapper;
import cn.iocoder.dashboard.modules.infra.enums.job.InfJobLogStatusEnum;
@ -10,7 +13,9 @@ import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
import java.util.Collection;
import java.util.Date;
import java.util.List;
/**
* Job 日志 Service 实现类
@ -46,4 +51,24 @@ public class InfJobLogServiceImpl implements InfJobLogService {
}
}
@Override
public InfJobLogDO getJobLog(Long id) {
return jobLogMapper.selectById(id);
}
@Override
public List<InfJobLogDO> getJobLogList(Collection<Long> ids) {
return jobLogMapper.selectBatchIds(ids);
}
@Override
public PageResult<InfJobLogDO> getJobLogPage(InfJobLogPageReqVO pageReqVO) {
return jobLogMapper.selectPage(pageReqVO);
}
@Override
public List<InfJobLogDO> getJobLogList(InfJobLogExportReqVO exportReqVO) {
return jobLogMapper.selectList(exportReqVO);
}
}

View File

@ -15,8 +15,9 @@ import java.util.List;
@Mapper
public interface SysDictDataMapper extends BaseMapperX<SysDictDataDO> {
default SysDictDataDO selectByLabel(String label) {
return selectOne(new QueryWrapper<SysDictDataDO>().eq("label", label));
default SysDictDataDO selectByDictTypeAndLabel(String dictType, String label) {
return selectOne(new QueryWrapper<SysDictDataDO>().eq("dict_type", dictType)
.eq("label", label));
}
default int selectCountByDictType(String dictType) {

View File

@ -18,8 +18,9 @@ public enum SysDictTypeEnum {
SYS_BOOLEAN_STRING("sys_boolean_string"), // Boolean 是否类型
INF_REDIS_TIMEOUT_TYPE("inf_redis_timeout_type"), // Redis 超时类型
INF_JOB_STATUS("inf_job_status") // 任务状态的枚举
;
INF_JOB_STATUS("inf_job_status"), // 定时任务状态的枚举
INF_JOB_LOG_STATUS("inf_job_log_status") // 定时任务日志状态的枚举
,;
/**

View File

@ -15,9 +15,6 @@ public class SysUserSessionTimeoutJob implements JobHandler {
@Override
public String execute(String param) throws Exception {
if (true) {
throw new RuntimeException("测试异常");
}
// System.out.println("执行了一次任务");
log.info("[execute][执行任务:{}]", param);
return null;

View File

@ -195,13 +195,13 @@ public class SysDictDataServiceImpl implements SysDictDataService {
// 校验自己存在
checkDictDataExists(id);
// 校验字典数据的值的唯一性
checkDictDataValueUnique(id, label);
checkDictDataValueUnique(id, dictType, label);
// 校验字典类型有效
checkDictTypeValid(dictType);
}
private void checkDictDataValueUnique(Long id, String label) {
SysDictDataDO dictData = dictDataMapper.selectByLabel(label);
private void checkDictDataValueUnique(Long id, String dictType, String label) {
SysDictDataDO dictData = dictDataMapper.selectByDictTypeAndLabel(dictType, label);
if (dictData == null) {
return;
}

View File

@ -51,7 +51,7 @@ public class ToolTestDemoController {
@DeleteMapping("/delete")
@ApiOperation("删除测试示例")
@ApiImplicitParam(name = "id", value = "编号", required = true)
@ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
@PreAuthorize("@ss.hasPermission('tool:test-demo:delete')")
public CommonResult<Boolean> deleteTestDemo(@RequestParam("id") Long id) {
testDemoService.deleteTestDemo(id);