定时任务支持在线生成cron表达式
This commit is contained in:
@@ -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("表达式无效");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user