JobController.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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.annotation.Resource;
  9. import javax.servlet.http.HttpServletRequest;
  10. import org.apache.commons.lang.StringUtils;
  11. import org.quartz.CronExpression;
  12. import org.quartz.Job;
  13. import org.quartz.SchedulerException;
  14. import org.springframework.stereotype.Controller;
  15. import org.springframework.ui.Model;
  16. import org.springframework.web.bind.annotation.RequestMapping;
  17. import org.springframework.web.bind.annotation.RequestParam;
  18. import org.springframework.web.bind.annotation.ResponseBody;
  19. import com.xxl.job.client.handler.HandlerRepository;
  20. import com.xxl.job.client.util.JacksonUtil;
  21. import com.xxl.job.core.model.ReturnT;
  22. import com.xxl.job.core.model.XxlJobInfo;
  23. import com.xxl.job.core.util.DynamicSchedulerUtil;
  24. import com.xxl.job.dao.IXxlJobInfoDao;
  25. import com.xxl.job.service.job.HttpJobBean;
  26. /**
  27. * index controller
  28. * @author xuxueli 2015-12-19 16:13:16
  29. */
  30. @Controller
  31. @RequestMapping("/job")
  32. public class JobController {
  33. @Resource
  34. private IXxlJobInfoDao xxlJobInfoDao;
  35. @RequestMapping
  36. public String index(Model model) {
  37. //List<Map<String, Object>> jobList = DynamicSchedulerUtil.getJobList();
  38. //model.addAttribute("jobList", jobList);
  39. return "job/index";
  40. }
  41. @RequestMapping("/pageList")
  42. @ResponseBody
  43. public Map<String, Object> pageList(@RequestParam(required = false, defaultValue = "0") int start,
  44. @RequestParam(required = false, defaultValue = "10") int length,
  45. String jobName, String filterTime) {
  46. // page list
  47. List<XxlJobInfo> list = xxlJobInfoDao.pageList(start, length, jobName, null, null);
  48. int list_count = xxlJobInfoDao.pageListCount(start, length, jobName, null, null);
  49. // fill job info
  50. if (list!=null && list.size()>0) {
  51. for (XxlJobInfo jobInfo : list) {
  52. DynamicSchedulerUtil.fillJobInfo(jobInfo);
  53. }
  54. }
  55. // package result
  56. Map<String, Object> maps = new HashMap<String, Object>();
  57. maps.put("recordsTotal", list_count); // 总记录数
  58. maps.put("recordsFiltered", list_count); // 过滤后的总记录数
  59. maps.put("data", list); // 分页列表
  60. return maps;
  61. }
  62. @RequestMapping("/add")
  63. @ResponseBody
  64. public ReturnT<String> add(HttpServletRequest request) {
  65. String triggerKeyName = null;
  66. String cronExpression = null;
  67. Map<String, String> jobData = new HashMap<String, String>();
  68. try {
  69. request.setCharacterEncoding("utf-8");
  70. } catch (UnsupportedEncodingException e1) {
  71. e1.printStackTrace();
  72. }
  73. @SuppressWarnings("unchecked")
  74. Set<Map.Entry<String, String[]>> paramSet = request.getParameterMap().entrySet();
  75. for (Entry<String, String[]> param : paramSet) {
  76. if (param.getKey().equals("triggerKeyName")) {
  77. triggerKeyName = param.getValue()[0];
  78. } else if (param.getKey().equals("cronExpression")) {
  79. cronExpression = param.getValue()[0];
  80. } else {
  81. jobData.put(param.getKey(), (String) (param.getValue().length>0?param.getValue()[0]:param.getValue()));
  82. }
  83. }
  84. // triggerKeyName
  85. if (StringUtils.isBlank(triggerKeyName)) {
  86. return new ReturnT<String>(500, "请输入“任务key”");
  87. }
  88. // cronExpression
  89. if (StringUtils.isBlank(cronExpression)) {
  90. return new ReturnT<String>(500, "请输入“任务corn”");
  91. }
  92. if (!CronExpression.isValidExpression(cronExpression)) {
  93. return new ReturnT<String>(500, "“任务corn”不合法");
  94. }
  95. // jobData
  96. if (jobData.get(HandlerRepository.job_desc)==null || jobData.get(HandlerRepository.job_desc).toString().trim().length()==0) {
  97. return new ReturnT<String>(500, "请输入“任务描述”");
  98. }
  99. if (jobData.get(HandlerRepository.job_url)==null || jobData.get(HandlerRepository.job_url).toString().trim().length()==0) {
  100. return new ReturnT<String>(500, "请输入“任务URL”");
  101. }
  102. if (jobData.get(HandlerRepository.handleName)==null || jobData.get(HandlerRepository.handleName).toString().trim().length()==0) {
  103. return new ReturnT<String>(500, "请输入“任务handler”");
  104. }
  105. // jobClass
  106. Class<? extends Job> jobClass = HttpJobBean.class;
  107. try {
  108. // add job 2 quartz
  109. boolean result = DynamicSchedulerUtil.addJob(triggerKeyName, cronExpression, jobClass, null);
  110. if (!result) {
  111. return new ReturnT<String>(500, "任务ID重复,请更换确认");
  112. }
  113. // Backup to the database
  114. XxlJobInfo jobInfo = new XxlJobInfo();
  115. jobInfo.setJobName(triggerKeyName);
  116. jobInfo.setJobCron(cronExpression);
  117. jobInfo.setJobClass(jobClass.getName());
  118. jobInfo.setJobData(JacksonUtil.writeValueAsString(jobData));
  119. xxlJobInfoDao.save(jobInfo);
  120. return ReturnT.SUCCESS;
  121. } catch (SchedulerException e) {
  122. e.printStackTrace();
  123. }
  124. return ReturnT.FAIL;
  125. }
  126. @RequestMapping("/reschedule")
  127. @ResponseBody
  128. public ReturnT<String> reschedule(String triggerKeyName, String cronExpression) {
  129. // triggerKeyName
  130. if (StringUtils.isBlank(triggerKeyName)) {
  131. return new ReturnT<String>(500, "请输入“任务key”");
  132. }
  133. // cronExpression
  134. if (StringUtils.isBlank(cronExpression)) {
  135. return new ReturnT<String>(500, "请输入“任务corn”");
  136. }
  137. if (!CronExpression.isValidExpression(cronExpression)) {
  138. return new ReturnT<String>(500, "“任务corn”不合法");
  139. }
  140. try {
  141. DynamicSchedulerUtil.rescheduleJob(triggerKeyName, cronExpression);
  142. // update
  143. XxlJobInfo jobInfo = xxlJobInfoDao.load(triggerKeyName);
  144. if (jobInfo!=null) {
  145. jobInfo.setJobCron(cronExpression);
  146. xxlJobInfoDao.update(jobInfo);
  147. }
  148. return ReturnT.SUCCESS;
  149. } catch (SchedulerException e) {
  150. e.printStackTrace();
  151. }
  152. return ReturnT.FAIL;
  153. }
  154. @RequestMapping("/remove")
  155. @ResponseBody
  156. public ReturnT<String> remove(String triggerKeyName) {
  157. try {
  158. if (triggerKeyName!=null) {
  159. DynamicSchedulerUtil.removeJob(triggerKeyName);
  160. xxlJobInfoDao.delete(triggerKeyName);
  161. return ReturnT.SUCCESS;
  162. }
  163. } catch (SchedulerException e) {
  164. e.printStackTrace();
  165. }
  166. return ReturnT.FAIL;
  167. }
  168. @RequestMapping("/pause")
  169. @ResponseBody
  170. public ReturnT<String> pause(String triggerKeyName) {
  171. try {
  172. DynamicSchedulerUtil.pauseJob(triggerKeyName);
  173. // update
  174. XxlJobInfo jobInfo = xxlJobInfoDao.load(triggerKeyName);
  175. if (jobInfo!=null) {
  176. jobInfo.setJobStatus("PAUSED");
  177. xxlJobInfoDao.update(jobInfo);
  178. }
  179. return ReturnT.SUCCESS;
  180. } catch (SchedulerException e) {
  181. e.printStackTrace();
  182. return ReturnT.FAIL;
  183. }
  184. }
  185. @RequestMapping("/resume")
  186. @ResponseBody
  187. public ReturnT<String> resume(String triggerKeyName) {
  188. try {
  189. DynamicSchedulerUtil.resumeJob(triggerKeyName);
  190. // update
  191. XxlJobInfo jobInfo = xxlJobInfoDao.load(triggerKeyName);
  192. if (jobInfo!=null) {
  193. jobInfo.setJobStatus("NORMAL");
  194. xxlJobInfoDao.update(jobInfo);
  195. }
  196. return ReturnT.SUCCESS;
  197. } catch (SchedulerException e) {
  198. e.printStackTrace();
  199. return ReturnT.FAIL;
  200. }
  201. }
  202. @RequestMapping("/trigger")
  203. @ResponseBody
  204. public ReturnT<String> triggerJob(String triggerKeyName) {
  205. try {
  206. DynamicSchedulerUtil.triggerJob(triggerKeyName);
  207. return ReturnT.SUCCESS;
  208. } catch (SchedulerException e) {
  209. e.printStackTrace();
  210. return ReturnT.FAIL;
  211. }
  212. }
  213. }