JobGroupController.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package com.xxl.job.admin.controller;
  2. import com.xxl.job.admin.core.model.ReturnT;
  3. import com.xxl.job.admin.core.model.XxlJobGroup;
  4. import com.xxl.job.admin.core.thread.JobRegistryHelper;
  5. import com.xxl.job.admin.dao.IXxlJobGroupDao;
  6. import com.xxl.job.admin.dao.IXxlJobInfoDao;
  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.List;
  16. /**
  17. * job group controller
  18. * @author xuxueli 2016-10-02 20:52:56
  19. */
  20. @Controller
  21. @RequestMapping("/jobgroup")
  22. public class JobGroupController {
  23. @Resource
  24. public IXxlJobInfoDao xxlJobInfoDao;
  25. @Resource
  26. public IXxlJobGroupDao xxlJobGroupDao;
  27. @RequestMapping
  28. public String index(Model model) {
  29. List<XxlJobGroup> list = xxlJobGroupDao.findAll();
  30. if (CollectionUtils.isNotEmpty(list)) {
  31. for (XxlJobGroup group: list) {
  32. List<String> registryList = JobRegistryHelper.discover(RegistHelper.RegistType.EXECUTOR.name(), group.getAppName());
  33. group.setRegistryList(registryList);
  34. }
  35. }
  36. model.addAttribute("list", list);
  37. return "jobgroup/jobgroup.index";
  38. }
  39. @RequestMapping("/save")
  40. @ResponseBody
  41. public ReturnT<String> save(XxlJobGroup xxlJobGroup){
  42. // valid
  43. if (xxlJobGroup.getAppName()==null || StringUtils.isBlank(xxlJobGroup.getAppName())) {
  44. return new ReturnT<String>(500, "请输入AppName");
  45. }
  46. if (xxlJobGroup.getAppName().length()>64) {
  47. return new ReturnT<String>(500, "AppName长度限制为4~64");
  48. }
  49. if (xxlJobGroup.getTitle()==null || StringUtils.isBlank(xxlJobGroup.getTitle())) {
  50. return new ReturnT<String>(500, "请输入名称");
  51. }
  52. int ret = xxlJobGroupDao.save(xxlJobGroup);
  53. return (ret>0)?ReturnT.SUCCESS:ReturnT.FAIL;
  54. }
  55. @RequestMapping("/update")
  56. @ResponseBody
  57. public ReturnT<String> update(XxlJobGroup xxlJobGroup){
  58. // valid
  59. if (xxlJobGroup.getAppName()==null || StringUtils.isBlank(xxlJobGroup.getAppName())) {
  60. return new ReturnT<String>(500, "请输入AppName");
  61. }
  62. if (xxlJobGroup.getAppName().length()>64) {
  63. return new ReturnT<String>(500, "AppName长度限制为4~64");
  64. }
  65. if (xxlJobGroup.getTitle()==null || StringUtils.isBlank(xxlJobGroup.getTitle())) {
  66. return new ReturnT<String>(500, "请输入名称");
  67. }
  68. int ret = xxlJobGroupDao.update(xxlJobGroup);
  69. return (ret>0)?ReturnT.SUCCESS:ReturnT.FAIL;
  70. }
  71. @RequestMapping("/remove")
  72. @ResponseBody
  73. public ReturnT<String> remove(int id){
  74. // valid
  75. int count = xxlJobInfoDao.pageListCount(0, 10, String.valueOf(id), null);
  76. if (count > 0) {
  77. return new ReturnT<String>(500, "该分组使用中, 不可删除");
  78. }
  79. List<XxlJobGroup> allList = xxlJobGroupDao.findAll();
  80. if (allList.size() == 1) {
  81. return new ReturnT<String>(500, "删除失败, 系统需要至少预留一个默认分组");
  82. }
  83. int ret = xxlJobGroupDao.remove(id);
  84. return (ret>0)?ReturnT.SUCCESS:ReturnT.FAIL;
  85. }
  86. }