SysJobController.java 7.4 KB

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