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