SysJobController.java 5.7 KB

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