IndexController.java 5.4 KB

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