SysJobController.java 6.8 KB

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