SysJobController.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package com.ruoyi.quartz.controller;
  2. import java.util.List;
  3. import javax.servlet.http.HttpServletResponse;
  4. import org.quartz.SchedulerException;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.security.access.prepost.PreAuthorize;
  7. import org.springframework.web.bind.annotation.DeleteMapping;
  8. import org.springframework.web.bind.annotation.GetMapping;
  9. import org.springframework.web.bind.annotation.PathVariable;
  10. import org.springframework.web.bind.annotation.PostMapping;
  11. import org.springframework.web.bind.annotation.PutMapping;
  12. import org.springframework.web.bind.annotation.RequestBody;
  13. import org.springframework.web.bind.annotation.RequestMapping;
  14. import org.springframework.web.bind.annotation.RestController;
  15. import com.ruoyi.common.annotation.Log;
  16. import com.ruoyi.common.constant.Constants;
  17. import com.ruoyi.common.core.controller.BaseController;
  18. import com.ruoyi.common.core.domain.AjaxResult;
  19. import com.ruoyi.common.core.page.TableDataInfo;
  20. import com.ruoyi.common.enums.BusinessType;
  21. import com.ruoyi.common.exception.job.TaskException;
  22. import com.ruoyi.common.utils.StringUtils;
  23. import com.ruoyi.common.utils.poi.ExcelUtil;
  24. import com.ruoyi.quartz.domain.SysJob;
  25. import com.ruoyi.quartz.service.ISysJobService;
  26. import com.ruoyi.quartz.util.CronUtils;
  27. import com.ruoyi.quartz.util.ScheduleUtils;
  28. /**
  29. * 调度任务信息操作处理
  30. *
  31. * @author ruoyi
  32. */
  33. @RestController
  34. @RequestMapping("/monitor/job")
  35. public class SysJobController extends BaseController
  36. {
  37. @Autowired
  38. private ISysJobService jobService;
  39. /**
  40. * 查询定时任务列表
  41. */
  42. @PreAuthorize("@ss.hasPermi('monitor:job:list')")
  43. @GetMapping("/list")
  44. public TableDataInfo list(SysJob sysJob)
  45. {
  46. startPage();
  47. List<SysJob> list = jobService.selectJobList(sysJob);
  48. return getDataTable(list);
  49. }
  50. /**
  51. * 导出定时任务列表
  52. */
  53. @PreAuthorize("@ss.hasPermi('monitor:job:export')")
  54. @Log(title = "定时任务", businessType = BusinessType.EXPORT)
  55. @PostMapping("/export")
  56. public void export(HttpServletResponse response, SysJob sysJob)
  57. {
  58. List<SysJob> list = jobService.selectJobList(sysJob);
  59. ExcelUtil<SysJob> util = new ExcelUtil<SysJob>(SysJob.class);
  60. util.exportExcel(response, list, "定时任务");
  61. }
  62. /**
  63. * 获取定时任务详细信息
  64. */
  65. @PreAuthorize("@ss.hasPermi('monitor:job:query')")
  66. @GetMapping(value = "/{jobId}")
  67. public AjaxResult getInfo(@PathVariable("jobId") Long jobId)
  68. {
  69. return success(jobService.selectJobById(jobId));
  70. }
  71. /**
  72. * 新增定时任务
  73. */
  74. @PreAuthorize("@ss.hasPermi('monitor:job:add')")
  75. @Log(title = "定时任务", businessType = BusinessType.INSERT)
  76. @PostMapping
  77. public AjaxResult add(@RequestBody SysJob job) throws SchedulerException, TaskException
  78. {
  79. if (!CronUtils.isValid(job.getCronExpression()))
  80. {
  81. return error("新增任务'" + job.getJobName() + "'失败,Cron表达式不正确");
  82. }
  83. else if (StringUtils.containsIgnoreCase(job.getInvokeTarget(), Constants.LOOKUP_RMI))
  84. {
  85. return error("新增任务'" + job.getJobName() + "'失败,目标字符串不允许'rmi'调用");
  86. }
  87. else if (StringUtils.containsAnyIgnoreCase(job.getInvokeTarget(), new String[] { Constants.LOOKUP_LDAP, Constants.LOOKUP_LDAPS }))
  88. {
  89. return error("新增任务'" + job.getJobName() + "'失败,目标字符串不允许'ldap(s)'调用");
  90. }
  91. else if (StringUtils.containsAnyIgnoreCase(job.getInvokeTarget(), new String[] { Constants.HTTP, Constants.HTTPS }))
  92. {
  93. return error("新增任务'" + job.getJobName() + "'失败,目标字符串不允许'http(s)'调用");
  94. }
  95. else if (StringUtils.containsAnyIgnoreCase(job.getInvokeTarget(), Constants.JOB_ERROR_STR))
  96. {
  97. return error("新增任务'" + job.getJobName() + "'失败,目标字符串存在违规");
  98. }
  99. else if (!ScheduleUtils.whiteList(job.getInvokeTarget()))
  100. {
  101. return error("新增任务'" + job.getJobName() + "'失败,目标字符串不在白名单内");
  102. }
  103. job.setCreateBy(getUsername());
  104. return toAjax(jobService.insertJob(job));
  105. }
  106. /**
  107. * 修改定时任务
  108. */
  109. @PreAuthorize("@ss.hasPermi('monitor:job:edit')")
  110. @Log(title = "定时任务", businessType = BusinessType.UPDATE)
  111. @PutMapping
  112. public AjaxResult edit(@RequestBody SysJob job) throws SchedulerException, TaskException
  113. {
  114. if (!CronUtils.isValid(job.getCronExpression()))
  115. {
  116. return error("修改任务'" + job.getJobName() + "'失败,Cron表达式不正确");
  117. }
  118. else if (StringUtils.containsIgnoreCase(job.getInvokeTarget(), Constants.LOOKUP_RMI))
  119. {
  120. return error("修改任务'" + job.getJobName() + "'失败,目标字符串不允许'rmi'调用");
  121. }
  122. else if (StringUtils.containsAnyIgnoreCase(job.getInvokeTarget(), new String[] { Constants.LOOKUP_LDAP, Constants.LOOKUP_LDAPS }))
  123. {
  124. return error("修改任务'" + job.getJobName() + "'失败,目标字符串不允许'ldap(s)'调用");
  125. }
  126. else if (StringUtils.containsAnyIgnoreCase(job.getInvokeTarget(), new String[] { Constants.HTTP, Constants.HTTPS }))
  127. {
  128. return error("修改任务'" + job.getJobName() + "'失败,目标字符串不允许'http(s)'调用");
  129. }
  130. else if (StringUtils.containsAnyIgnoreCase(job.getInvokeTarget(), Constants.JOB_ERROR_STR))
  131. {
  132. return error("修改任务'" + job.getJobName() + "'失败,目标字符串存在违规");
  133. }
  134. else if (!ScheduleUtils.whiteList(job.getInvokeTarget()))
  135. {
  136. return error("修改任务'" + job.getJobName() + "'失败,目标字符串不在白名单内");
  137. }
  138. job.setUpdateBy(getUsername());
  139. return toAjax(jobService.updateJob(job));
  140. }
  141. /**
  142. * 定时任务状态修改
  143. */
  144. @PreAuthorize("@ss.hasPermi('monitor:job:changeStatus')")
  145. @Log(title = "定时任务", businessType = BusinessType.UPDATE)
  146. @PutMapping("/changeStatus")
  147. public AjaxResult changeStatus(@RequestBody SysJob job) throws SchedulerException
  148. {
  149. SysJob newJob = jobService.selectJobById(job.getJobId());
  150. newJob.setStatus(job.getStatus());
  151. return toAjax(jobService.changeStatus(newJob));
  152. }
  153. /**
  154. * 定时任务立即执行一次
  155. */
  156. @PreAuthorize("@ss.hasPermi('monitor:job:changeStatus')")
  157. @Log(title = "定时任务", businessType = BusinessType.UPDATE)
  158. @PutMapping("/run")
  159. public AjaxResult run(@RequestBody SysJob job) throws SchedulerException
  160. {
  161. boolean result = jobService.run(job);
  162. return result ? success() : error("任务不存在或已过期!");
  163. }
  164. /**
  165. * 删除定时任务
  166. */
  167. @PreAuthorize("@ss.hasPermi('monitor:job:remove')")
  168. @Log(title = "定时任务", businessType = BusinessType.DELETE)
  169. @DeleteMapping("/{jobIds}")
  170. public AjaxResult remove(@PathVariable Long[] jobIds) throws SchedulerException, TaskException
  171. {
  172. jobService.deleteJobByIds(jobIds);
  173. return success();
  174. }
  175. }