一般在写定时任务的时候都是后台定义好的定时时间,但根据不同的项目需求需要客户去配置某个时间段的定时,把操作权给用户 所以就需要后端人员通过前端传值的方式来实现定时任务配置; 补充一下core表达式官网:https://cron.qqe2.com/
先看一下项目的原型图 类似于core表达式官网的效果,我查询了一下core表达式官网的配置发现他们通过JS进行拼接的方式; 相对于我们来说更倾向于后台去拼接这样的话能保证表达式正确性,前端也比较费时费力;以下就是后端的解决方案:
TaskScheduleModel.java
public class TaskScheduleModel {
Integer jobType;
Integer[] dayOfWeeks;
Integer[] dayOfMonths;
Integer second;
Integer minute;
Integer hour;
Integer beApart;
public Integer getBeApart() {
return beApart;
}
public void setBeApart(Integer beApart) {
this.beApart = beApart;
}
public Integer getJobType() {
return jobType;
}
public void setJobType(Integer jobType) {
this.jobType = jobType;
}
public Integer[] getDayOfWeeks() {
return dayOfWeeks;
}
public void setDayOfWeeks(Integer[] dayOfWeeks) {
this.dayOfWeeks = dayOfWeeks;
}
public Integer[] getDayOfMonths() {
return dayOfMonths;
}
public void setDayOfMonths(Integer[] dayOfMonths) {
this.dayOfMonths = dayOfMonths;
}
public Integer getSecond() {
return second;
}
public void setSecond(Integer second) {
this.second = second;
}
public Integer getMinute() {
return minute;
}
public void setMinute(Integer minute) {
this.minute = minute;
}
public Integer getHour() {
return hour;
}
public void setHour(Integer hour) {
this.hour = hour;
}
}
公共方法:
public static String createCronExpression(TaskScheduleModel taskScheduleModel){
StringBuffer cronExp = new StringBuffer("");
if(null == taskScheduleModel.getJobType()) {
System.out.println("执行周期未配置" );
}
if (null != taskScheduleModel.getSecond()
&& null != taskScheduleModel.getMinute()
&& null != taskScheduleModel.getHour()) {
cronExp.append(taskScheduleModel.getSecond()).append(" ");
cronExp.append(taskScheduleModel.getMinute()).append(" ");
cronExp.append(taskScheduleModel.getHour()).append(" ");
if(taskScheduleModel.getJobType().intValue() == 1){
if(taskScheduleModel.getBeApart()!=null){
cronExp.append("1");
cronExp.append("/");
cronExp.append(taskScheduleModel.getBeApart()+1);
cronExp.append(" ");
cronExp.append("* ");
cronExp.append("? ");
cronExp.append("*");
}else {
cronExp.append("* ");
cronExp.append("* ");
cronExp.append("?");
}
}
else if(taskScheduleModel.getJobType().intValue() == 3){
cronExp.append("? ");
cronExp.append("* ");
Integer[] weeks = taskScheduleModel.getDayOfWeeks();
for(int i = 0; i < weeks.length; i++){
if(i == 0){
cronExp.append(weeks[i]);
} else{
cronExp.append(",").append(weeks[i]);
}
}
}
else if(taskScheduleModel.getJobType().intValue() == 2){
Integer[] days = taskScheduleModel.getDayOfMonths();
for(int i = 0; i < days.length; i++){
if(i == 0){
if(days[i]==32){
String endMouthCron="0 0 0 L * ?";
return endMouthCron;
}else {
cronExp.append(days[i]);
}
} else{
cronExp.append(",").append(days[i]);
}
}
cronExp.append(" * ");
cronExp.append("?");
}
else if(taskScheduleModel.getJobType().intValue() == 0){
cronExp.append("* ");
cronExp.append("* ");
cronExp.append("?");
}
}
else {
System.out.println("时或分或秒参数未配置" );
}
return cronExp.toString();
}
public static String createDescription(TaskScheduleModel taskScheduleModel){
StringBuffer description = new StringBuffer("");
if (null != taskScheduleModel.getSecond()
&& null != taskScheduleModel.getMinute()
&& null != taskScheduleModel.getHour()) {
if(taskScheduleModel.getJobType().intValue() == 1){
description.append("每天");
description.append(taskScheduleModel.getHour()).append("时");
description.append(taskScheduleModel.getMinute()).append("分");
description.append(taskScheduleModel.getSecond()).append("秒");
description.append("执行");
}
else if(taskScheduleModel.getJobType().intValue() == 3){
if(taskScheduleModel.getDayOfWeeks() != null && taskScheduleModel.getDayOfWeeks().length > 0) {
String days = "";
for(int i : taskScheduleModel.getDayOfWeeks()) {
days += "周" + i;
}
description.append("每周的").append(days).append(" ");
}
if (null != taskScheduleModel.getSecond()
&& null != taskScheduleModel.getMinute()
&& null != taskScheduleModel.getHour()) {
description.append(",");
description.append(taskScheduleModel.getHour()).append("时");
description.append(taskScheduleModel.getMinute()).append("分");
description.append(taskScheduleModel.getSecond()).append("秒");
}
description.append("执行");
}
else if(taskScheduleModel.getJobType().intValue() == 2){
if(taskScheduleModel.getDayOfMonths() != null && taskScheduleModel.getDayOfMonths().length > 0) {
String days = "";
for(int i : taskScheduleModel.getDayOfMonths()) {
days += i + "号";
}
description.append("每月的").append(days).append(" ");
}
description.append(taskScheduleModel.getHour()).append("时");
description.append(taskScheduleModel.getMinute()).append("分");
description.append(taskScheduleModel.getSecond()).append("秒");
description.append("执行");
}
}
return description.toString();
}
public static String createLoopCronExpression(int rate, int cycle) {
String cron = "";
switch (rate) {
case 0:// 每cycle秒执行一次
cron = "0/" + cycle + " * * * * ?";
break;
case 1:// 每cycle分钟执行一次
cron = "0 0/" + cycle + " * * * ?";
break;
case 2:// 每cycle小时执行一次
cron = "0 0 0/" + cycle + " * * ?";
break;
case 3:// 每cycle天的0点执行一次
cron = "0 0 0 1/" + cycle + " * ?";
break;
case 4:// 每cycle月的1号0点执行一次
cron = "0 0 0 1 1/" + cycle + " ? ";
break;
case 5:// 每天cycle点执行一次
cron = "0 0 " + cycle+ " * * ?";
break;
default:// 默认每cycle秒执行一次
cron = "0/1 * * * * ?";
break;
}
return cron;
}
public static void main(String[] args) {
TaskScheduleModel taskScheduleModel = new TaskScheduleModel();
taskScheduleModel.setJobType(1);
Integer hour = 12;
Integer minute = 12;
Integer second = 12;
taskScheduleModel.setHour(hour);
taskScheduleModel.setMinute(minute);
taskScheduleModel.setSecond(second);
taskScheduleModel.setBeApart(1);
String cropExp = createCronExpression(taskScheduleModel);
System.out.println(cropExp + ":" + createDescription(taskScheduleModel));
taskScheduleModel.setJobType(3);
Integer[] dayOfWeeks = new Integer[3];
dayOfWeeks[0] = 1;
dayOfWeeks[1] = 2;
dayOfWeeks[2] = 3;
taskScheduleModel.setDayOfWeeks(dayOfWeeks);
cropExp = createCronExpression(taskScheduleModel);
System.out.println(cropExp + ":" + createDescription(taskScheduleModel));
taskScheduleModel.setJobType(2);
Integer[] dayOfMonths = new Integer[3];
dayOfMonths[0] = 1;
dayOfMonths[1] = 21;
dayOfMonths[2] = 13;
taskScheduleModel.setDayOfMonths(dayOfMonths);
cropExp = createCronExpression(taskScheduleModel);
System.out.println(cropExp + ":" + createDescription(taskScheduleModel));
taskScheduleModel.setJobType(0);
cropExp = createCronExpression(taskScheduleModel);
System.out.println(cropExp);
}
测试一下:
|