IndexController.java 4.0 KB

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