SysJobController.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. /**
  29. * 调度任务信息操作处理
  30. *
  31. * @author ruoyi
  32. */
  33. @RestController
  34. @RequestMapping("/job")
  35. public class SysJobController extends BaseController
  36. {
  37. @Autowired
  38. private ISysJobService jobService;
  39. /**
  40. * 查询定时任务列表
  41. */
  42. @RequiresPermissions("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. @RequiresPermissions("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. @RequiresPermissions("monitor:job:query")
  66. @GetMapping(value = "/{jobId}")
  67. public AjaxResult getInfo(@PathVariable("jobId") Long jobId)
  68. {
  69. return AjaxResult.success(jobService.selectJobById(jobId));
  70. }
  71. /**
  72. * 新增定时任务
  73. */
  74. @RequiresPermissions("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.containsIgnoreCase(job.getInvokeTarget(), Constants.LOOKUP_LDAP))
  88. {
  89. return error("新增任务'" + job.getJobName() + "'失败,目标字符串不允许'ldap://'调用");
  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. job.setCreateBy(SecurityUtils.getUsername());
  100. return toAjax(jobService.insertJob(job));
  101. }
  102. /**
  103. * 修改定时任务
  104. */
  105. @RequiresPermissions("monitor:job:edit")
  106. @Log(title = "定时任务", businessType = BusinessType.UPDATE)
  107. @PutMapping
  108. public AjaxResult edit(@RequestBody SysJob job) throws SchedulerException, TaskException
  109. {
  110. if (!CronUtils.isValid(job.getCronExpression()))
  111. {
  112. return error("修改任务'" + job.getJobName() + "'失败,Cron表达式不正确");
  113. }
  114. else if (StringUtils.containsIgnoreCase(job.getInvokeTarget(), Constants.LOOKUP_RMI))
  115. {
  116. return error("修改任务'" + job.getJobName() + "'失败,目标字符串不允许'rmi://'调用");
  117. }
  118. else if (StringUtils.containsIgnoreCase(job.getInvokeTarget(), Constants.LOOKUP_LDAP))
  119. {
  120. return error("修改任务'" + job.getJobName() + "'失败,目标字符串不允许'ldap://'调用");
  121. }
  122. else if (StringUtils.containsAnyIgnoreCase(job.getInvokeTarget(), new String[] { Constants.HTTP, Constants.HTTPS }))
  123. {
  124. return error("修改任务'" + job.getJobName() + "'失败,目标字符串不允许'http(s)//'调用");
  125. }
  126. else if (StringUtils.containsAnyIgnoreCase(job.getInvokeTarget(), Constants.JOB_ERROR_STR))
  127. {
  128. return error("修改任务'" + job.getJobName() + "'失败,目标字符串存在违规");
  129. }
  130. job.setUpdateBy(SecurityUtils.getUsername());
  131. return toAjax(jobService.updateJob(job));
  132. }
  133. /**
  134. * 定时任务状态修改
  135. */
  136. @RequiresPermissions("monitor:job:changeStatus")
  137. @Log(title = "定时任务", businessType = BusinessType.UPDATE)
  138. @PutMapping("/changeStatus")
  139. public AjaxResult changeStatus(@RequestBody SysJob job) throws SchedulerException
  140. {
  141. SysJob newJob = jobService.selectJobById(job.getJobId());
  142. newJob.setStatus(job.getStatus());
  143. return toAjax(jobService.changeStatus(newJob));
  144. }
  145. /**
  146. * 定时任务立即执行一次
  147. */
  148. @RequiresPermissions("monitor:job:changeStatus")
  149. @Log(title = "定时任务", businessType = BusinessType.UPDATE)
  150. @PutMapping("/run")
  151. public AjaxResult run(@RequestBody SysJob job) throws SchedulerException
  152. {
  153. jobService.run(job);
  154. return AjaxResult.success();
  155. }
  156. /**
  157. * 删除定时任务
  158. */
  159. @RequiresPermissions("monitor:job:remove")
  160. @Log(title = "定时任务", businessType = BusinessType.DELETE)
  161. @DeleteMapping("/{jobIds}")
  162. public AjaxResult remove(@PathVariable Long[] jobIds) throws SchedulerException, TaskException
  163. {
  164. jobService.deleteJobByIds(jobIds);
  165. return AjaxResult.success();
  166. }
  167. }