JobApiController.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package com.xxl.job.admin.controller;
  2. import com.xxl.job.admin.controller.annotation.PermessionLimit;
  3. import com.xxl.job.admin.core.model.XxlJobInfo;
  4. import com.xxl.job.admin.core.model.XxlJobLog;
  5. import com.xxl.job.admin.core.schedule.XxlJobDynamicScheduler;
  6. import com.xxl.job.admin.dao.IXxlJobInfoDao;
  7. import com.xxl.job.admin.dao.IXxlJobLogDao;
  8. import com.xxl.job.admin.dao.IXxlJobRegistryDao;
  9. import com.xxl.job.core.biz.model.HandleCallbackParam;
  10. import com.xxl.job.core.biz.model.RegistryParam;
  11. import com.xxl.job.core.biz.model.ReturnT;
  12. import com.xxl.job.core.util.AdminApiUtil;
  13. import org.apache.commons.lang.StringUtils;
  14. import org.quartz.SchedulerException;
  15. import org.slf4j.Logger;
  16. import org.slf4j.LoggerFactory;
  17. import org.springframework.stereotype.Controller;
  18. import org.springframework.web.bind.annotation.RequestBody;
  19. import org.springframework.web.bind.annotation.RequestMapping;
  20. import org.springframework.web.bind.annotation.RequestMethod;
  21. import org.springframework.web.bind.annotation.ResponseBody;
  22. import javax.annotation.Resource;
  23. import java.text.MessageFormat;
  24. import java.util.Date;
  25. import java.util.List;
  26. /**
  27. * Created by xuxueli on 17/5/10.
  28. */
  29. @Controller
  30. public class JobApiController {
  31. private static Logger logger = LoggerFactory.getLogger(JobApiController.class);
  32. @Resource
  33. public IXxlJobLogDao xxlJobLogDao;
  34. @Resource
  35. private IXxlJobInfoDao xxlJobInfoDao;
  36. @Resource
  37. private IXxlJobRegistryDao xxlJobRegistryDao;
  38. @RequestMapping(value= AdminApiUtil.CALLBACK, method = RequestMethod.POST, consumes = "application/json")
  39. @ResponseBody
  40. @PermessionLimit(limit=false)
  41. public ReturnT<String> callback(@RequestBody List<HandleCallbackParam> callbackParamList){
  42. for (HandleCallbackParam handleCallbackParam: callbackParamList) {
  43. ReturnT<String> callbackResult = callback(handleCallbackParam);
  44. logger.info("JobApiController.callback {}, handleCallbackParam={}, callbackResult={}",
  45. (callbackResult.getCode()==ReturnT.SUCCESS_CODE?"success":"fail"), handleCallbackParam, callbackResult);
  46. }
  47. return ReturnT.SUCCESS;
  48. }
  49. private ReturnT<String> callback(HandleCallbackParam handleCallbackParam) {
  50. // valid log item
  51. XxlJobLog log = xxlJobLogDao.load(handleCallbackParam.getLogId());
  52. if (log == null) {
  53. return new ReturnT<String>(ReturnT.FAIL_CODE, "log item not found.");
  54. }
  55. // trigger success, to trigger child job, and avoid repeat trigger child job
  56. String childTriggerMsg = null;
  57. if (ReturnT.SUCCESS_CODE==handleCallbackParam.getExecuteResult().getCode() && ReturnT.SUCCESS_CODE!=log.getHandleCode()) {
  58. XxlJobInfo xxlJobInfo = xxlJobInfoDao.loadById(log.getJobId());
  59. if (xxlJobInfo!=null && StringUtils.isNotBlank(xxlJobInfo.getChildJobKey())) {
  60. childTriggerMsg = "<hr>";
  61. String[] childJobKeys = xxlJobInfo.getChildJobKey().split(",");
  62. for (int i = 0; i < childJobKeys.length; i++) {
  63. String[] jobKeyArr = childJobKeys[i].split("_");
  64. if (jobKeyArr!=null && jobKeyArr.length==2) {
  65. XxlJobInfo childJobInfo = xxlJobInfoDao.loadById(Integer.valueOf(jobKeyArr[1]));
  66. if (childJobInfo!=null) {
  67. try {
  68. boolean ret = XxlJobDynamicScheduler.triggerJob(String.valueOf(childJobInfo.getId()), String.valueOf(childJobInfo.getJobGroup()));
  69. // add msg
  70. childTriggerMsg += MessageFormat.format("<br> {0}/{1} 触发子任务成功, 子任务Key: {2}, status: {3}, 子任务描述: {4}",
  71. (i+1), childJobKeys.length, childJobKeys[i], ret, childJobInfo.getJobDesc());
  72. } catch (SchedulerException e) {
  73. logger.error("", e);
  74. }
  75. } else {
  76. childTriggerMsg += MessageFormat.format("<br> {0}/{1} 触发子任务失败, 子任务xxlJobInfo不存在, 子任务Key: {2}",
  77. (i+1), childJobKeys.length, childJobKeys[i]);
  78. }
  79. } else {
  80. childTriggerMsg += MessageFormat.format("<br> {0}/{1} 触发子任务失败, 子任务Key格式错误, 子任务Key: {2}",
  81. (i+1), childJobKeys.length, childJobKeys[i]);
  82. }
  83. }
  84. }
  85. }
  86. // handle msg
  87. StringBuffer handleMsg = new StringBuffer();
  88. if (log.getHandleMsg()!=null) {
  89. handleMsg.append(log.getHandleMsg()).append("<br>");
  90. }
  91. if (handleCallbackParam.getExecuteResult().getMsg() != null) {
  92. handleMsg.append(handleCallbackParam.getExecuteResult().getMsg());
  93. }
  94. if (childTriggerMsg !=null) {
  95. handleMsg.append("<br>子任务触发备注:").append(childTriggerMsg);
  96. }
  97. // success, save log
  98. log.setHandleTime(new Date());
  99. log.setHandleCode(handleCallbackParam.getExecuteResult().getCode());
  100. log.setHandleMsg(handleMsg.toString());
  101. xxlJobLogDao.updateHandleInfo(log);
  102. return ReturnT.SUCCESS;
  103. }
  104. @RequestMapping(value=AdminApiUtil.REGISTRY, method = RequestMethod.POST, consumes = "application/json")
  105. @ResponseBody
  106. @PermessionLimit(limit=false)
  107. public ReturnT<String> registry(@RequestBody RegistryParam registryParam){
  108. int ret = xxlJobRegistryDao.registryUpdate(registryParam.getRegistGroup(), registryParam.getRegistryKey(), registryParam.getRegistryValue());
  109. if (ret < 1) {
  110. xxlJobRegistryDao.registrySave(registryParam.getRegistGroup(), registryParam.getRegistryKey(), registryParam.getRegistryValue());
  111. }
  112. return ReturnT.SUCCESS;
  113. }
  114. }