定时任务支持在线生成cron表达式

This commit is contained in:
RuoYi
2021-08-12 16:29:50 +08:00
parent e39f8f9127
commit 4ec04eda71
8 changed files with 2330 additions and 0 deletions

View File

@ -11,6 +11,7 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.Constants;
@ -189,4 +190,31 @@ public class SysJobController extends BaseController
{
return jobService.checkCronExpressionIsValid(job.getCronExpression());
}
/**
* Cron表达式在线生成
*/
@GetMapping("/cron")
public String cron()
{
return prefix + "/cron";
}
/**
* 查询cron表达式近5次的执行时间
*/
@GetMapping("/queryCronExpression")
@ResponseBody
public AjaxResult queryCronExpression(@RequestParam(value = "cronExpression", required = false) String cronExpression)
{
if (jobService.checkCronExpressionIsValid(cronExpression))
{
List<String> dateList = CronUtils.getRecentTriggerTime(cronExpression);
return success(dateList);
}
else
{
return error("表达式无效");
}
}
}

View File

@ -1,8 +1,13 @@
package com.ruoyi.quartz.util;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.quartz.CronExpression;
import org.quartz.TriggerUtils;
import org.quartz.impl.triggers.CronTriggerImpl;
import com.ruoyi.common.utils.DateUtils;
/**
* cron表达式工具类
@ -60,4 +65,30 @@ public class CronUtils
throw new IllegalArgumentException(e.getMessage());
}
}
/**
* 通过表达式获取近10次的执行时间
*
* @param cron 表达式
* @return 时间列表
*/
public static List<String> getRecentTriggerTime(String cron)
{
List<String> list = new ArrayList<String>();
try
{
CronTriggerImpl cronTriggerImpl = new CronTriggerImpl();
cronTriggerImpl.setCronExpression(cron);
List<Date> dates = TriggerUtils.computeFireTimes(cronTriggerImpl, null, 10);
for (Date date : dates)
{
list.add(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, date));
}
}
catch (ParseException e)
{
return null;
}
return list;
}
}

File diff suppressed because it is too large Load Diff

View File

@ -47,6 +47,9 @@
<a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="monitor:job:export">
<i class="fa fa-download"></i> 导出
</a>
<a class="btn btn-primary" onclick="javascript:cron()">
<i class="fa fa-code"></i> 生成表达式
</a>
<a class="btn btn-info" onclick="javascript:jobLog()" shiro:hasPermission="monitor:job:detail">
<i class="fa fa-list"></i> 日志
</a>
@ -176,6 +179,19 @@
}
$.modal.openTab("调度日志", url);
}
/* cron表达式生成 */
function cron() {
var url = prefix + '/cron';
var height = $(window).height() - 50;
top.layer.open({
title: "Cron表达式生成器",
type: 2,
area: ['800px', height + "px" ], //宽高
shadeClose: true,
content: url
});
}
</script>
</body>
</html>