5fa133850a23fb9728dcf4fe7c66855f16a4b02f.svn-base 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package com.xxl.job.admin.service.impl;
  2. import com.xxl.job.admin.core.model.XxlJobInfo;
  3. import com.xxl.job.admin.core.model.XxlJobLog;
  4. import com.xxl.job.admin.core.thread.JobTriggerPoolHelper;
  5. import com.xxl.job.admin.core.trigger.TriggerTypeEnum;
  6. import com.xxl.job.admin.core.util.I18nUtil;
  7. import com.xxl.job.admin.dao.XxlJobGroupDao;
  8. import com.xxl.job.admin.dao.XxlJobInfoDao;
  9. import com.xxl.job.admin.dao.XxlJobLogDao;
  10. import com.xxl.job.admin.dao.XxlJobRegistryDao;
  11. import com.xxl.job.core.biz.AdminBiz;
  12. import com.xxl.job.core.biz.model.HandleCallbackParam;
  13. import com.xxl.job.core.biz.model.RegistryParam;
  14. import com.xxl.job.core.biz.model.ReturnT;
  15. import com.xxl.job.core.handler.IJobHandler;
  16. import org.slf4j.Logger;
  17. import org.slf4j.LoggerFactory;
  18. import org.springframework.stereotype.Service;
  19. import org.springframework.util.StringUtils;
  20. import javax.annotation.Resource;
  21. import java.text.MessageFormat;
  22. import java.util.Date;
  23. import java.util.List;
  24. /**
  25. * @author xuxueli 2017-07-27 21:54:20
  26. */
  27. @Service
  28. public class AdminBizImpl implements AdminBiz {
  29. private static Logger logger = LoggerFactory.getLogger(AdminBizImpl.class);
  30. @Resource
  31. public XxlJobLogDao xxlJobLogDao;
  32. @Resource
  33. private XxlJobInfoDao xxlJobInfoDao;
  34. @Resource
  35. private XxlJobRegistryDao xxlJobRegistryDao;
  36. @Resource
  37. private XxlJobGroupDao xxlJobGroupDao;
  38. @Override
  39. public ReturnT<String> callback(List<HandleCallbackParam> callbackParamList) {
  40. for (HandleCallbackParam handleCallbackParam: callbackParamList) {
  41. ReturnT<String> callbackResult = callback(handleCallbackParam);
  42. logger.debug(">>>>>>>>> JobApiController.callback {}, handleCallbackParam={}, callbackResult={}",
  43. (callbackResult.getCode()==IJobHandler.SUCCESS.getCode()?"success":"fail"), handleCallbackParam, callbackResult);
  44. }
  45. return ReturnT.SUCCESS;
  46. }
  47. private ReturnT<String> callback(HandleCallbackParam handleCallbackParam) {
  48. // valid log item
  49. XxlJobLog log = xxlJobLogDao.load(handleCallbackParam.getLogId());
  50. if (log == null) {
  51. return new ReturnT<String>(ReturnT.FAIL_CODE, "log item not found.");
  52. }
  53. if (log.getHandleCode() > 0) {
  54. return new ReturnT<String>(ReturnT.FAIL_CODE, "log repeate callback."); // avoid repeat callback, trigger child job etc
  55. }
  56. // trigger success, to trigger child job
  57. String callbackMsg = null;
  58. if (IJobHandler.SUCCESS.getCode() == handleCallbackParam.getExecuteResult().getCode()) {
  59. XxlJobInfo xxlJobInfo = xxlJobInfoDao.loadById(log.getJobId());
  60. if (xxlJobInfo!=null && xxlJobInfo.getChildJobId()!=null && xxlJobInfo.getChildJobId().trim().length()>0) {
  61. callbackMsg = "<br><br><span style=\"color:#00c0ef;\" > >>>>>>>>>>>"+ I18nUtil.getString("jobconf_trigger_child_run") +"<<<<<<<<<<< </span><br>";
  62. String[] childJobIds = xxlJobInfo.getChildJobId().split(",");
  63. for (int i = 0; i < childJobIds.length; i++) {
  64. int childJobId = (childJobIds[i]!=null && childJobIds[i].trim().length()>0 && isNumeric(childJobIds[i]))?Integer.valueOf(childJobIds[i]):-1;
  65. if (childJobId > 0) {
  66. JobTriggerPoolHelper.trigger(childJobId, TriggerTypeEnum.PARENT, -1, null, null, null);
  67. ReturnT<String> triggerChildResult = ReturnT.SUCCESS;
  68. // add msg
  69. callbackMsg += MessageFormat.format(I18nUtil.getString("jobconf_callback_child_msg1"),
  70. (i+1),
  71. childJobIds.length,
  72. childJobIds[i],
  73. (triggerChildResult.getCode()==ReturnT.SUCCESS_CODE?I18nUtil.getString("system_success"):I18nUtil.getString("system_fail")),
  74. triggerChildResult.getMsg());
  75. } else {
  76. callbackMsg += MessageFormat.format(I18nUtil.getString("jobconf_callback_child_msg2"),
  77. (i+1),
  78. childJobIds.length,
  79. childJobIds[i]);
  80. }
  81. }
  82. }
  83. }
  84. // handle msg
  85. StringBuffer handleMsg = new StringBuffer();
  86. if (log.getHandleMsg()!=null) {
  87. handleMsg.append(log.getHandleMsg()).append("<br>");
  88. }
  89. if (handleCallbackParam.getExecuteResult().getMsg() != null) {
  90. handleMsg.append(handleCallbackParam.getExecuteResult().getMsg());
  91. }
  92. if (callbackMsg != null) {
  93. handleMsg.append(callbackMsg);
  94. }
  95. if (handleMsg.length() > 15000) {
  96. handleMsg = new StringBuffer(handleMsg.substring(0, 15000)); // text最大64kb 避免长度过长
  97. }
  98. // success, save log
  99. log.setHandleTime(new Date());
  100. log.setHandleCode(handleCallbackParam.getExecuteResult().getCode());
  101. log.setHandleMsg(handleMsg.toString());
  102. xxlJobLogDao.updateHandleInfo(log);
  103. return ReturnT.SUCCESS;
  104. }
  105. private boolean isNumeric(String str){
  106. try {
  107. int result = Integer.valueOf(str);
  108. return true;
  109. } catch (NumberFormatException e) {
  110. return false;
  111. }
  112. }
  113. @Override
  114. public ReturnT<String> registry(RegistryParam registryParam) {
  115. // valid
  116. if (!StringUtils.hasText(registryParam.getRegistryGroup())
  117. || !StringUtils.hasText(registryParam.getRegistryKey())
  118. || !StringUtils.hasText(registryParam.getRegistryValue())) {
  119. return new ReturnT<String>(ReturnT.FAIL_CODE, "Illegal Argument.");
  120. }
  121. int ret = xxlJobRegistryDao.registryUpdate(registryParam.getRegistryGroup(), registryParam.getRegistryKey(), registryParam.getRegistryValue(), new Date());
  122. if (ret < 1) {
  123. xxlJobRegistryDao.registrySave(registryParam.getRegistryGroup(), registryParam.getRegistryKey(), registryParam.getRegistryValue(), new Date());
  124. // fresh
  125. freshGroupRegistryInfo(registryParam);
  126. }
  127. return ReturnT.SUCCESS;
  128. }
  129. @Override
  130. public ReturnT<String> registryRemove(RegistryParam registryParam) {
  131. // valid
  132. if (!StringUtils.hasText(registryParam.getRegistryGroup())
  133. || !StringUtils.hasText(registryParam.getRegistryKey())
  134. || !StringUtils.hasText(registryParam.getRegistryValue())) {
  135. return new ReturnT<String>(ReturnT.FAIL_CODE, "Illegal Argument.");
  136. }
  137. int ret = xxlJobRegistryDao.registryDelete(registryParam.getRegistryGroup(), registryParam.getRegistryKey(), registryParam.getRegistryValue());
  138. if (ret > 0) {
  139. // fresh
  140. freshGroupRegistryInfo(registryParam);
  141. }
  142. return ReturnT.SUCCESS;
  143. }
  144. private void freshGroupRegistryInfo(RegistryParam registryParam){
  145. // Under consideration, prevent affecting core tables
  146. }
  147. }