SysJobController.java 6.8 KB

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