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.DetailedJobInfo;
24 import org.springframework.batch.admin.domain.DetailedJobInfoResource;
25 import org.springframework.batch.admin.domain.JobExecutionInfo;
26 import org.springframework.batch.admin.domain.NoSuchBatchJobException;
27 import org.springframework.batch.core.JobExecution;
28 import org.springframework.batch.core.launch.NoSuchJobException;
29 import org.springframework.data.domain.PageImpl;
30 import org.springframework.data.domain.Pageable;
31 import org.springframework.data.web.PagedResourcesAssembler;
32 import org.springframework.hateoas.ExposesResourceFor;
33 import org.springframework.hateoas.PagedResources;
34 import org.springframework.http.HttpStatus;
35 import org.springframework.web.bind.annotation.PathVariable;
36 import org.springframework.web.bind.annotation.RequestMapping;
37 import org.springframework.web.bind.annotation.RequestMethod;
38 import org.springframework.web.bind.annotation.ResponseStatus;
39 import org.springframework.web.bind.annotation.RestController;
40
41
42
43
44
45
46
47
48
49
50
51 @RestController
52 @RequestMapping("/batch/configurations")
53 @ExposesResourceFor(DetailedJobInfoResource.class)
54 public class BatchJobsController extends AbstractBatchJobsController {
55
56
57
58
59
60
61
62 @RequestMapping(value = "", method = RequestMethod.GET)
63 @ResponseStatus(HttpStatus.OK)
64 public PagedResources<DetailedJobInfoResource> jobs(Pageable pageable,
65 PagedResourcesAssembler<DetailedJobInfo> assembler) {
66 int total = jobService.countJobs();
67 Collection<String> names = jobService.listJobs(pageable.getOffset(), pageable.getPageSize());
68 List<DetailedJobInfo> detailedJobs = new ArrayList<DetailedJobInfo>();
69 for (String name : names) {
70 detailedJobs.add(getJobInfo(name));
71 }
72 return assembler.toResource(
73 new PageImpl<DetailedJobInfo>(detailedJobs, pageable, total),
74 jobInfoResourceAssembler);
75 }
76
77
78
79
80
81 @RequestMapping(value = "/{jobName}", method = RequestMethod.GET)
82 @ResponseStatus(HttpStatus.OK)
83 public DetailedJobInfoResource jobinfo(@PathVariable String jobName) {
84 return getJobInfoResource(jobName);
85 }
86
87
88
89
90
91 private DetailedJobInfoResource getJobInfoResource(String jobName) {
92 return jobInfoResourceAssembler.toResource(getJobInfo(jobName));
93 }
94
95
96
97
98
99
100
101 private DetailedJobInfo getJobInfo(String jobName) {
102 boolean launchable = jobService.isLaunchable(jobName);
103 try {
104 int count = jobService.countJobExecutionsForJob(jobName);
105 return new DetailedJobInfo(jobName, count, launchable,
106 jobService.isIncrementable(jobName),
107 getLastExecution(jobName));
108 }
109 catch (NoSuchJobException e) {
110 throw new NoSuchBatchJobException(jobName);
111 }
112 }
113
114
115
116
117
118
119 private JobExecutionInfo getLastExecution(String jobName) throws NoSuchJobException {
120 Collection<JobExecution> executions = jobService.listJobExecutionsForJob(jobName, 0, 1);
121 if (executions.size() > 0) {
122 return new JobExecutionInfo(executions.iterator().next(), timeZone);
123 }
124 else {
125 return null;
126 }
127 }
128
129 }