JobGroupController.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package com.xxl.job.admin.controller;
  2. import com.xxl.job.admin.core.model.XxlJobGroup;
  3. import com.xxl.job.admin.core.model.XxlJobRegistry;
  4. import com.xxl.job.admin.core.util.I18nUtil;
  5. import com.xxl.job.admin.service.IXxlJobGroupService;
  6. import com.xxl.job.admin.service.IXxlJobInfoService;
  7. import com.xxl.job.admin.service.IXxlJobRegistryService;
  8. import com.xxl.job.core.biz.model.ReturnT;
  9. import com.xxl.job.core.enums.RegistryConfig;
  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.ResponseBody;
  14. import javax.annotation.Resource;
  15. import java.util.*;
  16. /**
  17. * job group controller
  18. *
  19. * @author xuxueli 2016-10-02 20:52:56
  20. */
  21. @Controller
  22. @RequestMapping("/jobgroup")
  23. public class JobGroupController {
  24. @Resource
  25. public IXxlJobInfoService xxlJobInfoService;
  26. @Resource
  27. public IXxlJobGroupService xxlJobGroupService;
  28. @Resource
  29. private IXxlJobRegistryService xxlJobRegistryService;
  30. @RequestMapping
  31. public String index(Model model) {
  32. // job group (executor)
  33. List<XxlJobGroup> list = xxlJobGroupService.list();
  34. model.addAttribute("list", list);
  35. return "jobgroup/jobgroup.index";
  36. }
  37. @RequestMapping("/save")
  38. @ResponseBody
  39. public ReturnT<String> save(XxlJobGroup xxlJobGroup) {
  40. // valid
  41. if (xxlJobGroup.getAppName() == null || xxlJobGroup.getAppName().trim().length() == 0) {
  42. return new ReturnT<String>(500, (I18nUtil.getString("system_please_input") + "AppName"));
  43. }
  44. if (xxlJobGroup.getAppName().length() < 4 || xxlJobGroup.getAppName().length() > 64) {
  45. return new ReturnT<String>(500, I18nUtil.getString("jobgroup_field_appName_length"));
  46. }
  47. if (xxlJobGroup.getTitle() == null || xxlJobGroup.getTitle().trim().length() == 0) {
  48. return new ReturnT<String>(500, (I18nUtil.getString("system_please_input") + I18nUtil.getString("jobgroup_field_title")));
  49. }
  50. if (xxlJobGroup.getAddressType() != 0) {
  51. if (xxlJobGroup.getAddressList() == null || xxlJobGroup.getAddressList().trim().length() == 0) {
  52. return new ReturnT<String>(500, I18nUtil.getString("jobgroup_field_addressType_limit"));
  53. }
  54. String[] addresss = xxlJobGroup.getAddressList().split(",");
  55. for (String item : addresss) {
  56. if (item == null || item.trim().length() == 0) {
  57. return new ReturnT<String>(500, I18nUtil.getString("jobgroup_field_registryList_unvalid"));
  58. }
  59. }
  60. }
  61. boolean ret = xxlJobGroupService.save(xxlJobGroup);
  62. return (ret) ? ReturnT.SUCCESS : ReturnT.FAIL;
  63. }
  64. @RequestMapping("/update")
  65. @ResponseBody
  66. public ReturnT<String> update(XxlJobGroup xxlJobGroup) {
  67. // valid
  68. if (xxlJobGroup.getAppName() == null || xxlJobGroup.getAppName().trim().length() == 0) {
  69. return new ReturnT<String>(500, (I18nUtil.getString("system_please_input") + "AppName"));
  70. }
  71. if (xxlJobGroup.getAppName().length() < 4 || xxlJobGroup.getAppName().length() > 64) {
  72. return new ReturnT<String>(500, I18nUtil.getString("jobgroup_field_appName_length"));
  73. }
  74. if (xxlJobGroup.getTitle() == null || xxlJobGroup.getTitle().trim().length() == 0) {
  75. return new ReturnT<String>(500, (I18nUtil.getString("system_please_input") + I18nUtil.getString("jobgroup_field_title")));
  76. }
  77. if (xxlJobGroup.getAddressType() == 0) {
  78. // 0=自动注册
  79. List<String> registryList = findRegistryByAppName(xxlJobGroup.getAppName());
  80. String addressListStr = null;
  81. if (registryList != null && !registryList.isEmpty()) {
  82. Collections.sort(registryList);
  83. addressListStr = "";
  84. for (String item : registryList) {
  85. addressListStr += item + ",";
  86. }
  87. addressListStr = addressListStr.substring(0, addressListStr.length() - 1);
  88. }
  89. xxlJobGroup.setAddressList(addressListStr);
  90. } else {
  91. // 1=手动录入
  92. if (xxlJobGroup.getAddressList() == null || xxlJobGroup.getAddressList().trim().length() == 0) {
  93. return new ReturnT<String>(500, I18nUtil.getString("jobgroup_field_addressType_limit"));
  94. }
  95. String[] addresss = xxlJobGroup.getAddressList().split(",");
  96. for (String item : addresss) {
  97. if (item == null || item.trim().length() == 0) {
  98. return new ReturnT<String>(500, I18nUtil.getString("jobgroup_field_registryList_unvalid"));
  99. }
  100. }
  101. }
  102. boolean ret = xxlJobGroupService.updateById(xxlJobGroup);
  103. return (ret) ? ReturnT.SUCCESS : ReturnT.FAIL;
  104. }
  105. private List<String> findRegistryByAppName(String appNameParam) {
  106. HashMap<String, List<String>> appAddressMap = new HashMap<String, List<String>>();
  107. List<XxlJobRegistry> list = xxlJobRegistryService.findAll(RegistryConfig.DEAD_TIMEOUT, new Date());
  108. if (list != null) {
  109. for (XxlJobRegistry item : list) {
  110. if (RegistryConfig.RegistType.EXECUTOR.name().equals(item.getRegistryGroup())) {
  111. String appName = item.getRegistryKey();
  112. List<String> registryList = appAddressMap.get(appName);
  113. if (registryList == null) {
  114. registryList = new ArrayList<String>();
  115. }
  116. if (!registryList.contains(item.getRegistryValue())) {
  117. registryList.add(item.getRegistryValue());
  118. }
  119. appAddressMap.put(appName, registryList);
  120. }
  121. }
  122. }
  123. return appAddressMap.get(appNameParam);
  124. }
  125. @RequestMapping("/remove")
  126. @ResponseBody
  127. public ReturnT<String> remove(int id) {
  128. // valid
  129. int count = xxlJobInfoService.pageListCount(0, 10, id, -1, null, null, null);
  130. if (count > 0) {
  131. return new ReturnT<String>(500, I18nUtil.getString("jobgroup_del_limit_0"));
  132. }
  133. List<XxlJobGroup> allList = xxlJobGroupService.list();
  134. if (allList.size() == 1) {
  135. return new ReturnT<String>(500, I18nUtil.getString("jobgroup_del_limit_1"));
  136. }
  137. boolean ret = xxlJobGroupService.removeById(id);
  138. return (ret) ? ReturnT.SUCCESS : ReturnT.FAIL;
  139. }
  140. @RequestMapping("/loadById")
  141. @ResponseBody
  142. public ReturnT<XxlJobGroup> loadById(int id) {
  143. XxlJobGroup jobGroup = xxlJobGroupService.getById(id);
  144. return jobGroup != null ? new ReturnT<>(jobGroup) : new ReturnT<>(ReturnT.FAIL_CODE, null);
  145. }
  146. }