IndexController.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package com.xxl.controller;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Map;
  5. import org.apache.commons.lang.StringUtils;
  6. import org.quartz.CronExpression;
  7. import org.quartz.Job;
  8. import org.quartz.SchedulerException;
  9. import org.springframework.stereotype.Controller;
  10. import org.springframework.ui.Model;
  11. import org.springframework.web.bind.annotation.RequestMapping;
  12. import org.springframework.web.bind.annotation.ResponseBody;
  13. import com.xxl.quartz.DynamicSchedulerUtil;
  14. import com.xxl.quartz.ReturnT;
  15. @Controller
  16. public class IndexController {
  17. @RequestMapping("")
  18. public String index(Model model) {
  19. List<Map<String, Object>> jobList = DynamicSchedulerUtil.getJobList();
  20. model.addAttribute("jobList", jobList);
  21. return "job/index";
  22. }
  23. @RequestMapping("/add")
  24. @ResponseBody
  25. public ReturnT<String> add(String triggerKeyName, String cronExpression, String jobClassName, String jobDesc) {
  26. // triggerKeyName
  27. if (StringUtils.isBlank(triggerKeyName)) {
  28. return new ReturnT<String>(500, "请输入“任务key”");
  29. }
  30. // cronExpression
  31. if (StringUtils.isBlank(cronExpression)) {
  32. return new ReturnT<String>(500, "请输入“任务corn”");
  33. }
  34. if (!CronExpression.isValidExpression(cronExpression)) {
  35. return new ReturnT<String>(500, "“任务corn”不合法");
  36. }
  37. // jobClassName
  38. Class<?> clazz = null;
  39. try {
  40. clazz = Class.forName(jobClassName);
  41. } catch (ClassNotFoundException e1) {
  42. e1.printStackTrace();
  43. }
  44. if (clazz == null) {
  45. return new ReturnT<String>(500, "“任务Impl”不合法");
  46. }
  47. if (!Job.class.isAssignableFrom(clazz)) {
  48. return new ReturnT<String>(500, "“任务Impl”类必须继承Job接口");
  49. }
  50. @SuppressWarnings("unchecked")
  51. Class<? extends Job> jobClass = (Class<? extends Job>)clazz;
  52. // jobDesc
  53. if (StringUtils.isBlank(jobDesc)) {
  54. return new ReturnT<String>(500, "请输入“任务描述”");
  55. }
  56. try {
  57. Map<String, Object> jobData = new HashMap<String, Object>();
  58. jobData.put(DynamicSchedulerUtil.job_desc, jobDesc);
  59. DynamicSchedulerUtil.addJob(triggerKeyName, cronExpression, jobClass, jobData);
  60. return ReturnT.SUCCESS;
  61. } catch (SchedulerException e) {
  62. e.printStackTrace();
  63. }
  64. return ReturnT.FAIL;
  65. }
  66. @RequestMapping("/reschedule")
  67. @ResponseBody
  68. public ReturnT<String> reschedule(String triggerKeyName, String cronExpression) {
  69. // triggerKeyName
  70. if (StringUtils.isBlank(triggerKeyName)) {
  71. return new ReturnT<String>(500, "请输入“任务key”");
  72. }
  73. // cronExpression
  74. if (StringUtils.isBlank(cronExpression)) {
  75. return new ReturnT<String>(500, "请输入“任务corn”");
  76. }
  77. if (!CronExpression.isValidExpression(cronExpression)) {
  78. return new ReturnT<String>(500, "“任务corn”不合法");
  79. }
  80. try {
  81. DynamicSchedulerUtil.rescheduleJob(triggerKeyName, cronExpression);
  82. return ReturnT.SUCCESS;
  83. } catch (SchedulerException e) {
  84. e.printStackTrace();
  85. }
  86. return ReturnT.FAIL;
  87. }
  88. @RequestMapping("/remove")
  89. @ResponseBody
  90. public ReturnT<String> remove(String triggerKeyName) {
  91. try {
  92. DynamicSchedulerUtil.removeJob(triggerKeyName);
  93. return ReturnT.SUCCESS;
  94. } catch (SchedulerException e) {
  95. e.printStackTrace();
  96. return ReturnT.FAIL;
  97. }
  98. }
  99. @RequestMapping("/pause")
  100. @ResponseBody
  101. public ReturnT<String> pause(String triggerKeyName) {
  102. try {
  103. DynamicSchedulerUtil.pauseJob(triggerKeyName);
  104. return ReturnT.SUCCESS;
  105. } catch (SchedulerException e) {
  106. e.printStackTrace();
  107. return ReturnT.FAIL;
  108. }
  109. }
  110. @RequestMapping("/resume")
  111. @ResponseBody
  112. public ReturnT<String> resume(String triggerKeyName) {
  113. try {
  114. DynamicSchedulerUtil.resumeJob(triggerKeyName);
  115. return ReturnT.SUCCESS;
  116. } catch (SchedulerException e) {
  117. e.printStackTrace();
  118. return ReturnT.FAIL;
  119. }
  120. }
  121. @RequestMapping("/help")
  122. public String help(Model model) {
  123. return "job/help";
  124. }
  125. }