JobServiceController.java
4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
*
*/
package com.yoho.jobs.server.controller;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yoho.jobs.common.domain.ConstantEnum;
import com.yoho.jobs.common.domain.JobInfo;
import com.yoho.jobs.dal.IJobResultMapper;
import com.yoho.jobs.dal.domain.ProcessResultVO;
import com.yoho.jobs.server.domain.JobResponse;
import com.yoho.jobs.server.domain.PageRequest;
import com.yoho.jobs.server.domain.PageResponse;
import com.yoho.jobs.server.scheduler.JobService;
/**
* @author yanzhang.fu
*
*/
@RestController
public class JobServiceController {
@Resource(name = "jobService")
private JobService jobService;
@Resource
private IJobResultMapper jobResultMapper;
@Value("${limitrecord}")
private int limit;
@RequestMapping("/jobmanager/createjob.do")
public JobResponse createJob(@RequestBody JobInfo jobInfo) {
JobResponse response = new JobResponse();
try {
jobService.createJob(jobInfo);
} catch (Exception e) {
buildJobResponse(ConstantEnum.JobResponse.FAIL, e.getMessage(), response);
}
return response;
}
@RequestMapping("/jobmanager/stopjob.do")
public JobResponse stopJob(@RequestBody JobInfo jobInfo) {
JobResponse response = new JobResponse();
try {
jobService.stopJob(jobInfo);
} catch (Exception e) {
buildJobResponse(ConstantEnum.JobResponse.FAIL, e.getMessage(), response);
}
return response;
}
@RequestMapping("/jobmanager/startjob.do")
public JobResponse startJob(@RequestBody JobInfo jobInfo) {
JobResponse response = new JobResponse();
try {
jobService.startJob(jobInfo);
} catch (Exception e) {
buildJobResponse(ConstantEnum.JobResponse.FAIL, e.getMessage(), response);
}
return response;
}
@RequestMapping("/jobmanager/updatejob.do")
public JobResponse updateJob(@RequestBody JobInfo jobInfo) {
JobResponse response = new JobResponse();
try {
jobService.updateJob(jobInfo);
} catch (Exception e) {
buildJobResponse(ConstantEnum.JobResponse.FAIL, e.getMessage(), response);
}
return response;
}
@RequestMapping("/jobmanager/showalljob.do")
public JobResponse showAllJobs(@RequestBody PageRequest request) {
JobResponse response = new JobResponse();
try {
checkParam(request);
Map<String, List<JobInfo>> jobs = jobService.getAllJobs();
List<JobInfo> allJobs = new ArrayList<JobInfo>();
jobs.forEach((k, v) -> {
allJobs.addAll(v);
});
PageResponse<JobInfo> pr = new PageResponse<JobInfo>();
int endIdx = min(allJobs.size(), request.getCurrentPage() * request.getPageSize());
pr.setRows(allJobs.subList((request.getCurrentPage() - 1)*request.getPageSize(), endIdx));
pr.setCurrPage(request.getCurrentPage());
pr.setPageSize(request.getPageSize());
pr.setTotal(allJobs.size());
response.setData(pr);
response.setCode(ConstantEnum.JobResponse.SUCCESS);
response.setDesc(ConstantEnum.JobResponse.SUCCESS_DESC);
} catch (Exception e) {
response.setCode(ConstantEnum.JobResponse.FAIL);
response.setDesc(e.getMessage());
}
return response;
}
private void checkParam(PageRequest request) throws Exception {
if(request.getCurrentPage()<1 || request.getPageSize()<1) {
throw new Exception("check request failed. currentPage or pageSize less than 1");
}
}
@RequestMapping("/jobmanager/retryjob.do")
public JobResponse retryJob(@RequestBody JobInfo jobInfo) {
JobResponse response = new JobResponse();
try {
jobService.retryJob(jobInfo);
} catch (Exception e) {
buildJobResponse(ConstantEnum.JobResponse.FAIL, e.getMessage(), response);
}
return response;
}
@RequestMapping("/jobmanager/getdetailjob.do")
public JobResponse getDetailJob(@RequestBody JobInfo jobInfo) {
JobResponse response = new JobResponse();
try {
List<ProcessResultVO> jobs = jobResultMapper.selectJobs(jobInfo.getJobName(), jobInfo.getModule(), limit);
PageResponse<ProcessResultVO> pr = new PageResponse<ProcessResultVO>();
pr.setRows(jobs);
pr.setCurrPage(1);
pr.setTotal(jobs.size());
response.setData(pr);
} catch (Exception e) {
buildJobResponse(ConstantEnum.JobResponse.FAIL, e.getMessage(), response);
}
return response;
}
public int getLimit() {
return limit;
}
public void setLimit(int limit) {
this.limit = limit;
}
private void buildJobResponse(String code, String message, JobResponse response) {
response.setCode(code);
response.setDesc(message);
}
private int min(int a, int b) {
return a > b ? b : a;
}
}