SysJobController.java 5.7 KB

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