+ cron expression preview

pull/6/head
lijingwei 8 years ago
parent 723c6b07eb
commit 5ab7cfc441

@ -141,6 +141,13 @@
<version>${project.parent.version}</version> <version>${project.parent.version}</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
</dependencies> </dependencies>
</project> </project>

@ -6,18 +6,17 @@ import com.xxl.job.admin.core.util.I18nUtil;
import com.xxl.job.admin.service.XxlJobService; import com.xxl.job.admin.service.XxlJobService;
import com.xxl.job.core.biz.model.ReturnT; import com.xxl.job.core.biz.model.ReturnT;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.quartz.CronExpression;
import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.Map; import java.util.Map;
@ -100,6 +99,29 @@ public class IndexController {
return "help"; return "help";
} }
@ResponseBody
@RequestMapping("/cron-preview")
@PermessionLimit(limit=false)
public ReturnT<String[]> cronPreview(HttpServletResponse response, String expression, @RequestParam(defaultValue = "5")int times){
if (times <= 0) times = 5;
try {
CronExpression exp = new CronExpression(expression);
SimpleDateFormat df = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
Date d = new Date();
// 循环得到接下来n此的触发时间点供验证
String[] arr = new String[times];
int i = 0;
while (i < times) {
d = exp.getNextValidTimeAfter(d);
arr[i] = df.format(d);
i ++;
}
return new ReturnT<String[]>(arr);
} catch (ParseException e) {
return new ReturnT<String[]>(500, I18nUtil.getString("jobinfo_field_cron_unvalid"));
}
}
@InitBinder @InitBinder
public void initBinder(WebDataBinder binder) { public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

@ -124,7 +124,14 @@
</select> </select>
</div> </div>
<label for="lastname" class="col-sm-2 control-label">Cron<font color="red">*</font></label> <label for="lastname" class="col-sm-2 control-label">Cron<font color="red">*</font></label>
<div class="col-sm-4"><input type="text" class="form-control" name="jobCron" placeholder="${I18n.system_please_input}Cron" maxlength="128" ></div> <div class="col-sm-4">
<div class="col-sm-9" style="padding-left: 0;padding-right: 0;">
<input type="text" class="form-control" name="jobCron" placeholder="${I18n.system_please_input}Cron" maxlength="128" >
</div>
<div class="col-sm-3">
<button type="button" class="btn btn-warning cron-expression-test">test</button>
</div>
</div>
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="firstname" class="col-sm-2 control-label">${I18n.jobinfo_field_gluetype}<font color="red">*</font></label> <label for="firstname" class="col-sm-2 control-label">${I18n.jobinfo_field_gluetype}<font color="red">*</font></label>
@ -292,7 +299,14 @@ process.exit(0)
</select> </select>
</div> </div>
<label for="lastname" class="col-sm-2 control-label">Cron<font color="red">*</font></label> <label for="lastname" class="col-sm-2 control-label">Cron<font color="red">*</font></label>
<div class="col-sm-4"><input type="text" class="form-control" name="jobCron" placeholder="${I18n.system_please_input}Cron" maxlength="128" ></div> <div class="col-sm-4">
<div class="col-sm-9" style="padding-left: 0;padding-right: 0;">
<input type="text" class="form-control" name="jobCron" placeholder="${I18n.system_please_input}Cron" maxlength="128" >
</div>
<div class="col-sm-3">
<button type="button" class="btn btn-warning cron-expression-test">test</button>
</div>
</div>
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="firstname" class="col-sm-2 control-label">${I18n.jobinfo_field_gluetype}<font color="red">*</font></label> <label for="firstname" class="col-sm-2 control-label">${I18n.jobinfo_field_gluetype}<font color="red">*</font></label>

@ -459,4 +459,39 @@ $(function() {
return glueTypeTitle; return glueTypeTitle;
} }
$("body").on("click", ".cron-expression-test", function () {
var expression = $(this).closest("form").find("input[name=jobCron]").val();
console.log("expression", expression);
if (expression.length == "") {
layer.open({
title: I18n.system_tips,
btn: [ I18n.system_ok ],
content: ( I18n.jobinfo_field_cron_unvalid ),
icon: '2'
});
return;
}
$.post(base_url + "/cron-preview", {expression: expression}, function(data, status) {
if (data.code == "200") {
var content = "<ul>"
for (i=0; i < data.content.length; i++){
content += "<li>"+data.content[i]+"</li>";
}
content += "</ul>";
layer.open({
title: I18n.system_tips ,
btn: [ I18n.system_ok ],
content: content || "error",
icon: '1'
});
} else {
layer.open({
title: I18n.system_tips ,
btn: [ I18n.system_ok ],
content: (data.msg || I18n.jobinfo_field_cron_unvalid ),
icon: '2'
});
}
});
})
}); });

Loading…
Cancel
Save