View Javadoc
1   /*
2    * Copyright 2013-2014 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Controller for batch job instances.
42   * 
43   * @author Ilayaperumal Gopinathan
44   * @since 2.0
45   */
46  @RestController
47  @RequestMapping("/batch/instances")
48  @ExposesResourceFor(JobInstanceInfoResource.class)
49  public class BatchJobInstancesController extends AbstractBatchJobsController {
50  
51  	/**
52  	 * Return job instance info by the given instance id.
53  	 * 
54  	 * @param instanceId job instance id
55  	 * @return job instance info
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  	 * Return a paged collection of job instances for a given job.
85  	 *
86  	 * @param jobName name of the batch job
87  	 * @param startJobInstance start index for the job instance
88  	 * @param pageSize page size for the list
89  	 * @return collection of JobInstances by job name
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 }