JobController.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package com.xxl.job.controller;
  2. import java.io.UnsupportedEncodingException;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.Map.Entry;
  7. import java.util.Set;
  8. import javax.servlet.http.HttpServletRequest;
  9. import org.apache.commons.lang.StringUtils;
  10. import org.quartz.CronExpression;
  11. import org.quartz.Job;
  12. import org.quartz.SchedulerException;
  13. import org.springframework.stereotype.Controller;
  14. import org.springframework.ui.Model;
  15. import org.springframework.web.bind.annotation.RequestMapping;
  16. import org.springframework.web.bind.annotation.ResponseBody;
  17. import com.xxl.job.client.handler.HandlerRepository;
  18. import com.xxl.job.core.model.ReturnT;
  19. import com.xxl.job.core.util.DynamicSchedulerUtil;
  20. import com.xxl.job.service.job.HttpJobBean;
  21. /**
  22. * index controller
  23. * @author xuxueli 2015-12-19 16:13:16
  24. */
  25. @Controller
  26. @RequestMapping("/job")
  27. public class JobController {
  28. @RequestMapping
  29. public String index(Model model) {
  30. List<Map<String, Object>> jobList = DynamicSchedulerUtil.getJobList();
  31. model.addAttribute("jobList", jobList);
  32. return "job/index";
  33. }
  34. @RequestMapping("/add")
  35. @ResponseBody
  36. public ReturnT<String> add(HttpServletRequest request) {
  37. String triggerKeyName = null;
  38. String cronExpression = null;
  39. Map<String, Object> jobData = new HashMap<String, Object>();
  40. try {
  41. request.setCharacterEncoding("utf-8");
  42. } catch (UnsupportedEncodingException e1) {
  43. e1.printStackTrace();
  44. }
  45. @SuppressWarnings("unchecked")
  46. Set<Map.Entry<String, String[]>> paramSet = request.getParameterMap().entrySet();
  47. for (Entry<String, String[]> param : paramSet) {
  48. if (param.getKey().equals("triggerKeyName")) {
  49. triggerKeyName = param.getValue()[0];
  50. } else if (param.getKey().equals("cronExpression")) {
  51. cronExpression = param.getValue()[0];
  52. } else {
  53. jobData.put(param.getKey(), param.getValue().length>0?param.getValue()[0]:param.getValue());
  54. }
  55. }
  56. // triggerKeyName
  57. if (StringUtils.isBlank(triggerKeyName)) {
  58. return new ReturnT<String>(500, "请输入“任务key”");
  59. }
  60. // cronExpression
  61. if (StringUtils.isBlank(cronExpression)) {
  62. return new ReturnT<String>(500, "请输入“任务corn”");
  63. }
  64. if (!CronExpression.isValidExpression(cronExpression)) {
  65. return new ReturnT<String>(500, "“任务corn”不合法");
  66. }
  67. // jobData
  68. if (jobData.get(HandlerRepository.job_desc)==null || jobData.get(HandlerRepository.job_desc).toString().trim().length()==0) {
  69. return new ReturnT<String>(500, "请输入“任务描述”");
  70. }
  71. if (jobData.get(HandlerRepository.job_url)==null || jobData.get(HandlerRepository.job_url).toString().trim().length()==0) {
  72. return new ReturnT<String>(500, "请输入“任务URL”");
  73. }
  74. if (jobData.get(HandlerRepository.handleName)==null || jobData.get(HandlerRepository.handleName).toString().trim().length()==0) {
  75. return new ReturnT<String>(500, "请输入“任务handler”");
  76. }
  77. // jobClass
  78. Class<? extends Job> jobClass = HttpJobBean.class;
  79. try {
  80. boolean result = DynamicSchedulerUtil.addJob(triggerKeyName, cronExpression, jobClass, jobData);
  81. if (!result) {
  82. return new ReturnT<String>(500, "任务ID重复,请更换确认");
  83. }
  84. return ReturnT.SUCCESS;
  85. } catch (SchedulerException e) {
  86. e.printStackTrace();
  87. }
  88. return ReturnT.FAIL;
  89. }
  90. @RequestMapping("/reschedule")
  91. @ResponseBody
  92. public ReturnT<String> reschedule(String triggerKeyName, String cronExpression) {
  93. // triggerKeyName
  94. if (StringUtils.isBlank(triggerKeyName)) {
  95. return new ReturnT<String>(500, "请输入“任务key”");
  96. }
  97. // cronExpression
  98. if (StringUtils.isBlank(cronExpression)) {
  99. return new ReturnT<String>(500, "请输入“任务corn”");
  100. }
  101. if (!CronExpression.isValidExpression(cronExpression)) {
  102. return new ReturnT<String>(500, "“任务corn”不合法");
  103. }
  104. try {
  105. DynamicSchedulerUtil.rescheduleJob(triggerKeyName, cronExpression);
  106. return ReturnT.SUCCESS;
  107. } catch (SchedulerException e) {
  108. e.printStackTrace();
  109. }
  110. return ReturnT.FAIL;
  111. }
  112. @RequestMapping("/remove")
  113. @ResponseBody
  114. public ReturnT<String> remove(String triggerKeyName) {
  115. try {
  116. DynamicSchedulerUtil.removeJob(triggerKeyName);
  117. return ReturnT.SUCCESS;
  118. } catch (SchedulerException e) {
  119. e.printStackTrace();
  120. return ReturnT.FAIL;
  121. }
  122. }
  123. @RequestMapping("/pause")
  124. @ResponseBody
  125. public ReturnT<String> pause(String triggerKeyName) {
  126. try {
  127. DynamicSchedulerUtil.pauseJob(triggerKeyName);
  128. return ReturnT.SUCCESS;
  129. } catch (SchedulerException e) {
  130. e.printStackTrace();
  131. return ReturnT.FAIL;
  132. }
  133. }
  134. @RequestMapping("/resume")
  135. @ResponseBody
  136. public ReturnT<String> resume(String triggerKeyName) {
  137. try {
  138. DynamicSchedulerUtil.resumeJob(triggerKeyName);
  139. return ReturnT.SUCCESS;
  140. } catch (SchedulerException e) {
  141. e.printStackTrace();
  142. return ReturnT.FAIL;
  143. }
  144. }
  145. @RequestMapping("/trigger")
  146. @ResponseBody
  147. public ReturnT<String> triggerJob(String triggerKeyName) {
  148. try {
  149. DynamicSchedulerUtil.triggerJob(triggerKeyName);
  150. return ReturnT.SUCCESS;
  151. } catch (SchedulerException e) {
  152. e.printStackTrace();
  153. return ReturnT.FAIL;
  154. }
  155. }
  156. }