JobGroupController.java 5.5 KB

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