Showing
10 changed files
with
114 additions
and
227 deletions
@@ -27,7 +27,7 @@ public class TestSchedule { | @@ -27,7 +27,7 @@ public class TestSchedule { | ||
27 | int i = 0; | 27 | int i = 0; |
28 | while (!future.isDone()) { | 28 | while (!future.isDone()) { |
29 | try { | 29 | try { |
30 | - if (i < 70) { | 30 | + if (i < 7) { |
31 | Thread.currentThread().sleep(1000l); | 31 | Thread.currentThread().sleep(1000l); |
32 | i++; | 32 | i++; |
33 | } else { | 33 | } else { |
@@ -12,6 +12,7 @@ import org.springframework.web.bind.annotation.RequestBody; | @@ -12,6 +12,7 @@ import org.springframework.web.bind.annotation.RequestBody; | ||
12 | import org.springframework.web.bind.annotation.RequestMapping; | 12 | import org.springframework.web.bind.annotation.RequestMapping; |
13 | import org.springframework.web.bind.annotation.RestController; | 13 | import org.springframework.web.bind.annotation.RestController; |
14 | 14 | ||
15 | +import com.yoho.jobs.server.domain.ConstantEnum; | ||
15 | import com.yoho.jobs.server.domain.JobInfo; | 16 | import com.yoho.jobs.server.domain.JobInfo; |
16 | import com.yoho.jobs.server.domain.JobResponse; | 17 | import com.yoho.jobs.server.domain.JobResponse; |
17 | import com.yoho.jobs.server.domain.ShowAllJobResponse; | 18 | import com.yoho.jobs.server.domain.ShowAllJobResponse; |
@@ -31,11 +32,9 @@ public class JobServiceController { | @@ -31,11 +32,9 @@ public class JobServiceController { | ||
31 | JobResponse response = new JobResponse(); | 32 | JobResponse response = new JobResponse(); |
32 | try { | 33 | try { |
33 | jobService.createJob(jobInfo); | 34 | jobService.createJob(jobInfo); |
34 | - response.setCode("0"); | ||
35 | - response.setDesc("success"); | 35 | + buildJobResponse(ConstantEnum.JobResponse.SUCCESS, ConstantEnum.JobResponse.SUCCESS_DESC, response); |
36 | } catch (Exception e) { | 36 | } catch (Exception e) { |
37 | - response.setCode("1"); | ||
38 | - response.setDesc(e.getLocalizedMessage()); | 37 | + buildJobResponse(ConstantEnum.JobResponse.FAIL, e.getMessage(), response); |
39 | } | 38 | } |
40 | return response; | 39 | return response; |
41 | } | 40 | } |
@@ -45,11 +44,9 @@ public class JobServiceController { | @@ -45,11 +44,9 @@ public class JobServiceController { | ||
45 | JobResponse response = new JobResponse(); | 44 | JobResponse response = new JobResponse(); |
46 | try { | 45 | try { |
47 | jobService.stopJob(jobInfo); | 46 | jobService.stopJob(jobInfo); |
48 | - response.setCode("0"); | ||
49 | - response.setDesc("success"); | 47 | + buildJobResponse(ConstantEnum.JobResponse.SUCCESS, ConstantEnum.JobResponse.SUCCESS_DESC, response); |
50 | } catch (Exception e) { | 48 | } catch (Exception e) { |
51 | - response.setCode("1"); | ||
52 | - response.setDesc(e.getLocalizedMessage()); | 49 | + buildJobResponse(ConstantEnum.JobResponse.FAIL, e.getMessage(), response); |
53 | } | 50 | } |
54 | return response; | 51 | return response; |
55 | } | 52 | } |
@@ -59,11 +56,9 @@ public class JobServiceController { | @@ -59,11 +56,9 @@ public class JobServiceController { | ||
59 | JobResponse response = new JobResponse(); | 56 | JobResponse response = new JobResponse(); |
60 | try { | 57 | try { |
61 | jobService.startJob(jobInfo); | 58 | jobService.startJob(jobInfo); |
62 | - response.setCode("0"); | ||
63 | - response.setDesc("success"); | 59 | + buildJobResponse(ConstantEnum.JobResponse.SUCCESS, ConstantEnum.JobResponse.SUCCESS_DESC, response); |
64 | } catch (Exception e) { | 60 | } catch (Exception e) { |
65 | - response.setCode("1"); | ||
66 | - response.setDesc(e.getLocalizedMessage()); | 61 | + buildJobResponse(ConstantEnum.JobResponse.FAIL, e.getMessage(), response); |
67 | } | 62 | } |
68 | 63 | ||
69 | return response; | 64 | return response; |
@@ -74,11 +69,9 @@ public class JobServiceController { | @@ -74,11 +69,9 @@ public class JobServiceController { | ||
74 | JobResponse response = new JobResponse(); | 69 | JobResponse response = new JobResponse(); |
75 | try { | 70 | try { |
76 | jobService.updateJob(jobInfo); | 71 | jobService.updateJob(jobInfo); |
77 | - response.setCode("0"); | ||
78 | - response.setDesc("success"); | 72 | + buildJobResponse(ConstantEnum.JobResponse.SUCCESS, ConstantEnum.JobResponse.SUCCESS_DESC, response); |
79 | } catch (Exception e) { | 73 | } catch (Exception e) { |
80 | - response.setCode("1"); | ||
81 | - response.setDesc(e.getLocalizedMessage()); | 74 | + buildJobResponse(ConstantEnum.JobResponse.FAIL, e.getMessage(), response); |
82 | } | 75 | } |
83 | 76 | ||
84 | return response; | 77 | return response; |
@@ -94,7 +87,7 @@ public class JobServiceController { | @@ -94,7 +87,7 @@ public class JobServiceController { | ||
94 | response.setDesc("success"); | 87 | response.setDesc("success"); |
95 | } catch (Exception e) { | 88 | } catch (Exception e) { |
96 | response.setCode("1"); | 89 | response.setCode("1"); |
97 | - response.setDesc(e.getLocalizedMessage()); | 90 | + response.setDesc(e.getMessage()); |
98 | } | 91 | } |
99 | return response; | 92 | return response; |
100 | } | 93 | } |
@@ -104,13 +97,17 @@ public class JobServiceController { | @@ -104,13 +97,17 @@ public class JobServiceController { | ||
104 | JobResponse response = new JobResponse(); | 97 | JobResponse response = new JobResponse(); |
105 | try { | 98 | try { |
106 | jobService.retryJob(jobInfo); | 99 | jobService.retryJob(jobInfo); |
107 | - response.setCode("0"); | ||
108 | - response.setDesc("success"); | 100 | + buildJobResponse(ConstantEnum.JobResponse.SUCCESS, ConstantEnum.JobResponse.SUCCESS_DESC, response); |
101 | + | ||
109 | } catch (Exception e) { | 102 | } catch (Exception e) { |
110 | - response.setCode("1"); | ||
111 | - response.setDesc(e.getLocalizedMessage()); | 103 | + buildJobResponse(ConstantEnum.JobResponse.FAIL, e.getMessage(), response); |
112 | } | 104 | } |
113 | 105 | ||
114 | return response; | 106 | return response; |
115 | } | 107 | } |
108 | + | ||
109 | + private void buildJobResponse(String code, String message, JobResponse response) { | ||
110 | + response.setCode(code); | ||
111 | + response.setDesc(message); | ||
112 | + } | ||
116 | } | 113 | } |
@@ -53,7 +53,7 @@ public interface ConstantEnum { | @@ -53,7 +53,7 @@ public interface ConstantEnum { | ||
53 | } | 53 | } |
54 | 54 | ||
55 | public enum JobOperatorEnum { | 55 | public enum JobOperatorEnum { |
56 | - EXEC(0, "执行"), UPDATE(1, "更新"), STOP(2, "停止"), CREATE(3, "新建"),TRIGGER(4,"任务触发"); | 56 | + EXEC(0, "执行"), UPDATE(1, "更新"), STOP(2, "停止"), CREATE(3, "新建"), TRIGGER(4, "任务触发"); |
57 | private int value; | 57 | private int value; |
58 | private String name; | 58 | private String name; |
59 | 59 | ||
@@ -72,4 +72,13 @@ public interface ConstantEnum { | @@ -72,4 +72,13 @@ public interface ConstantEnum { | ||
72 | 72 | ||
73 | } | 73 | } |
74 | 74 | ||
75 | + public class JobResponse { | ||
76 | + public static final String SUCCESS = "0"; | ||
77 | + | ||
78 | + public static final String SUCCESS_DESC = "success"; | ||
79 | + | ||
80 | + public static final String FAIL = "1"; | ||
81 | + | ||
82 | + } | ||
83 | + | ||
75 | } | 84 | } |
@@ -5,6 +5,8 @@ package com.yoho.jobs.server.domain; | @@ -5,6 +5,8 @@ package com.yoho.jobs.server.domain; | ||
5 | 5 | ||
6 | import java.util.concurrent.ScheduledFuture; | 6 | import java.util.concurrent.ScheduledFuture; |
7 | 7 | ||
8 | +import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; | ||
9 | + | ||
8 | import lombok.Getter; | 10 | import lombok.Getter; |
9 | import lombok.Setter; | 11 | import lombok.Setter; |
10 | import lombok.ToString; | 12 | import lombok.ToString; |
@@ -22,5 +24,7 @@ public class ScheduleJobInfo { | @@ -22,5 +24,7 @@ public class ScheduleJobInfo { | ||
22 | 24 | ||
23 | // 定时任务返回对象 | 25 | // 定时任务返回对象 |
24 | private ScheduledFuture<?> future; | 26 | private ScheduledFuture<?> future; |
27 | + | ||
28 | + ThreadPoolTaskScheduler scheduler; | ||
25 | 29 | ||
26 | } | 30 | } |
@@ -63,10 +63,10 @@ public class ElectionService { | @@ -63,10 +63,10 @@ public class ElectionService { | ||
63 | 63 | ||
64 | Container.CONTAINER.putAllClient(jobService.getAllClient()); | 64 | Container.CONTAINER.putAllClient(jobService.getAllClient()); |
65 | // 监听客户端 | 65 | // 监听客户端 |
66 | -// listenClientNode(jobsInfo); | 66 | + // listenClientNode(jobsInfo); |
67 | listenClient(); | 67 | listenClient(); |
68 | // 调度开始,只有在触发调度的时候再选取执行节点 | 68 | // 调度开始,只有在触发调度的时候再选取执行节点 |
69 | - jobScheduleService.batchSchedule(Container.CONTAINER.getAllJobs()); | 69 | + // jobScheduleService.batchSchedule(Container.CONTAINER.getAllJobs()); |
70 | 70 | ||
71 | count.await(); | 71 | count.await(); |
72 | } | 72 | } |
@@ -91,122 +91,6 @@ public class ElectionService { | @@ -91,122 +91,6 @@ public class ElectionService { | ||
91 | 91 | ||
92 | } | 92 | } |
93 | 93 | ||
94 | - private void listenClientNode(Map<String, List<JobInfo>> jobsInfo) throws Exception { | ||
95 | - for (Map.Entry<String, List<JobInfo>> entry : jobsInfo.entrySet()) { | ||
96 | - buildWatch(entry.getKey()); | ||
97 | - watchJobInfo(entry.getKey()); | ||
98 | - } | ||
99 | - | ||
100 | - } | ||
101 | - | ||
102 | - private void buildWatch(String module) throws Exception { | ||
103 | - TreeCache treeCache = new TreeCache(client, CLIENT_NODES + module + "/clientnode"); | ||
104 | - treeCache.getListenable().addListener(new TreeCacheListener() { | ||
105 | - @Override | ||
106 | - public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception { | ||
107 | - ChildData data = event.getData(); | ||
108 | - if (data != null) { | ||
109 | - String path = data.getPath(); | ||
110 | - String clientIp = path.substring(path.lastIndexOf("/") + 1); | ||
111 | - switch (event.getType()) { | ||
112 | - case NODE_ADDED: | ||
113 | - logger.info("register new client, the client is {}", String.valueOf(data.getData())); | ||
114 | - Container.CONTAINER.addClient(module, clientIp); | ||
115 | - break; | ||
116 | - case NODE_REMOVED: | ||
117 | - // 有客户端下线,需要将下线的客户端上执行的任务切换到其他节点上 | ||
118 | - logger.info("remove node {}", data.getPath()); | ||
119 | - // String path = data.getPath(); | ||
120 | - // String clientIp = path.substring(path.lastIndexOf("/") + 1); | ||
121 | - Container.CONTAINER.removeClient(module, clientIp); | ||
122 | - // 重新选取节点 | ||
123 | - break; | ||
124 | - case NODE_UPDATED: | ||
125 | - break; | ||
126 | - | ||
127 | - default: | ||
128 | - break; | ||
129 | - } | ||
130 | - } else { | ||
131 | - logger.info("data is null : " + event.getType()); | ||
132 | - } | ||
133 | - } | ||
134 | - }); | ||
135 | - // 开始监听 | ||
136 | - treeCache.start(); | ||
137 | - } | ||
138 | - | ||
139 | - private void watchJobInfo(String module) throws Exception { | ||
140 | - TreeCache treeCache = new TreeCache(client, CLIENT_NODES + module + "/jobInfo"); | ||
141 | - treeCache.getListenable().addListener(new TreeCacheListener() { | ||
142 | - @Override | ||
143 | - public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception { | ||
144 | - ChildData data = event.getData(); | ||
145 | - if (data != null) { | ||
146 | - JobInfo jobInfo = JSON.parseObject(data.getData(), JobInfo.class); | ||
147 | - switch (event.getType()) { | ||
148 | - // TODO 若先启动服务端,后启动客户端,会不会有问题---有问题 | ||
149 | - case NODE_ADDED: | ||
150 | - logger.info("create new Job, the Job is {}", String.valueOf(data.getData())); | ||
151 | - ScheduleJobInfo scheduleJobInfo = new ScheduleJobInfo(); | ||
152 | - scheduleJobInfo.setJobInfo(jobInfo); | ||
153 | - scheduleJobInfo.setFuture(null); | ||
154 | - Container.CONTAINER.addJobInfo(jobInfo.getModule(), scheduleJobInfo); | ||
155 | - // if (jobInfo.getOperator() == ConstantEnum.JobOperatorEnum.EXEC.getValue()) { | ||
156 | - jobInfo.setOperator(ConstantEnum.JobOperatorEnum.TRIGGER.getValue()); | ||
157 | - jobInfo.setStatus(ConstantEnum.JobStatusEnum.STARTING.getValue()); | ||
158 | - jobScheduleService.schedule(scheduleJobInfo); | ||
159 | - // } | ||
160 | - break; | ||
161 | - case NODE_REMOVED: | ||
162 | - | ||
163 | - break; | ||
164 | - case NODE_UPDATED: | ||
165 | - logger.info("update Job, the Job is {}", String.valueOf(data.getData())); | ||
166 | - // 如果是任务的启动则需要schedule | ||
167 | - ScheduleJobInfo updateScheduleJobInfo = Container.CONTAINER.getJobsByModule(jobInfo.getModule()) | ||
168 | - .get(jobInfo.getJobName()); | ||
169 | - if (jobInfo.getOperator() == ConstantEnum.JobOperatorEnum.EXEC.getValue()) { | ||
170 | - jobInfo.setOperator(ConstantEnum.JobOperatorEnum.TRIGGER.getValue()); | ||
171 | - jobInfo.setStatus(ConstantEnum.JobStatusEnum.STARTING.getValue()); | ||
172 | - | ||
173 | - updateScheduleJobInfo.setJobInfo(jobInfo); | ||
174 | - updateScheduleJobInfo.setFuture(null); | ||
175 | - jobScheduleService.schedule(updateScheduleJobInfo); | ||
176 | - } else if (jobInfo.getOperator() == ConstantEnum.JobOperatorEnum.STOP.getValue()) { | ||
177 | - // 当前任务是执行状态时,才可以停止,然后cancel之 | ||
178 | - if (jobInfo.getStatus() == ConstantEnum.JobStatusEnum.STARTING.getValue()) { | ||
179 | - jobInfo.setStatus(ConstantEnum.JobStatusEnum.STOPPING.getValue()); | ||
180 | - updateScheduleJobInfo.setJobInfo(jobInfo); | ||
181 | - updateScheduleJobInfo.getFuture().cancel(false); | ||
182 | - // 重新更新到zk上 | ||
183 | - String jobPath = CLIENT_NODES + jobInfo.getModule() + "/jobinfo/" | ||
184 | - + jobInfo.getJobName(); | ||
185 | - try { | ||
186 | - client.setData().forPath(jobPath, | ||
187 | - JSON.toJSONString(jobInfo).getBytes(Charset.forName("UTF-8"))); | ||
188 | - } catch (Exception e) { | ||
189 | - logger.error("stop job faild , job is {}, exception {}", jobInfo, e); | ||
190 | - } | ||
191 | - } | ||
192 | - } else if (jobInfo.getOperator() == ConstantEnum.JobOperatorEnum.UPDATE.getValue()) { | ||
193 | - updateScheduleJobInfo.setJobInfo(jobInfo); | ||
194 | - } | ||
195 | - | ||
196 | - break; | ||
197 | - | ||
198 | - default: | ||
199 | - break; | ||
200 | - } | ||
201 | - } else { | ||
202 | - logger.info("data is null : " + event.getType()); | ||
203 | - } | ||
204 | - } | ||
205 | - }); | ||
206 | - // 开始监听 | ||
207 | - treeCache.start(); | ||
208 | - } | ||
209 | - | ||
210 | private void buildScheduleJobInfoContainer(Map<String, List<JobInfo>> jobsInfo) { | 94 | private void buildScheduleJobInfoContainer(Map<String, List<JobInfo>> jobsInfo) { |
211 | for (Map.Entry<String, List<JobInfo>> entry : jobsInfo.entrySet()) { | 95 | for (Map.Entry<String, List<JobInfo>> entry : jobsInfo.entrySet()) { |
212 | String moduleName = entry.getKey(); | 96 | String moduleName = entry.getKey(); |
@@ -250,17 +134,18 @@ public class ElectionService { | @@ -250,17 +134,18 @@ public class ElectionService { | ||
250 | scheduleJobInfo.setJobInfo(jobInfo); | 134 | scheduleJobInfo.setJobInfo(jobInfo); |
251 | scheduleJobInfo.setFuture(null); | 135 | scheduleJobInfo.setFuture(null); |
252 | Container.CONTAINER.addJobInfo(jobInfo.getModule(), scheduleJobInfo); | 136 | Container.CONTAINER.addJobInfo(jobInfo.getModule(), scheduleJobInfo); |
253 | - jobInfo.setOperator(ConstantEnum.JobOperatorEnum.TRIGGER.getValue()); | ||
254 | - jobInfo.setStatus(ConstantEnum.JobStatusEnum.STARTING.getValue()); | ||
255 | - jobScheduleService.schedule(scheduleJobInfo); | 137 | + if (jobInfo.getOperator() == ConstantEnum.JobOperatorEnum.EXEC.getValue() |
138 | + || jobInfo.getOperator() == ConstantEnum.JobOperatorEnum.TRIGGER.getValue()) { | ||
139 | + jobInfo.setOperator(ConstantEnum.JobOperatorEnum.TRIGGER.getValue()); | ||
140 | + jobInfo.setStatus(ConstantEnum.JobStatusEnum.STARTING.getValue()); | ||
141 | + jobScheduleService.schedule(scheduleJobInfo); | ||
142 | + } | ||
256 | } | 143 | } |
257 | break; | 144 | break; |
258 | case NODE_REMOVED: | 145 | case NODE_REMOVED: |
259 | if (isClientInfoType(pathType)) { | 146 | if (isClientInfoType(pathType)) { |
260 | // 有客户端下线,需要将下线的客户端上执行的任务切换到其他节点上 | 147 | // 有客户端下线,需要将下线的客户端上执行的任务切换到其他节点上 |
261 | logger.info("remove node {}", data.getPath()); | 148 | logger.info("remove node {}", data.getPath()); |
262 | - // String path = data.getPath(); | ||
263 | - // String clientIp = path.substring(path.lastIndexOf("/") + 1); | ||
264 | Container.CONTAINER.removeClient(parseModule(path), | 149 | Container.CONTAINER.removeClient(parseModule(path), |
265 | path.substring(path.lastIndexOf("/") + 1)); | 150 | path.substring(path.lastIndexOf("/") + 1)); |
266 | // 重新选取节点 | 151 | // 重新选取节点 |
@@ -289,7 +174,7 @@ public class ElectionService { | @@ -289,7 +174,7 @@ public class ElectionService { | ||
289 | updateScheduleJobInfo.setJobInfo(jobInfo); | 174 | updateScheduleJobInfo.setJobInfo(jobInfo); |
290 | updateScheduleJobInfo.getFuture().cancel(false); | 175 | updateScheduleJobInfo.getFuture().cancel(false); |
291 | // 重新更新到zk上 | 176 | // 重新更新到zk上 |
292 | - String jobPath = CLIENT_NODES + jobInfo.getModule() + "/jobinfo/" | 177 | + String jobPath = CLIENT_NODES + "/" + jobInfo.getModule() + "/jobinfo/" |
293 | + jobInfo.getJobName(); | 178 | + jobInfo.getJobName(); |
294 | try { | 179 | try { |
295 | client.setData().forPath(jobPath, | 180 | client.setData().forPath(jobPath, |
@@ -332,7 +217,7 @@ public class ElectionService { | @@ -332,7 +217,7 @@ public class ElectionService { | ||
332 | } | 217 | } |
333 | 218 | ||
334 | private String parseModule(String path) { | 219 | private String parseModule(String path) { |
335 | - String s = path.substring(CLIENT_NODES.length()+1); | 220 | + String s = path.substring(CLIENT_NODES.length() + 1); |
336 | String module = s.substring(0, s.indexOf("/")); | 221 | String module = s.substring(0, s.indexOf("/")); |
337 | return module; | 222 | return module; |
338 | } | 223 | } |
@@ -48,8 +48,13 @@ public class JobScheduleServiceImpl implements JobScheduleService { | @@ -48,8 +48,13 @@ public class JobScheduleServiceImpl implements JobScheduleService { | ||
48 | if (job.getJobInfo().getOperator() != ConstantEnum.JobOperatorEnum.TRIGGER.getValue()) { | 48 | if (job.getJobInfo().getOperator() != ConstantEnum.JobOperatorEnum.TRIGGER.getValue()) { |
49 | return; | 49 | return; |
50 | } | 50 | } |
51 | - ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler(); | ||
52 | - scheduler.initialize(); | 51 | + ThreadPoolTaskScheduler scheduler = job.getScheduler(); |
52 | + if( scheduler== null) { | ||
53 | + scheduler = new ThreadPoolTaskScheduler(); | ||
54 | + scheduler.initialize(); | ||
55 | + job.setScheduler(scheduler); | ||
56 | + } | ||
57 | + | ||
53 | String cronExpress = job.getJobInfo().getCronExpression(); | 58 | String cronExpress = job.getJobInfo().getCronExpression(); |
54 | // 选取执行节点 | 59 | // 选取执行节点 |
55 | if (job.getJobInfo().getJobType() == 0) { | 60 | if (job.getJobInfo().getJobType() == 0) { |
@@ -19,6 +19,8 @@ import org.slf4j.LoggerFactory; | @@ -19,6 +19,8 @@ import org.slf4j.LoggerFactory; | ||
19 | import com.alibaba.fastjson.JSON; | 19 | import com.alibaba.fastjson.JSON; |
20 | import com.yoho.jobs.server.domain.ConstantEnum; | 20 | import com.yoho.jobs.server.domain.ConstantEnum; |
21 | import com.yoho.jobs.server.domain.JobInfo; | 21 | import com.yoho.jobs.server.domain.JobInfo; |
22 | +import com.yoho.jobs.server.domain.ScheduleJobInfo; | ||
23 | +import com.yoho.jobs.server.scheduler.Container; | ||
22 | import com.yoho.jobs.server.scheduler.JobService; | 24 | import com.yoho.jobs.server.scheduler.JobService; |
23 | 25 | ||
24 | /** | 26 | /** |
@@ -132,22 +134,17 @@ public class JobServiceImpl implements JobService { | @@ -132,22 +134,17 @@ public class JobServiceImpl implements JobService { | ||
132 | if (logger.isDebugEnabled()) { | 134 | if (logger.isDebugEnabled()) { |
133 | logger.debug("begin to create job , job is {}", jobInfo); | 135 | logger.debug("begin to create job , job is {}", jobInfo); |
134 | } | 136 | } |
135 | - String jobPath = JOBINFO_PATH + "/" + jobInfo.getModule() + "/jobinfo/" + jobInfo.getJobName(); | ||
136 | - jobInfo.setOperator(ConstantEnum.JobOperatorEnum.CREATE.getValue()); | ||
137 | - jobInfo.setStatus(ConstantEnum.JobStatusEnum.STOPPING.getValue()); | ||
138 | - try { | ||
139 | - Stat stat = client.checkExists().forPath(jobPath); | ||
140 | - if (stat != null) { | ||
141 | - String message = "the job already exist in zk,plz check it ,jobinfo is " + jobInfo; | ||
142 | - logger.error("the job already exits in zk,plz check it ,jobinfo is {}", jobInfo); | ||
143 | - throw new Exception(message); | ||
144 | - } else { | ||
145 | - client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(jobPath, | ||
146 | - JSON.toJSONString(jobInfo).getBytes(Charset.forName("UTF-8"))); | ||
147 | - } | ||
148 | - } catch (Exception e) { | ||
149 | - logger.error("System error.", e); | ||
150 | - throw e; | 137 | + |
138 | + if (!checkJobExist(jobInfo)) { | ||
139 | + String message = "the job already exist in zk,plz check it ,jobinfo is " + jobInfo; | ||
140 | + logger.error("the job already exits in zk,plz check it ,jobinfo is {}", jobInfo); | ||
141 | + throw new Exception(message); | ||
142 | + } else { | ||
143 | + String jobPath = JOBINFO_PATH + "/" + jobInfo.getModule() + "/jobinfo/" + jobInfo.getJobName(); | ||
144 | + jobInfo.setOperator(ConstantEnum.JobOperatorEnum.CREATE.getValue()); | ||
145 | + jobInfo.setStatus(ConstantEnum.JobStatusEnum.STOPPING.getValue()); | ||
146 | + client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(jobPath, | ||
147 | + JSON.toJSONString(jobInfo).getBytes(Charset.forName("UTF-8"))); | ||
151 | } | 148 | } |
152 | 149 | ||
153 | } | 150 | } |
@@ -157,22 +154,22 @@ public class JobServiceImpl implements JobService { | @@ -157,22 +154,22 @@ public class JobServiceImpl implements JobService { | ||
157 | if (logger.isDebugEnabled()) { | 154 | if (logger.isDebugEnabled()) { |
158 | logger.debug("begin to startJob job , job is {}", jobInfo); | 155 | logger.debug("begin to startJob job , job is {}", jobInfo); |
159 | } | 156 | } |
160 | - String jobPath = JOBINFO_PATH + "/" + jobInfo.getModule() + "/jobinfo/" + jobInfo.getJobName(); | ||
161 | - jobInfo.setOperator(ConstantEnum.JobOperatorEnum.EXEC.getValue()); | ||
162 | - try { | ||
163 | - Stat stat = client.checkExists().forPath(jobPath); | ||
164 | - if (stat == null) { | ||
165 | - // 不存在 | ||
166 | - String message = "job not exist, job is " + jobInfo; | ||
167 | - logger.error(message); | ||
168 | - throw new Exception(message); | ||
169 | - } else { | ||
170 | - // 存在 | ||
171 | - client.setData().forPath(jobPath, JSON.toJSONString(jobInfo).getBytes(Charset.forName("UTF-8"))); | ||
172 | - } | ||
173 | - } catch (Exception e) { | ||
174 | 157 | ||
175 | - throw e; | 158 | + if (!checkJobExist(jobInfo)) { |
159 | + // 不存在 | ||
160 | + String message = "job not exist, job is " + jobInfo; | ||
161 | + logger.error(message); | ||
162 | + throw new Exception(message); | ||
163 | + } else { | ||
164 | + // 存在 | ||
165 | + String jobPath = JOBINFO_PATH + "/" + jobInfo.getModule() + "/jobinfo/" + jobInfo.getJobName(); | ||
166 | + jobInfo.setOperator(ConstantEnum.JobOperatorEnum.EXEC.getValue()); | ||
167 | + ScheduleJobInfo updateScheduleJobInfo = Container.CONTAINER.getJobsByModule(jobInfo.getModule()) | ||
168 | + .get(jobInfo.getJobName()); | ||
169 | + updateScheduleJobInfo.getJobInfo().setOperator(ConstantEnum.JobOperatorEnum.EXEC.getValue()); | ||
170 | + updateScheduleJobInfo.getJobInfo().setStatus(ConstantEnum.JobStatusEnum.STARTING.getValue()); | ||
171 | + client.setData().forPath(jobPath, | ||
172 | + JSON.toJSONString(updateScheduleJobInfo.getJobInfo()).getBytes(Charset.forName("UTF-8"))); | ||
176 | } | 173 | } |
177 | } | 174 | } |
178 | 175 | ||
@@ -184,22 +181,22 @@ public class JobServiceImpl implements JobService { | @@ -184,22 +181,22 @@ public class JobServiceImpl implements JobService { | ||
184 | if (jobInfo.getStatus() == ConstantEnum.JobStatusEnum.STOPPING.getValue()) { | 181 | if (jobInfo.getStatus() == ConstantEnum.JobStatusEnum.STOPPING.getValue()) { |
185 | return; | 182 | return; |
186 | } | 183 | } |
187 | - String jobPath = JOBINFO_PATH + "/" + jobInfo.getModule() + "/jobinfo/" + jobInfo.getJobName(); | ||
188 | - jobInfo.setOperator(ConstantEnum.JobOperatorEnum.STOP.getValue()); | ||
189 | - try { | ||
190 | - Stat stat = client.checkExists().forPath(jobPath); | ||
191 | - if (stat == null) { | ||
192 | - // 不存在 | ||
193 | - String message = "job not exist, job is " + jobInfo; | ||
194 | - logger.error(message); | ||
195 | - throw new Exception(message); | ||
196 | - } else { | ||
197 | - // 存在 | ||
198 | - client.setData().forPath(jobPath, JSON.toJSONString(jobInfo).getBytes(Charset.forName("UTF-8"))); | ||
199 | - } | ||
200 | - } catch (Exception e) { | ||
201 | 184 | ||
202 | - throw e; | 185 | + if (!checkJobExist(jobInfo)) { |
186 | + String message = "job not exist, job is " + jobInfo; | ||
187 | + logger.error(message); | ||
188 | + throw new Exception(message); | ||
189 | + } else { | ||
190 | + // 存在 | ||
191 | + String jobPath = JOBINFO_PATH + "/" + jobInfo.getModule() + "/jobinfo/" + jobInfo.getJobName(); | ||
192 | + jobInfo.setOperator(ConstantEnum.JobOperatorEnum.STOP.getValue()); | ||
193 | + ScheduleJobInfo updateScheduleJobInfo = Container.CONTAINER.getJobsByModule(jobInfo.getModule()) | ||
194 | + .get(jobInfo.getJobName()); | ||
195 | + updateScheduleJobInfo.getFuture().cancel(false); | ||
196 | + updateScheduleJobInfo.getJobInfo().setOperator(ConstantEnum.JobOperatorEnum.STOP.getValue()); | ||
197 | + updateScheduleJobInfo.getJobInfo().setStatus(ConstantEnum.JobStatusEnum.STOPPING.getValue()); | ||
198 | + client.setData().forPath(jobPath, | ||
199 | + JSON.toJSONString(updateScheduleJobInfo.getJobInfo()).getBytes(Charset.forName("UTF-8"))); | ||
203 | } | 200 | } |
204 | } | 201 | } |
205 | 202 | ||
@@ -207,8 +204,7 @@ public class JobServiceImpl implements JobService { | @@ -207,8 +204,7 @@ public class JobServiceImpl implements JobService { | ||
207 | public void updateJob(JobInfo jobInfo) throws Exception { | 204 | public void updateJob(JobInfo jobInfo) throws Exception { |
208 | 205 | ||
209 | String jobPath = JOBINFO_PATH + "/" + jobInfo.getModule() + "/jobinfo/" + jobInfo.getJobName(); | 206 | String jobPath = JOBINFO_PATH + "/" + jobInfo.getModule() + "/jobinfo/" + jobInfo.getJobName(); |
210 | - Stat stat = client.checkExists().forPath(jobPath); | ||
211 | - if (stat == null) { | 207 | + if (!checkJobExist(jobInfo)) { |
212 | // 当任务不存在时,create之 | 208 | // 当任务不存在时,create之 |
213 | jobInfo.setOperator(ConstantEnum.JobOperatorEnum.CREATE.getValue()); | 209 | jobInfo.setOperator(ConstantEnum.JobOperatorEnum.CREATE.getValue()); |
214 | createJob(jobInfo); | 210 | createJob(jobInfo); |
@@ -231,23 +227,25 @@ public class JobServiceImpl implements JobService { | @@ -231,23 +227,25 @@ public class JobServiceImpl implements JobService { | ||
231 | if (logger.isDebugEnabled()) { | 227 | if (logger.isDebugEnabled()) { |
232 | logger.debug("begin to retryJob job , job is {}", jobInfo); | 228 | logger.debug("begin to retryJob job , job is {}", jobInfo); |
233 | } | 229 | } |
234 | - String jobPath = JOBINFO_PATH + "/" + jobInfo.getModule() + "/jobinfo/" + jobInfo.getJobName(); | ||
235 | - jobInfo.setOperator(ConstantEnum.JobOperatorEnum.TRIGGER.getValue()); | ||
236 | - try { | ||
237 | - Stat stat = client.checkExists().forPath(jobPath); | ||
238 | - if (stat == null) { | ||
239 | - // 不存在 | ||
240 | - String message = "job not exist, job is " + jobInfo; | ||
241 | - logger.error(message); | ||
242 | - throw new Exception(message); | ||
243 | - } else { | ||
244 | - // 存在 | ||
245 | - client.setData().forPath(jobPath, JSON.toJSONString(jobInfo).getBytes(Charset.forName("UTF-8"))); | ||
246 | - } | ||
247 | - } catch (Exception e) { | 230 | + if (!checkJobExist(jobInfo)) { |
231 | + String message = "job not exist, job is " + jobInfo; | ||
232 | + logger.error(message); | ||
233 | + throw new Exception(message); | ||
234 | + } else { | ||
235 | + jobInfo.setOperator(ConstantEnum.JobOperatorEnum.TRIGGER.getValue()); | ||
236 | + // 存在 | ||
237 | + String jobPath = JOBINFO_PATH + "/" + jobInfo.getModule() + "/jobinfo/" + jobInfo.getJobName(); | ||
238 | + client.setData().forPath(jobPath, JSON.toJSONString(jobInfo).getBytes(Charset.forName("UTF-8"))); | ||
239 | + } | ||
240 | + } | ||
248 | 241 | ||
249 | - throw e; | 242 | + private boolean checkJobExist(JobInfo jobInfo) throws Exception { |
243 | + String jobPath = JOBINFO_PATH + "/" + jobInfo.getModule() + "/jobinfo/" + jobInfo.getJobName(); | ||
244 | + Stat stat = client.checkExists().forPath(jobPath); | ||
245 | + if (stat == null) { | ||
246 | + return false; | ||
247 | + } else { | ||
248 | + return true; | ||
250 | } | 249 | } |
251 | - | ||
252 | } | 250 | } |
253 | } | 251 | } |
@@ -42,6 +42,7 @@ public class JobTrigger implements Runnable { | @@ -42,6 +42,7 @@ public class JobTrigger implements Runnable { | ||
42 | String jobPath = CLIENT_PATH_PREFIX + jobInfo.getModule() + "/jobinfo/" + jobInfo.getJobName(); | 42 | String jobPath = CLIENT_PATH_PREFIX + jobInfo.getModule() + "/jobinfo/" + jobInfo.getJobName(); |
43 | try { | 43 | try { |
44 | client.setData().forPath(jobPath, JSON.toJSONString(jobInfo).getBytes(Charset.forName("UTF-8"))); | 44 | client.setData().forPath(jobPath, JSON.toJSONString(jobInfo).getBytes(Charset.forName("UTF-8"))); |
45 | + System.out.println("执行 "+jobInfo.getModule()+":"+jobInfo.getJobName()); | ||
45 | } catch (Exception e) { | 46 | } catch (Exception e) { |
46 | logger.error("trigger job faild , job is {}, exception {}", jobInfo, e); | 47 | logger.error("trigger job faild , job is {}, exception {}", jobInfo, e); |
47 | //TODO 记录错误日志,上报告警 | 48 | //TODO 记录错误日志,上报告警 |
-
Please register or login to post a comment