xm
2024-06-14 722af26bc6fec32bb289b1df51a9016a4935610f
提交 | 用户 | 时间
722af2 1 package com.xxl.job.admin.controller;
X 2
3 import com.xxl.job.admin.core.exception.XxlJobException;
4 import com.xxl.job.admin.core.model.XxlJobGroup;
5 import com.xxl.job.admin.core.model.XxlJobInfo;
6 import com.xxl.job.admin.core.model.XxlJobUser;
7 import com.xxl.job.admin.core.route.ExecutorRouteStrategyEnum;
8 import com.xxl.job.admin.core.scheduler.MisfireStrategyEnum;
9 import com.xxl.job.admin.core.scheduler.ScheduleTypeEnum;
10 import com.xxl.job.admin.core.thread.JobScheduleHelper;
11 import com.xxl.job.admin.core.thread.JobTriggerPoolHelper;
12 import com.xxl.job.admin.core.trigger.TriggerTypeEnum;
13 import com.xxl.job.admin.core.util.I18nUtil;
14 import com.xxl.job.admin.dao.XxlJobGroupDao;
15 import com.xxl.job.admin.service.LoginService;
16 import com.xxl.job.admin.service.XxlJobService;
17 import com.xxl.job.core.biz.model.ReturnT;
18 import com.xxl.job.core.enums.ExecutorBlockStrategyEnum;
19 import com.xxl.job.core.glue.GlueTypeEnum;
20 import com.xxl.job.core.util.DateUtil;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import org.springframework.stereotype.Controller;
24 import org.springframework.ui.Model;
25 import org.springframework.web.bind.annotation.RequestMapping;
26 import org.springframework.web.bind.annotation.RequestParam;
27 import org.springframework.web.bind.annotation.ResponseBody;
28
29 import javax.annotation.Resource;
30 import javax.servlet.http.HttpServletRequest;
31 import java.util.*;
32
33 /**
34  * index controller
35  *
36  * @author xuxueli 2015-12-19 16:13:16
37  */
38 @Controller
39 @RequestMapping("/jobinfo")
40 public class JobInfoController {
41     private static Logger logger = LoggerFactory.getLogger(JobInfoController.class);
42
43     @Resource
44     private XxlJobGroupDao xxlJobGroupDao;
45     @Resource
46     private XxlJobService xxlJobService;
47
48     @RequestMapping
49     public String index(HttpServletRequest request, Model model, @RequestParam(required = false, defaultValue = "-1") int jobGroup) {
50
51         // 枚举-字典
52         model.addAttribute("ExecutorRouteStrategyEnum", ExecutorRouteStrategyEnum.values());        // 路由策略-列表
53         model.addAttribute("GlueTypeEnum", GlueTypeEnum.values());                                // Glue类型-字典
54         model.addAttribute("ExecutorBlockStrategyEnum", ExecutorBlockStrategyEnum.values());        // 阻塞处理策略-字典
55         model.addAttribute("ScheduleTypeEnum", ScheduleTypeEnum.values());                        // 调度类型
56         model.addAttribute("MisfireStrategyEnum", MisfireStrategyEnum.values());                    // 调度过期策略
57
58         // 执行器列表
59         List<XxlJobGroup> jobGroupList_all = xxlJobGroupDao.findAll();
60
61         // filter group
62         List<XxlJobGroup> jobGroupList = filterJobGroupByRole(request, jobGroupList_all);
63         if (jobGroupList == null || jobGroupList.size() == 0) {
64             throw new XxlJobException(I18nUtil.getString("jobgroup_empty"));
65         }
66
67         model.addAttribute("JobGroupList", jobGroupList);
68         model.addAttribute("jobGroup", jobGroup);
69
70         return "jobinfo/jobinfo.index";
71     }
72
73     public static List<XxlJobGroup> filterJobGroupByRole(HttpServletRequest request, List<XxlJobGroup> jobGroupList_all) {
74         List<XxlJobGroup> jobGroupList = new ArrayList<>();
75         if (jobGroupList_all != null && jobGroupList_all.size() > 0) {
76             XxlJobUser loginUser = (XxlJobUser) request.getAttribute(LoginService.LOGIN_IDENTITY_KEY);
77             if (loginUser.getRole() == 1) {
78                 jobGroupList = jobGroupList_all;
79             } else {
80                 List<String> groupIdStrs = new ArrayList<>();
81                 if (loginUser.getPermission() != null && loginUser.getPermission().trim().length() > 0) {
82                     groupIdStrs = Arrays.asList(loginUser.getPermission().trim().split(","));
83                 }
84                 for (XxlJobGroup groupItem : jobGroupList_all) {
85                     if (groupIdStrs.contains(String.valueOf(groupItem.getId()))) {
86                         jobGroupList.add(groupItem);
87                     }
88                 }
89             }
90         }
91         return jobGroupList;
92     }
93
94     public static void validPermission(HttpServletRequest request, int jobGroup) {
95         XxlJobUser loginUser = (XxlJobUser) request.getAttribute(LoginService.LOGIN_IDENTITY_KEY);
96         if (!loginUser.validPermission(jobGroup)) {
97             throw new RuntimeException(I18nUtil.getString("system_permission_limit") + "[username=" + loginUser.getUsername() + "]");
98         }
99     }
100
101     @RequestMapping("/pageList")
102     @ResponseBody
103     public Map<String, Object> pageList(@RequestParam(required = false, defaultValue = "0") int start,
104                                         @RequestParam(required = false, defaultValue = "10") int length,
105                                         int jobGroup, int triggerStatus, String jobDesc, String executorHandler, String author) {
106
107         return xxlJobService.pageList(start, length, jobGroup, triggerStatus, jobDesc, executorHandler, author);
108     }
109
110     @RequestMapping("/add")
111     @ResponseBody
112     public ReturnT<String> add(XxlJobInfo jobInfo) {
113         return xxlJobService.add(jobInfo);
114     }
115
116     @RequestMapping("/update")
117     @ResponseBody
118     public ReturnT<String> update(XxlJobInfo jobInfo) {
119         return xxlJobService.update(jobInfo);
120     }
121
122     @RequestMapping("/remove")
123     @ResponseBody
124     public ReturnT<String> remove(int id) {
125         return xxlJobService.remove(id);
126     }
127
128     @RequestMapping("/stop")
129     @ResponseBody
130     public ReturnT<String> pause(int id) {
131         return xxlJobService.stop(id);
132     }
133
134     @RequestMapping("/start")
135     @ResponseBody
136     public ReturnT<String> start(int id) {
137         return xxlJobService.start(id);
138     }
139
140     @RequestMapping("/trigger")
141     @ResponseBody
142     //@PermissionLimit(limit = false)
143     public ReturnT<String> triggerJob(int id, String executorParam, String addressList) {
144         // force cover job param
145         if (executorParam == null) {
146             executorParam = "";
147         }
148
149         JobTriggerPoolHelper.trigger(id, TriggerTypeEnum.MANUAL, -1, null, executorParam, addressList);
150         return ReturnT.SUCCESS;
151     }
152
153     @RequestMapping("/nextTriggerTime")
154     @ResponseBody
155     public ReturnT<List<String>> nextTriggerTime(String scheduleType, String scheduleConf) {
156
157         XxlJobInfo paramXxlJobInfo = new XxlJobInfo();
158         paramXxlJobInfo.setScheduleType(scheduleType);
159         paramXxlJobInfo.setScheduleConf(scheduleConf);
160
161         List<String> result = new ArrayList<>();
162         try {
163             Date lastTime = new Date();
164             for (int i = 0; i < 5; i++) {
165                 lastTime = JobScheduleHelper.generateNextValidTime(paramXxlJobInfo, lastTime);
166                 if (lastTime != null) {
167                     result.add(DateUtil.formatDateTime(lastTime));
168                 } else {
169                     break;
170                 }
171             }
172         } catch (Exception e) {
173             logger.error(e.getMessage(), e);
174             return new ReturnT<List<String>>(ReturnT.FAIL_CODE, (I18nUtil.getString("schedule_type") + I18nUtil.getString("system_unvalid")) + e.getMessage());
175         }
176         return new ReturnT<List<String>>(result);
177
178     }
179
180 }