houzhongjian
2024-11-07 a874b928e16320839315b9abcdf2cece1229a424
提交 | 用户 | 时间
e7c126 1 package com.xxl.job.admin.controller;
H 2
3 import com.xxl.job.admin.controller.annotation.PermissionLimit;
4 import com.xxl.job.admin.core.model.XxlJobGroup;
5 import com.xxl.job.admin.core.model.XxlJobRegistry;
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.admin.dao.XxlJobRegistryDao;
10 import com.xxl.job.core.biz.model.ReturnT;
11 import com.xxl.job.core.enums.RegistryConfig;
12 import org.springframework.stereotype.Controller;
13 import org.springframework.ui.Model;
14 import org.springframework.web.bind.annotation.RequestMapping;
15 import org.springframework.web.bind.annotation.RequestParam;
16 import org.springframework.web.bind.annotation.ResponseBody;
17
18 import javax.annotation.Resource;
19 import javax.servlet.http.HttpServletRequest;
20 import java.util.*;
21
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
30     @Resource
31     public XxlJobInfoDao xxlJobInfoDao;
32     @Resource
33     public XxlJobGroupDao xxlJobGroupDao;
34     @Resource
35     private XxlJobRegistryDao xxlJobRegistryDao;
36
37     @RequestMapping
38     @PermissionLimit(adminuser = true)
39     public String index(Model model) {
40         return "jobgroup/jobgroup.index";
41     }
42
43     @RequestMapping("/pageList")
44     @ResponseBody
45     @PermissionLimit(adminuser = true)
46     public Map<String, Object> pageList(HttpServletRequest request,
47                                         @RequestParam(required = false, defaultValue = "0") int start,
48                                         @RequestParam(required = false, defaultValue = "10") int length,
49                                         String appname, String title) {
50
51         // page query
52         List<XxlJobGroup> list = xxlJobGroupDao.pageList(start, length, appname, title);
53         int list_count = xxlJobGroupDao.pageListCount(start, length, appname, title);
54
55         // package result
56         Map<String, Object> maps = new HashMap<String, Object>();
57         maps.put("recordsTotal", list_count);        // 总记录数
58         maps.put("recordsFiltered", list_count);    // 过滤后的总记录数
59         maps.put("data", list);                      // 分页列表
60         return maps;
61     }
62
63     @RequestMapping("/save")
64     @ResponseBody
65     @PermissionLimit(adminuser = true)
66     public ReturnT<String> save(XxlJobGroup xxlJobGroup){
67
68         // valid
69         if (xxlJobGroup.getAppname()==null || xxlJobGroup.getAppname().trim().length()==0) {
70             return new ReturnT<String>(500, (I18nUtil.getString("system_please_input")+"AppName") );
71         }
72         if (xxlJobGroup.getAppname().length()<4 || xxlJobGroup.getAppname().length()>64) {
73             return new ReturnT<String>(500, I18nUtil.getString("jobgroup_field_appname_length") );
74         }
75         if (xxlJobGroup.getAppname().contains(">") || xxlJobGroup.getAppname().contains("<")) {
76             return new ReturnT<String>(500, "AppName"+I18nUtil.getString("system_unvalid") );
77         }
78         if (xxlJobGroup.getTitle()==null || xxlJobGroup.getTitle().trim().length()==0) {
79             return new ReturnT<String>(500, (I18nUtil.getString("system_please_input") + I18nUtil.getString("jobgroup_field_title")) );
80         }
81         if (xxlJobGroup.getTitle().contains(">") || xxlJobGroup.getTitle().contains("<")) {
82             return new ReturnT<String>(500, I18nUtil.getString("jobgroup_field_title")+I18nUtil.getString("system_unvalid") );
83         }
84         if (xxlJobGroup.getAddressType()!=0) {
85             if (xxlJobGroup.getAddressList()==null || xxlJobGroup.getAddressList().trim().length()==0) {
86                 return new ReturnT<String>(500, I18nUtil.getString("jobgroup_field_addressType_limit") );
87             }
88             if (xxlJobGroup.getAddressList().contains(">") || xxlJobGroup.getAddressList().contains("<")) {
89                 return new ReturnT<String>(500, I18nUtil.getString("jobgroup_field_registryList")+I18nUtil.getString("system_unvalid") );
90             }
91
92             String[] addresss = xxlJobGroup.getAddressList().split(",");
93             for (String item: addresss) {
94                 if (item==null || item.trim().length()==0) {
95                     return new ReturnT<String>(500, I18nUtil.getString("jobgroup_field_registryList_unvalid") );
96                 }
97             }
98         }
99
100         // process
101         xxlJobGroup.setUpdateTime(new Date());
102
103         int ret = xxlJobGroupDao.save(xxlJobGroup);
104         return (ret>0)?ReturnT.SUCCESS:ReturnT.FAIL;
105     }
106
107     @RequestMapping("/update")
108     @ResponseBody
109     @PermissionLimit(adminuser = true)
110     public ReturnT<String> update(XxlJobGroup xxlJobGroup){
111         // valid
112         if (xxlJobGroup.getAppname()==null || xxlJobGroup.getAppname().trim().length()==0) {
113             return new ReturnT<String>(500, (I18nUtil.getString("system_please_input")+"AppName") );
114         }
115         if (xxlJobGroup.getAppname().length()<4 || xxlJobGroup.getAppname().length()>64) {
116             return new ReturnT<String>(500, I18nUtil.getString("jobgroup_field_appname_length") );
117         }
118         if (xxlJobGroup.getTitle()==null || xxlJobGroup.getTitle().trim().length()==0) {
119             return new ReturnT<String>(500, (I18nUtil.getString("system_please_input") + I18nUtil.getString("jobgroup_field_title")) );
120         }
121         if (xxlJobGroup.getAddressType() == 0) {
122             // 0=自动注册
123             List<String> registryList = findRegistryByAppName(xxlJobGroup.getAppname());
124             String addressListStr = null;
125             if (registryList!=null && !registryList.isEmpty()) {
126                 Collections.sort(registryList);
127                 addressListStr = "";
128                 for (String item:registryList) {
129                     addressListStr += item + ",";
130                 }
131                 addressListStr = addressListStr.substring(0, addressListStr.length()-1);
132             }
133             xxlJobGroup.setAddressList(addressListStr);
134         } else {
135             // 1=手动录入
136             if (xxlJobGroup.getAddressList()==null || xxlJobGroup.getAddressList().trim().length()==0) {
137                 return new ReturnT<String>(500, I18nUtil.getString("jobgroup_field_addressType_limit") );
138             }
139             String[] addresss = xxlJobGroup.getAddressList().split(",");
140             for (String item: addresss) {
141                 if (item==null || item.trim().length()==0) {
142                     return new ReturnT<String>(500, I18nUtil.getString("jobgroup_field_registryList_unvalid") );
143                 }
144             }
145         }
146
147         // process
148         xxlJobGroup.setUpdateTime(new Date());
149
150         int ret = xxlJobGroupDao.update(xxlJobGroup);
151         return (ret>0)?ReturnT.SUCCESS:ReturnT.FAIL;
152     }
153
154     private List<String> findRegistryByAppName(String appnameParam){
155         HashMap<String, List<String>> appAddressMap = new HashMap<String, List<String>>();
156         List<XxlJobRegistry> list = xxlJobRegistryDao.findAll(RegistryConfig.DEAD_TIMEOUT, new Date());
157         if (list != null) {
158             for (XxlJobRegistry item: list) {
159                 if (RegistryConfig.RegistType.EXECUTOR.name().equals(item.getRegistryGroup())) {
160                     String appname = item.getRegistryKey();
161                     List<String> registryList = appAddressMap.get(appname);
162                     if (registryList == null) {
163                         registryList = new ArrayList<String>();
164                     }
165
166                     if (!registryList.contains(item.getRegistryValue())) {
167                         registryList.add(item.getRegistryValue());
168                     }
169                     appAddressMap.put(appname, registryList);
170                 }
171             }
172         }
173         return appAddressMap.get(appnameParam);
174     }
175
176     @RequestMapping("/remove")
177     @ResponseBody
178     @PermissionLimit(adminuser = true)
179     public ReturnT<String> remove(int id){
180
181         // valid
182         int count = xxlJobInfoDao.pageListCount(0, 10, id, -1,  null, null, null);
183         if (count > 0) {
184             return new ReturnT<String>(500, I18nUtil.getString("jobgroup_del_limit_0") );
185         }
186
187         List<XxlJobGroup> allList = xxlJobGroupDao.findAll();
188         if (allList.size() == 1) {
189             return new ReturnT<String>(500, I18nUtil.getString("jobgroup_del_limit_1") );
190         }
191
192         int ret = xxlJobGroupDao.remove(id);
193         return (ret>0)?ReturnT.SUCCESS:ReturnT.FAIL;
194     }
195
196     @RequestMapping("/loadById")
197     @ResponseBody
198     @PermissionLimit(adminuser = true)
199     public ReturnT<XxlJobGroup> loadById(int id){
200         XxlJobGroup jobGroup = xxlJobGroupDao.load(id);
201         return jobGroup!=null?new ReturnT<XxlJobGroup>(jobGroup):new ReturnT<XxlJobGroup>(ReturnT.FAIL_CODE, null);
202     }
203
204 }