2b564dd5be20818de72e6cc8ed4371207a8cf2f5.svn-base 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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.dao.XxlJobGroupDao;
  6. import com.xxl.job.admin.dao.XxlJobInfoDao;
  7. import com.xxl.job.admin.dao.XxlJobRegistryDao;
  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.RequestParam;
  14. import org.springframework.web.bind.annotation.ResponseBody;
  15. import javax.annotation.Resource;
  16. import javax.servlet.http.HttpServletRequest;
  17. import java.util.*;
  18. /**
  19. * job group controller
  20. * @author xuxueli 2016-10-02 20:52:56
  21. */
  22. @Controller
  23. @RequestMapping("/jobgroup")
  24. public class JobGroupController {
  25. @Resource
  26. public XxlJobInfoDao xxlJobInfoDao;
  27. @Resource
  28. public XxlJobGroupDao xxlJobGroupDao;
  29. @Resource
  30. private XxlJobRegistryDao xxlJobRegistryDao;
  31. @RequestMapping
  32. public String index(Model model) {
  33. return "jobgroup/jobgroup.index";
  34. }
  35. @RequestMapping("/pageList")
  36. @ResponseBody
  37. public Map<String, Object> pageList(HttpServletRequest request,
  38. @RequestParam(required = false, defaultValue = "0") int start,
  39. @RequestParam(required = false, defaultValue = "10") int length,
  40. String appname, String title) {
  41. // page query
  42. List<XxlJobGroup> list = xxlJobGroupDao.pageList(start, length, appname, title);
  43. int list_count = xxlJobGroupDao.pageListCount(start, length, appname, title);
  44. // package result
  45. Map<String, Object> maps = new HashMap<String, Object>();
  46. maps.put("recordsTotal", list_count); // 总记录数
  47. maps.put("recordsFiltered", list_count); // 过滤后的总记录数
  48. maps.put("data", list); // 分页列表
  49. return maps;
  50. }
  51. @RequestMapping("/save")
  52. @ResponseBody
  53. public ReturnT<String> save(XxlJobGroup xxlJobGroup){
  54. // valid
  55. if (xxlJobGroup.getAppname()==null || xxlJobGroup.getAppname().trim().length()==0) {
  56. return new ReturnT<String>(500, (I18nUtil.getString("system_please_input")+"AppName") );
  57. }
  58. if (xxlJobGroup.getAppname().length()<4 || xxlJobGroup.getAppname().length()>64) {
  59. return new ReturnT<String>(500, I18nUtil.getString("jobgroup_field_appname_length") );
  60. }
  61. if (xxlJobGroup.getTitle()==null || xxlJobGroup.getTitle().trim().length()==0) {
  62. return new ReturnT<String>(500, (I18nUtil.getString("system_please_input") + I18nUtil.getString("jobgroup_field_title")) );
  63. }
  64. if (xxlJobGroup.getAddressType()!=0) {
  65. if (xxlJobGroup.getAddressList()==null || xxlJobGroup.getAddressList().trim().length()==0) {
  66. return new ReturnT<String>(500, I18nUtil.getString("jobgroup_field_addressType_limit") );
  67. }
  68. String[] addresss = xxlJobGroup.getAddressList().split(",");
  69. for (String item: addresss) {
  70. if (item==null || item.trim().length()==0) {
  71. return new ReturnT<String>(500, I18nUtil.getString("jobgroup_field_registryList_unvalid") );
  72. }
  73. }
  74. }
  75. int ret = xxlJobGroupDao.save(xxlJobGroup);
  76. return (ret>0)?ReturnT.SUCCESS:ReturnT.FAIL;
  77. }
  78. @RequestMapping("/update")
  79. @ResponseBody
  80. public ReturnT<String> update(XxlJobGroup xxlJobGroup){
  81. // valid
  82. if (xxlJobGroup.getAppname()==null || xxlJobGroup.getAppname().trim().length()==0) {
  83. return new ReturnT<String>(500, (I18nUtil.getString("system_please_input")+"AppName") );
  84. }
  85. if (xxlJobGroup.getAppname().length()<4 || xxlJobGroup.getAppname().length()>64) {
  86. return new ReturnT<String>(500, I18nUtil.getString("jobgroup_field_appname_length") );
  87. }
  88. if (xxlJobGroup.getTitle()==null || xxlJobGroup.getTitle().trim().length()==0) {
  89. return new ReturnT<String>(500, (I18nUtil.getString("system_please_input") + I18nUtil.getString("jobgroup_field_title")) );
  90. }
  91. if (xxlJobGroup.getAddressType() == 0) {
  92. // 0=自动注册
  93. List<String> registryList = findRegistryByAppName(xxlJobGroup.getAppname());
  94. String addressListStr = null;
  95. if (registryList!=null && !registryList.isEmpty()) {
  96. Collections.sort(registryList);
  97. addressListStr = "";
  98. for (String item:registryList) {
  99. addressListStr += item + ",";
  100. }
  101. addressListStr = addressListStr.substring(0, addressListStr.length()-1);
  102. }
  103. xxlJobGroup.setAddressList(addressListStr);
  104. } else {
  105. // 1=手动录入
  106. if (xxlJobGroup.getAddressList()==null || xxlJobGroup.getAddressList().trim().length()==0) {
  107. return new ReturnT<String>(500, I18nUtil.getString("jobgroup_field_addressType_limit") );
  108. }
  109. String[] addresss = xxlJobGroup.getAddressList().split(",");
  110. for (String item: addresss) {
  111. if (item==null || item.trim().length()==0) {
  112. return new ReturnT<String>(500, I18nUtil.getString("jobgroup_field_registryList_unvalid") );
  113. }
  114. }
  115. }
  116. int ret = xxlJobGroupDao.update(xxlJobGroup);
  117. return (ret>0)?ReturnT.SUCCESS:ReturnT.FAIL;
  118. }
  119. private List<String> findRegistryByAppName(String appnameParam){
  120. HashMap<String, List<String>> appAddressMap = new HashMap<String, List<String>>();
  121. List<XxlJobRegistry> list = xxlJobRegistryDao.findAll(RegistryConfig.DEAD_TIMEOUT, new Date());
  122. if (list != null) {
  123. for (XxlJobRegistry item: list) {
  124. if (RegistryConfig.RegistType.EXECUTOR.name().equals(item.getRegistryGroup())) {
  125. String appname = item.getRegistryKey();
  126. List<String> registryList = appAddressMap.get(appname);
  127. if (registryList == null) {
  128. registryList = new ArrayList<String>();
  129. }
  130. if (!registryList.contains(item.getRegistryValue())) {
  131. registryList.add(item.getRegistryValue());
  132. }
  133. appAddressMap.put(appname, registryList);
  134. }
  135. }
  136. }
  137. return appAddressMap.get(appnameParam);
  138. }
  139. @RequestMapping("/remove")
  140. @ResponseBody
  141. public ReturnT<String> remove(int id){
  142. // valid
  143. int count = xxlJobInfoDao.pageListCount(0, 10, id, -1, null, null, null);
  144. if (count > 0) {
  145. return new ReturnT<String>(500, I18nUtil.getString("jobgroup_del_limit_0") );
  146. }
  147. List<XxlJobGroup> allList = xxlJobGroupDao.findAll();
  148. if (allList.size() == 1) {
  149. return new ReturnT<String>(500, I18nUtil.getString("jobgroup_del_limit_1") );
  150. }
  151. int ret = xxlJobGroupDao.remove(id);
  152. return (ret>0)?ReturnT.SUCCESS:ReturnT.FAIL;
  153. }
  154. @RequestMapping("/loadById")
  155. @ResponseBody
  156. public ReturnT<XxlJobGroup> loadById(int id){
  157. XxlJobGroup jobGroup = xxlJobGroupDao.load(id);
  158. return jobGroup!=null?new ReturnT<XxlJobGroup>(jobGroup):new ReturnT<XxlJobGroup>(ReturnT.FAIL_CODE, null);
  159. }
  160. }