JobLogController.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package com.xxl.job.controller;
  2. import java.text.ParseException;
  3. import java.util.Date;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import javax.annotation.Resource;
  8. import org.apache.commons.lang.StringUtils;
  9. import org.apache.commons.lang.time.DateUtils;
  10. import org.springframework.stereotype.Controller;
  11. import org.springframework.ui.Model;
  12. import org.springframework.web.bind.annotation.RequestMapping;
  13. import org.springframework.web.bind.annotation.RequestParam;
  14. import org.springframework.web.bind.annotation.ResponseBody;
  15. import com.xxl.job.client.handler.HandlerRepository;
  16. import com.xxl.job.client.util.HttpUtil;
  17. import com.xxl.job.client.util.HttpUtil.RemoteCallBack;
  18. import com.xxl.job.client.util.JacksonUtil;
  19. import com.xxl.job.core.constant.Constants.JobGroupEnum;
  20. import com.xxl.job.core.model.ReturnT;
  21. import com.xxl.job.core.model.XxlJobLog;
  22. import com.xxl.job.dao.IXxlJobLogDao;
  23. /**
  24. * index controller
  25. * @author xuxueli 2015-12-19 16:13:16
  26. */
  27. @Controller
  28. @RequestMapping("/joblog")
  29. public class JobLogController {
  30. @Resource
  31. public IXxlJobLogDao xxlJobLogDao;
  32. @RequestMapping
  33. public String index(Model model, String jobGroup, String jobName) {
  34. model.addAttribute("jobGroup", jobGroup);
  35. model.addAttribute("jobName", jobName);
  36. model.addAttribute("JobGroupList", JobGroupEnum.values());
  37. return "joblog/index";
  38. }
  39. @RequestMapping("/pageList")
  40. @ResponseBody
  41. public Map<String, Object> pageList(@RequestParam(required = false, defaultValue = "0") int start,
  42. @RequestParam(required = false, defaultValue = "10") int length,
  43. String jobGroup, String jobName, String filterTime) {
  44. // parse param
  45. Date triggerTimeStart = null;
  46. Date triggerTimeEnd = null;
  47. if (StringUtils.isNotBlank(filterTime)) {
  48. String[] temp = filterTime.split(" - ");
  49. if (temp!=null && temp.length == 2) {
  50. try {
  51. triggerTimeStart = DateUtils.parseDate(temp[0], new String[]{"yyyy-MM-dd HH:mm:ss"});
  52. triggerTimeEnd = DateUtils.parseDate(temp[1], new String[]{"yyyy-MM-dd HH:mm:ss"});
  53. } catch (ParseException e) { }
  54. }
  55. }
  56. // page query
  57. List<XxlJobLog> list = xxlJobLogDao.pageList(start, length, jobGroup, jobName, triggerTimeStart, triggerTimeEnd);
  58. int list_count = xxlJobLogDao.pageListCount(start, length, jobGroup, jobName, triggerTimeStart, triggerTimeEnd);
  59. // package result
  60. Map<String, Object> maps = new HashMap<String, Object>();
  61. maps.put("recordsTotal", list_count); // 总记录数
  62. maps.put("recordsFiltered", list_count); // 过滤后的总记录数
  63. maps.put("data", list); // 分页列表
  64. return maps;
  65. }
  66. @RequestMapping("/save")
  67. @ResponseBody
  68. public RemoteCallBack triggerLog(int trigger_log_id, String status, String msg) {
  69. RemoteCallBack callBack = new RemoteCallBack();
  70. callBack.setStatus(RemoteCallBack.FAIL);
  71. XxlJobLog log = xxlJobLogDao.load(trigger_log_id);
  72. if (log!=null) {
  73. log.setHandleTime(new Date());
  74. log.setHandleStatus(status);
  75. log.setHandleMsg(msg);
  76. xxlJobLogDao.updateHandleInfo(log);
  77. callBack.setStatus(RemoteCallBack.SUCCESS);
  78. return callBack;
  79. }
  80. return callBack;
  81. }
  82. @RequestMapping("/logDetail")
  83. @ResponseBody
  84. public ReturnT<String> logDetail(int id){
  85. // base check
  86. XxlJobLog log = xxlJobLogDao.load(id);
  87. if (log == null) {
  88. return new ReturnT<String>(500, "参数异常");
  89. }
  90. // server address
  91. @SuppressWarnings("unchecked")
  92. Map<String, String> jobDataMap = JacksonUtil.readValue(log.getJobData(), Map.class);
  93. String handler_address = jobDataMap.get(HandlerRepository.HANDLER_ADDRESS);
  94. if (!handler_address.startsWith("http")){
  95. handler_address = "http://" + handler_address + "/";
  96. }
  97. // trigger id, trigger time
  98. Map<String, String> reqMap = new HashMap<String, String>();
  99. reqMap.put(HandlerRepository.NAMESPACE, HandlerRepository.NameSpaceEnum.LOG.name());
  100. reqMap.put(HandlerRepository.TRIGGER_LOG_ID, String.valueOf(id));
  101. reqMap.put(HandlerRepository.TRIGGER_TIMESTAMP, String.valueOf(log.getTriggerTime().getTime()));
  102. RemoteCallBack callBack = HttpUtil.post(handler_address, reqMap);
  103. if (HttpUtil.RemoteCallBack.SUCCESS.equals(callBack.getStatus())) {
  104. return new ReturnT<String>(callBack.getMsg());
  105. } else {
  106. return new ReturnT<String>(500, callBack.getMsg());
  107. }
  108. }
  109. @RequestMapping("/logDetailPage")
  110. public String logDetailPage(int id, Model model){
  111. ReturnT<String> data = logDetail(id);
  112. model.addAttribute("result", data);
  113. return "joblog/logdetail";
  114. }
  115. }