Files
RuoYi/ruoyi-quartz/src/main/java/com/ruoyi/quartz/controller/SysJobController.java

221 lines
7.2 KiB
Java
Raw Normal View History

2019-03-08 14:44:38 +08:00
package com.ruoyi.quartz.controller;
2018-07-09 08:44:52 +08:00
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
2019-03-13 19:45:12 +08:00
import org.quartz.SchedulerException;
2018-07-09 08:44:52 +08:00
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
2018-07-22 23:05:50 +08:00
import org.springframework.ui.ModelMap;
2019-07-19 09:53:45 +08:00
import org.springframework.validation.annotation.Validated;
2018-07-09 08:44:52 +08:00
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;
2018-07-09 08:44:52 +08:00
import org.springframework.web.bind.annotation.ResponseBody;
2018-10-07 14:16:47 +08:00
import com.ruoyi.common.annotation.Log;
2021-06-15 09:22:55 +08:00
import com.ruoyi.common.constant.Constants;
2019-03-08 14:44:38 +08:00
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
2019-03-08 16:11:24 +08:00
import com.ruoyi.common.core.page.TableDataInfo;
2018-10-07 14:16:47 +08:00
import com.ruoyi.common.enums.BusinessType;
2019-03-13 19:45:12 +08:00
import com.ruoyi.common.exception.job.TaskException;
2021-06-15 09:22:55 +08:00
import com.ruoyi.common.utils.StringUtils;
2019-01-11 09:50:16 +08:00
import com.ruoyi.common.utils.poi.ExcelUtil;
2018-10-07 14:16:47 +08:00
import com.ruoyi.quartz.domain.SysJob;
import com.ruoyi.quartz.service.ISysJobService;
2020-07-16 17:09:15 +08:00
import com.ruoyi.quartz.util.CronUtils;
2018-07-09 08:44:52 +08:00
/**
* 调度任务信息操作处理
*
* @author ruoyi
*/
@Controller
@RequestMapping("/monitor/job")
2018-10-10 21:56:19 +08:00
public class SysJobController extends BaseController
2018-07-09 08:44:52 +08:00
{
private String prefix = "monitor/job";
@Autowired
2018-10-07 14:16:47 +08:00
private ISysJobService jobService;
2018-07-09 08:44:52 +08:00
@RequiresPermissions("monitor:job:view")
@GetMapping()
public String job()
{
return prefix + "/job";
}
@RequiresPermissions("monitor:job:list")
@PostMapping("/list")
@ResponseBody
2018-10-07 14:16:47 +08:00
public TableDataInfo list(SysJob job)
2018-07-09 08:44:52 +08:00
{
startPage();
2018-10-07 14:16:47 +08:00
List<SysJob> list = jobService.selectJobList(job);
2018-07-09 08:44:52 +08:00
return getDataTable(list);
}
2018-08-18 23:51:19 +08:00
@Log(title = "定时任务", businessType = BusinessType.EXPORT)
2018-08-01 11:42:13 +08:00
@RequiresPermissions("monitor:job:export")
2018-07-09 08:44:52 +08:00
@PostMapping("/export")
@ResponseBody
2018-10-07 14:16:47 +08:00
public AjaxResult export(SysJob job)
2018-07-09 08:44:52 +08:00
{
2018-10-07 14:16:47 +08:00
List<SysJob> list = jobService.selectJobList(job);
ExcelUtil<SysJob> util = new ExcelUtil<SysJob>(SysJob.class);
2019-01-10 19:06:15 +08:00
return util.exportExcel(list, "定时任务");
2018-07-09 08:44:52 +08:00
}
2018-08-18 23:51:19 +08:00
@Log(title = "定时任务", businessType = BusinessType.DELETE)
2018-07-09 08:44:52 +08:00
@RequiresPermissions("monitor:job:remove")
@PostMapping("/remove")
@ResponseBody
2019-03-13 19:45:12 +08:00
public AjaxResult remove(String ids) throws SchedulerException
2018-07-09 08:44:52 +08:00
{
2019-03-13 19:45:12 +08:00
jobService.deleteJobByIds(ids);
return success();
2018-07-09 08:44:52 +08:00
}
2018-12-01 16:14:19 +08:00
@RequiresPermissions("monitor:job:detail")
@GetMapping("/detail/{jobId}")
public String detail(@PathVariable("jobId") Long jobId, ModelMap mmap)
{
mmap.put("name", "job");
mmap.put("job", jobService.selectJobById(jobId));
return prefix + "/detail";
}
2018-07-09 08:44:52 +08:00
/**
* 任务调度状态修改
*/
2018-08-18 23:51:19 +08:00
@Log(title = "定时任务", businessType = BusinessType.UPDATE)
2018-07-09 08:44:52 +08:00
@RequiresPermissions("monitor:job:changeStatus")
@PostMapping("/changeStatus")
@ResponseBody
2019-03-13 19:45:12 +08:00
public AjaxResult changeStatus(SysJob job) throws SchedulerException
2018-07-09 08:44:52 +08:00
{
SysJob newJob = jobService.selectJobById(job.getJobId());
newJob.setStatus(job.getStatus());
return toAjax(jobService.changeStatus(newJob));
2018-07-09 08:44:52 +08:00
}
2018-07-22 23:05:50 +08:00
2018-07-09 08:44:52 +08:00
/**
* 任务调度立即执行一次
*/
2018-08-18 23:51:19 +08:00
@Log(title = "定时任务", businessType = BusinessType.UPDATE)
2018-07-09 08:44:52 +08:00
@RequiresPermissions("monitor:job:changeStatus")
@PostMapping("/run")
@ResponseBody
2019-03-13 19:45:12 +08:00
public AjaxResult run(SysJob job) throws SchedulerException
2018-07-09 08:44:52 +08:00
{
2019-03-13 19:45:12 +08:00
jobService.run(job);
return success();
2018-07-09 08:44:52 +08:00
}
/**
* 新增调度
*/
@GetMapping("/add")
2018-07-22 23:05:50 +08:00
public String add()
2018-07-09 08:44:52 +08:00
{
return prefix + "/add";
}
2018-07-22 23:05:50 +08:00
/**
* 新增保存调度
*/
2018-08-18 23:51:19 +08:00
@Log(title = "定时任务", businessType = BusinessType.INSERT)
2018-07-22 23:05:50 +08:00
@RequiresPermissions("monitor:job:add")
@PostMapping("/add")
@ResponseBody
2019-07-19 09:53:45 +08:00
public AjaxResult addSave(@Validated SysJob job) throws SchedulerException, TaskException
2018-07-22 23:05:50 +08:00
{
2020-07-16 17:09:15 +08:00
if (!CronUtils.isValid(job.getCronExpression()))
{
return error("新增任务'" + job.getJobName() + "'失败Cron表达式不正确");
2021-06-15 09:22:55 +08:00
}
else if (StringUtils.containsIgnoreCase(job.getInvokeTarget(), Constants.LOOKUP_RMI))
{
2021-07-30 10:20:01 +08:00
return error("新增任务'" + job.getJobName() + "'失败,目标字符串不允许'rmi://'调用");
}
else if (StringUtils.containsAnyIgnoreCase(job.getInvokeTarget(), new String[] { Constants.HTTP, Constants.HTTPS }))
{
return error("新增任务'" + job.getJobName() + "'失败,目标字符串不允许'http(s)//'调用");
2020-07-16 17:09:15 +08:00
}
return toAjax(jobService.insertJob(job));
2018-07-22 23:05:50 +08:00
}
2018-07-09 08:44:52 +08:00
/**
* 修改调度
*/
@GetMapping("/edit/{jobId}")
2018-07-22 23:05:50 +08:00
public String edit(@PathVariable("jobId") Long jobId, ModelMap mmap)
2018-07-09 08:44:52 +08:00
{
2018-07-22 23:05:50 +08:00
mmap.put("job", jobService.selectJobById(jobId));
2018-07-09 08:44:52 +08:00
return prefix + "/edit";
}
/**
2018-07-22 23:05:50 +08:00
* 修改保存调度
2018-07-09 08:44:52 +08:00
*/
2018-08-18 23:51:19 +08:00
@Log(title = "定时任务", businessType = BusinessType.UPDATE)
2018-07-22 23:05:50 +08:00
@RequiresPermissions("monitor:job:edit")
@PostMapping("/edit")
2018-07-09 08:44:52 +08:00
@ResponseBody
2019-07-19 09:53:45 +08:00
public AjaxResult editSave(@Validated SysJob job) throws SchedulerException, TaskException
2018-07-09 08:44:52 +08:00
{
2020-07-16 17:09:15 +08:00
if (!CronUtils.isValid(job.getCronExpression()))
{
return error("修改任务'" + job.getJobName() + "'失败Cron表达式不正确");
2021-06-15 09:22:55 +08:00
}
else if (StringUtils.containsIgnoreCase(job.getInvokeTarget(), Constants.LOOKUP_RMI))
{
2021-07-30 10:20:01 +08:00
return error("修改任务'" + job.getJobName() + "'失败,目标字符串不允许'rmi://'调用");
}
else if (StringUtils.containsAnyIgnoreCase(job.getInvokeTarget(), new String[] { Constants.HTTP, Constants.HTTPS }))
{
return error("修改任务'" + job.getJobName() + "'失败,目标字符串不允许'http(s)//'调用");
2020-07-16 17:09:15 +08:00
}
return toAjax(jobService.updateJob(job));
2018-07-09 08:44:52 +08:00
}
2019-03-08 14:44:38 +08:00
2018-10-27 21:39:01 +08:00
/**
* 校验cron表达式是否有效
*/
@PostMapping("/checkCronExpressionIsValid")
@ResponseBody
public boolean checkCronExpressionIsValid(SysJob job)
{
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("表达式无效");
}
}
2018-07-09 08:44:52 +08:00
}