1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.batch.admin.web;
18
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.List;
22
23 import org.springframework.batch.admin.domain.JobExecutionInfo;
24 import org.springframework.batch.admin.domain.JobInstanceInfoResource;
25 import org.springframework.batch.admin.domain.NoSuchBatchJobException;
26 import org.springframework.batch.admin.domain.NoSuchBatchJobInstanceException;
27 import org.springframework.batch.core.JobExecution;
28 import org.springframework.batch.core.JobInstance;
29 import org.springframework.batch.core.launch.NoSuchJobException;
30 import org.springframework.batch.core.launch.NoSuchJobInstanceException;
31 import org.springframework.hateoas.ExposesResourceFor;
32 import org.springframework.http.HttpStatus;
33 import org.springframework.web.bind.annotation.PathVariable;
34 import org.springframework.web.bind.annotation.RequestMapping;
35 import org.springframework.web.bind.annotation.RequestMethod;
36 import org.springframework.web.bind.annotation.RequestParam;
37 import org.springframework.web.bind.annotation.ResponseStatus;
38 import org.springframework.web.bind.annotation.RestController;
39
40
41
42
43
44
45
46 @RestController
47 @RequestMapping("/batch/instances")
48 @ExposesResourceFor(JobInstanceInfoResource.class)
49 public class BatchJobInstancesController extends AbstractBatchJobsController {
50
51
52
53
54
55
56
57 @RequestMapping(value = "/{instanceId}", method = RequestMethod.GET)
58 @ResponseStatus(HttpStatus.OK)
59 public JobInstanceInfoResource getJobInstance(@PathVariable long instanceId) {
60 try {
61 JobInstance jobInstance = jobService.getJobInstance(instanceId);
62 String jobName = jobInstance.getJobName();
63
64 try {
65 List<JobExecution> jobExecutions = (List<JobExecution>) jobService.getJobExecutionsForJobInstance(
66 jobInstance.getJobName(), jobInstance.getId());
67 List<JobExecutionInfo> jobExecutionInfos = new ArrayList<JobExecutionInfo>();
68 for (JobExecution jobExecution : jobExecutions) {
69 jobExecutionInfos.add(new JobExecutionInfo(jobExecution, timeZone));
70 }
71
72 return jobInstanceInfoResourceAssembler.toResource(new JobInstanceInfo(jobInstance, jobExecutions));
73 }
74 catch (NoSuchJobException e) {
75 throw new NoSuchBatchJobException(jobName);
76 }
77 }
78 catch (NoSuchJobInstanceException e) {
79 throw new NoSuchBatchJobInstanceException(instanceId);
80 }
81 }
82
83
84
85
86
87
88
89
90
91 @RequestMapping(value = "", method = RequestMethod.GET, params = "jobname")
92 @ResponseStatus(HttpStatus.OK)
93 public Collection<JobInstanceInfoResource> instancesForJob(@RequestParam("jobname") String jobName,
94 @RequestParam(defaultValue = "0") int startJobInstance, @RequestParam(defaultValue = "20") int pageSize) {
95
96 try {
97 Collection<JobInstance> jobInstances = jobService.listJobInstances(jobName, startJobInstance, pageSize);
98 List<JobInstanceInfoResource> result = new ArrayList<JobInstanceInfoResource>();
99 for (JobInstance jobInstance : jobInstances) {
100 List<JobExecution> jobExecutions = (List<JobExecution>) jobService.getJobExecutionsForJobInstance(
101 jobName, jobInstance.getId());
102 List<JobExecutionInfo> jobExecutionInfos = new ArrayList<JobExecutionInfo>();
103 for (JobExecution jobExecution : jobExecutions) {
104 jobExecutionInfos.add(new JobExecutionInfo(jobExecution, timeZone));
105 }
106 result.add(jobInstanceInfoResourceAssembler.toResource(new JobInstanceInfo(jobInstance,
107 jobExecutions)));
108 }
109 return result;
110 }
111 catch (NoSuchJobException e) {
112 throw new NoSuchBatchJobException(jobName);
113 }
114 }
115 }