JobGroupController.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package com.xxl.job.admin.controller;
  2. import com.xxl.job.admin.core.model.XxlJobGroup;
  3. import com.xxl.job.admin.core.thread.JobRegistryHelper;
  4. import com.xxl.job.admin.dao.IXxlJobGroupDao;
  5. import com.xxl.job.admin.dao.IXxlJobInfoDao;
  6. import com.xxl.job.core.biz.model.ReturnT;
  7. import com.xxl.job.core.registry.RegistHelper;
  8. import org.apache.commons.collections.CollectionUtils;
  9. import org.apache.commons.lang.StringUtils;
  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.Arrays;
  16. import java.util.List;
  17. /**
  18. * job group controller
  19. * @author xuxueli 2016-10-02 20:52:56
  20. */
  21. @Controller
  22. @RequestMapping("/jobgroup")
  23. public class JobGroupController {
  24. @Resource
  25. public IXxlJobInfoDao xxlJobInfoDao;
  26. @Resource
  27. public IXxlJobGroupDao xxlJobGroupDao;
  28. @RequestMapping
  29. public String index(Model model) {
  30. // job admin
  31. List<String> adminAddressList = JobRegistryHelper.discover(RegistHelper.RegistType.ADMIN.name(), RegistHelper.RegistType.ADMIN.name());
  32. // job group (executor)
  33. List<XxlJobGroup> list = xxlJobGroupDao.findAll();
  34. if (CollectionUtils.isNotEmpty(list)) {
  35. for (XxlJobGroup group: list) {
  36. List<String> registryList = null;
  37. if (group.getAddressType() == 0) {
  38. registryList = JobRegistryHelper.discover(RegistHelper.RegistType.EXECUTOR.name(), group.getAppName());
  39. } else {
  40. if (StringUtils.isNotBlank(group.getAddressList())) {
  41. registryList = Arrays.asList(group.getAddressList().split(","));
  42. }
  43. }
  44. group.setRegistryList(registryList);
  45. }
  46. }
  47. model.addAttribute("adminAddressList", adminAddressList);
  48. model.addAttribute("list", list);
  49. return "jobgroup/jobgroup.index";
  50. }
  51. @RequestMapping("/save")
  52. @ResponseBody
  53. public ReturnT<String> save(XxlJobGroup xxlJobGroup){
  54. // valid
  55. if (xxlJobGroup.getAppName()==null || StringUtils.isBlank(xxlJobGroup.getAppName())) {
  56. return new ReturnT<String>(500, "请输入AppName");
  57. }
  58. if (xxlJobGroup.getAppName().length()>64) {
  59. return new ReturnT<String>(500, "AppName长度限制为4~64");
  60. }
  61. if (xxlJobGroup.getTitle()==null || StringUtils.isBlank(xxlJobGroup.getTitle())) {
  62. return new ReturnT<String>(500, "请输入名称");
  63. }
  64. if (xxlJobGroup.getAddressType()!=0) {
  65. if (StringUtils.isBlank(xxlJobGroup.getAddressList())) {
  66. return new ReturnT<String>(500, "手动录入注册方式,机器地址不可为空");
  67. }
  68. String[] addresss = xxlJobGroup.getAddressList().split(",");
  69. for (String item: addresss) {
  70. if (StringUtils.isBlank(item)) {
  71. return new ReturnT<String>(500, "机器地址非法");
  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 || StringUtils.isBlank(xxlJobGroup.getAppName())) {
  83. return new ReturnT<String>(500, "请输入AppName");
  84. }
  85. if (xxlJobGroup.getAppName().length()>64) {
  86. return new ReturnT<String>(500, "AppName长度限制为4~64");
  87. }
  88. if (xxlJobGroup.getTitle()==null || StringUtils.isBlank(xxlJobGroup.getTitle())) {
  89. return new ReturnT<String>(500, "请输入名称");
  90. }
  91. if (xxlJobGroup.getAddressType()!=0) {
  92. if (StringUtils.isBlank(xxlJobGroup.getAddressList())) {
  93. return new ReturnT<String>(500, "手动录入注册方式,机器地址不可为空");
  94. }
  95. String[] addresss = xxlJobGroup.getAddressList().split(",");
  96. for (String item: addresss) {
  97. if (StringUtils.isBlank(item)) {
  98. return new ReturnT<String>(500, "机器地址非法");
  99. }
  100. }
  101. }
  102. int ret = xxlJobGroupDao.update(xxlJobGroup);
  103. return (ret>0)?ReturnT.SUCCESS:ReturnT.FAIL;
  104. }
  105. @RequestMapping("/remove")
  106. @ResponseBody
  107. public ReturnT<String> remove(int id){
  108. // valid
  109. int count = xxlJobInfoDao.pageListCount(0, 10, id, null);
  110. if (count > 0) {
  111. return new ReturnT<String>(500, "该分组使用中, 不可删除");
  112. }
  113. List<XxlJobGroup> allList = xxlJobGroupDao.findAll();
  114. if (allList.size() == 1) {
  115. return new ReturnT<String>(500, "删除失败, 系统需要至少预留一个默认分组");
  116. }
  117. int ret = xxlJobGroupDao.remove(id);
  118. return (ret>0)?ReturnT.SUCCESS:ReturnT.FAIL;
  119. }
  120. }