SysJobController.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package com.ruoyi.quartz.controller;
  2. import com.ruoyi.common.annotation.Log;
  3. import com.ruoyi.common.constant.Constants;
  4. import com.ruoyi.common.core.controller.BaseController;
  5. import com.ruoyi.common.core.domain.AjaxResult;
  6. import com.ruoyi.common.core.page.TableDataInfo;
  7. import com.ruoyi.common.enums.BusinessType;
  8. import com.ruoyi.common.exception.job.TaskException;
  9. import com.ruoyi.common.utils.StringUtils;
  10. import com.ruoyi.common.utils.poi.ExcelUtil;
  11. import com.ruoyi.quartz.domain.SysJob;
  12. import com.ruoyi.quartz.service.ISysJobService;
  13. import com.ruoyi.quartz.util.CronUtils;
  14. import org.quartz.SchedulerException;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.web.bind.annotation.DeleteMapping;
  17. import org.springframework.web.bind.annotation.GetMapping;
  18. import org.springframework.web.bind.annotation.PathVariable;
  19. import org.springframework.web.bind.annotation.PostMapping;
  20. import org.springframework.web.bind.annotation.PutMapping;
  21. import org.springframework.web.bind.annotation.RequestBody;
  22. import org.springframework.web.bind.annotation.RequestMapping;
  23. import org.springframework.web.bind.annotation.RestController;
  24. import javax.servlet.http.HttpServletResponse;
  25. import java.util.List;
  26. /**
  27. * 调度任务信息操作处理
  28. *
  29. * @author ruoyi
  30. */
  31. @RestController
  32. @RequestMapping("/monitor/job")
  33. public class SysJobController extends BaseController {
  34. @Autowired
  35. private ISysJobService jobService;
  36. /**
  37. * 查询定时任务列表
  38. */
  39. @GetMapping("/list")
  40. public TableDataInfo list(SysJob sysJob) {
  41. return jobService.selectPageJobList(sysJob);
  42. }
  43. /**
  44. * 导出定时任务列表
  45. */
  46. @Log(title = "定时任务", businessType = BusinessType.EXPORT)
  47. @GetMapping("/export")
  48. public void export(SysJob sysJob, HttpServletResponse response) {
  49. List<SysJob> list = jobService.selectJobList(sysJob);
  50. ExcelUtil.exportExcel(list, "定时任务", SysJob.class, response);
  51. }
  52. /**
  53. * 获取定时任务详细信息
  54. */
  55. @GetMapping(value = "/{jobId}")
  56. public AjaxResult getInfo(@PathVariable("jobId") Long jobId) {
  57. return AjaxResult.success(jobService.selectJobById(jobId));
  58. }
  59. /**
  60. * 新增定时任务
  61. */
  62. @Log(title = "定时任务", businessType = BusinessType.INSERT)
  63. @PostMapping
  64. public AjaxResult add(@RequestBody SysJob job) throws SchedulerException, TaskException {
  65. if (!CronUtils.isValid(job.getCronExpression())) {
  66. return error("新增任务'" + job.getJobName() + "'失败,Cron表达式不正确");
  67. } else if (StringUtils.containsIgnoreCase(job.getInvokeTarget(), Constants.LOOKUP_RMI)) {
  68. return error("新增任务'" + job.getJobName() + "'失败,目标字符串不允许'rmi://'调用");
  69. } else if (StringUtils.containsIgnoreCase(job.getInvokeTarget(), Constants.LOOKUP_LDAP)) {
  70. return error("新增任务'" + job.getJobName() + "'失败,目标字符串不允许'ldap://'调用");
  71. } else if (StringUtils.containsAnyIgnoreCase(job.getInvokeTarget(), new String[]{Constants.HTTP, Constants.HTTPS})) {
  72. return error("新增任务'" + job.getJobName() + "'失败,目标字符串不允许'http(s)//'调用");
  73. }
  74. job.setCreateBy(getUsername());
  75. return toAjax(jobService.insertJob(job));
  76. }
  77. /**
  78. * 修改定时任务
  79. */
  80. @Log(title = "定时任务", businessType = BusinessType.UPDATE)
  81. @PutMapping
  82. public AjaxResult edit(@RequestBody SysJob job) throws SchedulerException, TaskException {
  83. if (!CronUtils.isValid(job.getCronExpression())) {
  84. return error("修改任务'" + job.getJobName() + "'失败,Cron表达式不正确");
  85. } else if (StringUtils.containsIgnoreCase(job.getInvokeTarget(), Constants.LOOKUP_RMI)) {
  86. return error("修改任务'" + job.getJobName() + "'失败,目标字符串不允许'rmi://'调用");
  87. } else if (StringUtils.containsIgnoreCase(job.getInvokeTarget(), Constants.LOOKUP_LDAP)) {
  88. return error("修改任务'" + job.getJobName() + "'失败,目标字符串不允许'ldap://'调用");
  89. } else if (StringUtils.containsAnyIgnoreCase(job.getInvokeTarget(), new String[]{Constants.HTTP, Constants.HTTPS})) {
  90. return error("修改任务'" + job.getJobName() + "'失败,目标字符串不允许'http(s)//'调用");
  91. }
  92. job.setUpdateBy(getUsername());
  93. return toAjax(jobService.updateJob(job));
  94. }
  95. /**
  96. * 定时任务状态修改
  97. */
  98. @Log(title = "定时任务", businessType = BusinessType.UPDATE)
  99. @PutMapping("/changeStatus")
  100. public AjaxResult changeStatus(@RequestBody SysJob job) throws SchedulerException {
  101. SysJob newJob = jobService.selectJobById(job.getJobId());
  102. newJob.setStatus(job.getStatus());
  103. return toAjax(jobService.changeStatus(newJob));
  104. }
  105. /**
  106. * 定时任务立即执行一次
  107. */
  108. @Log(title = "定时任务", businessType = BusinessType.UPDATE)
  109. @PutMapping("/run")
  110. public AjaxResult run(@RequestBody SysJob job) throws SchedulerException {
  111. jobService.run(job);
  112. return AjaxResult.success();
  113. }
  114. /**
  115. * 删除定时任务
  116. */
  117. @Log(title = "定时任务", businessType = BusinessType.DELETE)
  118. @DeleteMapping("/{jobIds}")
  119. public AjaxResult remove(@PathVariable Long[] jobIds) throws SchedulerException, TaskException {
  120. jobService.deleteJobByIds(jobIds);
  121. return AjaxResult.success();
  122. }
  123. }