Skip to content
This repository was archived by the owner on Mar 29, 2019. It is now read-only.

Commit fa69a53

Browse files
committed
Added EnableBatchAdmin and fixed pagination
1 parent 725ba93 commit fa69a53

File tree

11 files changed

+181
-63
lines changed

11 files changed

+181
-63
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.springframework.batch.admin.annotation;
2+
3+
import java.lang.annotation.Documented;
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
import java.lang.annotation.Target;
8+
9+
import org.springframework.context.annotation.ImportResource;
10+
11+
/**
12+
* This annotation is responsible for bootstrapping Spring Batch Admin
13+
* within an existing web application.
14+
*
15+
* @author Michael Minella
16+
* @since 2.0
17+
*/
18+
@Target(ElementType.TYPE)
19+
@Retention(RetentionPolicy.RUNTIME)
20+
@Documented
21+
@ImportResource({"classpath*:/META-INF/spring/batch/bootstrap/**/*.xml",
22+
"classpath*:/META-INF/spring/batch/override/**/*.xml",
23+
"classpath*:/org/springframework/batch/admin/web/resources/servlet-config.xml"})
24+
public @interface EnableBatchAdmin {
25+
}

spring-batch-admin-manager/src/main/java/org/springframework/batch/admin/web/BatchJobExecutionsController.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,26 +97,27 @@ public PagedResources<JobExecutionInfoResource> list(Pageable pageable) throws N
9797
* Return a paged collection of job executions for a given job.
9898
*
9999
* @param jobName name of the job
100-
* @param startJobExecution start index for the job execution list
101-
* @param pageSize page size for the list
102-
* @return collection of JobExecutionInfo
100+
* @param pageable If not provided will default to page 0 and a page size of 20
101+
* @return Collection of JobExecutionInfo
103102
*/
104103
@RequestMapping(value = "", method = RequestMethod.GET, params = "jobname")
105104
@ResponseStatus(HttpStatus.OK)
106-
public Collection<JobExecutionInfoResource> executionsForJob(@RequestParam("jobname") String jobName,
107-
@RequestParam(defaultValue = "0") int startJobExecution,
108-
@RequestParam(defaultValue = "20") int pageSize) {
105+
public PagedResources<JobExecutionInfoResource> executionsForJob(@RequestParam("jobname") String jobName,
106+
Pageable pageable) {
109107

110108
Collection<JobExecutionInfoResource> result = new ArrayList<JobExecutionInfoResource>();
111109
try {
112-
for (JobExecution jobExecution : jobService.listJobExecutionsForJob(jobName, startJobExecution, pageSize)) {
110+
for (JobExecution jobExecution : jobService.listJobExecutionsForJob(jobName, pageable.getOffset(), pageable.getPageSize())) {
113111
result.add(jobExecutionInfoResourceAssembler.toResource(new JobExecutionInfo(jobExecution, timeZone)));
114112
}
113+
114+
return new PagedResources<JobExecutionInfoResource>(result,
115+
new PageMetadata(pageable.getPageSize(), pageable.getPageNumber(),
116+
jobService.countJobExecutionsForJob(jobName)));
115117
}
116118
catch (NoSuchJobException e) {
117119
throw new NoSuchBatchJobException(jobName);
118120
}
119-
return result;
120121
}
121122

122123
/**
@@ -127,11 +128,11 @@ public Collection<JobExecutionInfoResource> executionsForJob(@RequestParam("jobn
127128
*/
128129
@RequestMapping(value = "", method = RequestMethod.POST, params = "jobname")
129130
@ResponseStatus(HttpStatus.CREATED)
130-
public void launchJob(@RequestParam("jobname") String name, @RequestParam(required = false) String jobParameters) throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, NoSuchJobException {
131+
public void launchJob(@RequestParam("jobname") String name, @RequestParam(value = "jobParameters", required = false) String jobParameters) throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, NoSuchJobException {
131132
JobParameters params = new JobParameters();
132133
if(jobParameters != null) {
133134
JobParametersExtractor extractor = new JobParametersExtractor();
134-
extractor.fromString(jobParameters);
135+
params = extractor.fromString(jobParameters);
135136
}
136137

137138
jobService.launch(name, params);

spring-batch-admin-manager/src/main/java/org/springframework/batch/admin/web/BatchJobInstancesController.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@
2828
import org.springframework.batch.core.JobInstance;
2929
import org.springframework.batch.core.launch.NoSuchJobException;
3030
import org.springframework.batch.core.launch.NoSuchJobInstanceException;
31+
import org.springframework.data.domain.PageImpl;
32+
import org.springframework.data.domain.Pageable;
33+
import org.springframework.data.web.PagedResourcesAssembler;
3134
import org.springframework.hateoas.ExposesResourceFor;
35+
import org.springframework.hateoas.PagedResources;
3236
import org.springframework.http.HttpStatus;
3337
import org.springframework.stereotype.Controller;
3438
import org.springframework.web.bind.annotation.PathVariable;
@@ -84,29 +88,24 @@ public JobInstanceInfoResource getJobInstance(@PathVariable long instanceId) {
8488
* Return a paged collection of job instances for a given job.
8589
*
8690
* @param jobName name of the batch job
87-
* @param startJobInstance start index for the job instance
88-
* @param pageSize page size for the list
8991
* @return collection of JobInstances by job name
9092
*/
9193
@RequestMapping(value = "", method = RequestMethod.GET, params = "jobname")
9294
@ResponseStatus(HttpStatus.OK)
93-
public Collection<JobInstanceInfoResource> instancesForJob(@RequestParam("jobname") String jobName,
94-
@RequestParam(defaultValue = "0") int startJobInstance, @RequestParam(defaultValue = "20") int pageSize) {
95+
public PagedResources<JobInstanceInfoResource> instancesForJob(Pageable pageable, PagedResourcesAssembler<JobInstanceInfo> assembler, @RequestParam("jobname") String jobName) {
9596

9697
try {
97-
Collection<JobInstance> jobInstances = jobService.listJobInstances(jobName, startJobInstance, pageSize);
98-
List<JobInstanceInfoResource> result = new ArrayList<JobInstanceInfoResource>();
98+
List<JobInstanceInfo> result = new ArrayList<JobInstanceInfo>();
99+
long total = jobService.countJobInstances(jobName);
100+
101+
Collection<JobInstance> jobInstances = jobService.listJobInstances(jobName, pageable.getOffset(), pageable.getPageSize());
99102
for (JobInstance jobInstance : jobInstances) {
100103
List<JobExecution> jobExecutions = (List<JobExecution>) jobService.getJobExecutionsForJobInstance(
101104
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)));
105+
result.add(new JobInstanceInfo(jobInstance, jobExecutions));
108106
}
109-
return result;
107+
108+
return assembler.toResource(new PageImpl<JobInstanceInfo>(result, pageable, total), jobInstanceInfoResourceAssembler);
110109
}
111110
catch (NoSuchJobException e) {
112111
throw new NoSuchBatchJobException(jobName);

spring-batch-admin-manager/src/main/java/org/springframework/batch/admin/web/RestConfiguration.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,18 @@ public BatchStepExecutionsController batchStepExecutionsController() {
5858
return new BatchStepExecutionsController();
5959
}
6060

61-
@Bean
62-
public ViewResolver jsonViewResolver() {
63-
return new JsonViewResolver();
64-
}
65-
6661
@Bean
6762
public ViewResolver contentNegotiatingViewResolver(ContentNegotiationManager manager) {
6863
// Define the view resolvers
6964
List<ViewResolver> resolvers = new ArrayList<ViewResolver>();
7065

71-
resolvers.add(jsonViewResolver());
66+
resolvers.add(new JsonViewResolver());
7267

7368
// Create the CNVR plugging in the resolvers and the content-negotiation manager
7469
ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
7570
resolver.setViewResolvers(resolvers);
7671
resolver.setContentNegotiationManager(manager);
72+
7773
return resolver;
7874
}
7975
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright 2015 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+
package org.springframework.batch.admin.annotation;
17+
18+
import static org.junit.Assert.assertEquals;
19+
20+
import java.net.URL;
21+
import java.net.URLClassLoader;
22+
23+
import javax.sql.DataSource;
24+
25+
import org.junit.After;
26+
import org.junit.Before;
27+
import org.junit.Test;
28+
29+
import org.springframework.batch.admin.service.JobService;
30+
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
31+
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
32+
import org.springframework.batch.core.explore.JobExplorer;
33+
import org.springframework.batch.core.launch.JobLauncher;
34+
import org.springframework.batch.core.repository.JobRepository;
35+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
36+
import org.springframework.context.annotation.Configuration;
37+
import org.springframework.stereotype.Controller;
38+
import org.springframework.transaction.PlatformTransactionManager;
39+
40+
/**
41+
* @author Michael Minella
42+
*/
43+
public class EnableBatchAdminTests {
44+
45+
private AnnotationConfigApplicationContext context;
46+
47+
@Before
48+
public void setUp() {
49+
this.context = new AnnotationConfigApplicationContext(BatchAdminConfiguration.class);
50+
}
51+
52+
@After
53+
public void tearDown() {
54+
this.context.close();
55+
}
56+
57+
@Test
58+
public void testContext() {
59+
60+
ClassLoader cl = ClassLoader.getSystemClassLoader();
61+
62+
URL[] urls = ((URLClassLoader)cl).getURLs();
63+
64+
System.out.println("****************************************");
65+
for(URL url: urls){
66+
System.out.println(url.getFile());
67+
}
68+
69+
String[] beanDefinitionNames = this.context.getBeanDefinitionNames();
70+
71+
System.out.println("****************************************");
72+
for (String beanDefinitionName : beanDefinitionNames) {
73+
System.out.println(beanDefinitionName);
74+
}
75+
System.out.println("****************************************");
76+
77+
assertEquals(1, this.context.getBeanNamesForType(JobBuilderFactory.class).length);
78+
assertEquals(1, this.context.getBeanNamesForType(StepBuilderFactory.class).length);
79+
assertEquals(1, this.context.getBeanNamesForType(JobRepository.class).length);
80+
assertEquals(1, this.context.getBeanNamesForType(JobExplorer.class).length);
81+
assertEquals(1, this.context.getBeanNamesForType(JobLauncher.class).length);
82+
assertEquals(9, this.context.getBeanNamesForAnnotation(Controller.class).length);
83+
assertEquals(1, this.context.getBeanNamesForType(DataSource.class).length);
84+
assertEquals(1, this.context.getBeanNamesForType(PlatformTransactionManager.class).length);
85+
assertEquals(1, this.context.getBeanNamesForType(JobService.class).length);
86+
}
87+
88+
@Configuration
89+
@EnableBatchAdmin
90+
public static class BatchAdminConfiguration {
91+
}
92+
}

spring-batch-admin-manager/src/test/java/org/springframework/batch/admin/web/BatchJobExecutionsControllerIntegrationTests.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,23 +97,22 @@ public void testGetJobExecutionsByName() throws Exception {
9797
when(jobService.countJobExecutions()).thenReturn(1);
9898

9999
mockMvc.perform(
100-
get("/batch/executions").param("jobname", "job1")
101-
.param("startJobExecution", "0").param("pageSize", "20").accept(
100+
get("/batch/executions").param("jobname", "job1").param("startJobInstance", "0").param("pageSize", "20").accept(
102101
MediaType.APPLICATION_JSON)).andDo(print()).andExpect(status().isOk())
103-
.andExpect(jsonPath("$.[*]", Matchers.hasSize(1)))
104-
.andExpect(jsonPath("$.[*][0].executionId").value(3))
105-
.andExpect(jsonPath("$.[*][0].jobId").value(2))
102+
.andExpect(jsonPath("$.pagedResources.content", Matchers.hasSize(1)))
103+
.andExpect(jsonPath("$.pagedResources.content[0].executionId").value(3))
104+
.andExpect(jsonPath("$.pagedResources.content[0].jobId").value(2))
106105
.andExpect(
107-
jsonPath("$.[*][0].jobParameters.parameters.param1.value").value("test"))
106+
jsonPath("$.pagedResources.content[0].jobParameters.parameters.param1.value").value("test"))
108107
.andExpect(
109-
jsonPath("$.[*][0].jobParameters.parameters.param1.type").value("STRING"))
110-
.andExpect(jsonPath("$.[*][0].jobParameters.parameters.param1.identifying").value(
108+
jsonPath("$.pagedResources.content[0].jobParameters.parameters.param1.type").value("STRING"))
109+
.andExpect(jsonPath("$.pagedResources.content[0].jobParameters.parameters.param1.identifying").value(
111110
true))
112111
.andExpect(
113-
jsonPath("$.[*][0].jobParameters.parameters.param2.value").value(123))
112+
jsonPath("$.pagedResources.content[0].jobParameters.parameters.param2.value").value(123))
114113
.andExpect(
115-
jsonPath("$.[*][0].jobParameters.parameters.param2.type").value("LONG"))
116-
.andExpect(jsonPath("$.[*][0].jobParameters.parameters.param2.identifying").value(
114+
jsonPath("$.pagedResources.content[0].jobParameters.parameters.param2.type").value("LONG"))
115+
.andExpect(jsonPath("$.pagedResources.content[0].jobParameters.parameters.param2.identifying").value(
117116
false));
118117
}
119118

spring-batch-admin-manager/src/test/java/org/springframework/batch/admin/web/BatchJobInstancesControllerIntegrationTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ public void testGetJobInstanceByJobName() throws Exception {
9999
mockMvc.perform(
100100
get("/batch/instances").param("jobname", "job1").param("startJobInstance", "0").param("pageSize", "20").accept(
101101
MediaType.APPLICATION_JSON)).andDo(print()).andExpect(status().isOk())
102-
.andExpect(jsonPath("$.jobInstanceInfoResourceList", Matchers.hasSize(2)))
103-
.andExpect(jsonPath("$.jobInstanceInfoResourceList[*].instanceId", contains(0, 3)))
104-
.andExpect(jsonPath("$.jobInstanceInfoResourceList[*].jobName", contains("job1", "job1")));
102+
.andExpect(jsonPath("$.pagedResources.content", Matchers.hasSize(2)))
103+
.andExpect(jsonPath("$.pagedResources.content[*].instanceId", contains(0, 3)))
104+
.andExpect(jsonPath("$.pagedResources.content[*].jobName", contains("job1", "job1")));
105105
}
106106

107107
@Test

spring-batch-admin-resources/src/main/java/org/springframework/batch/admin/web/util/HomeController.java

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,26 @@
1515
*/
1616
package org.springframework.batch.admin.web.util;
1717

18+
import java.lang.reflect.Method;
19+
import java.util.ArrayList;
20+
import java.util.Arrays;
21+
import java.util.Collection;
22+
import java.util.Enumeration;
23+
import java.util.HashMap;
24+
import java.util.HashSet;
25+
import java.util.Iterator;
26+
import java.util.List;
27+
import java.util.Map;
28+
import java.util.Properties;
29+
import java.util.Set;
30+
import java.util.SortedSet;
31+
import java.util.TreeSet;
32+
33+
import javax.servlet.http.HttpServletRequest;
34+
1835
import org.apache.commons.logging.Log;
1936
import org.apache.commons.logging.LogFactory;
37+
2038
import org.springframework.beans.BeansException;
2139
import org.springframework.beans.factory.InitializingBean;
2240
import org.springframework.context.ApplicationContext;
@@ -32,22 +50,6 @@
3250
import org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping;
3351
import org.springframework.web.util.UrlPathHelper;
3452

35-
import javax.servlet.http.HttpServletRequest;
36-
import java.lang.reflect.Method;
37-
import java.util.ArrayList;
38-
import java.util.Arrays;
39-
import java.util.Collection;
40-
import java.util.Enumeration;
41-
import java.util.HashMap;
42-
import java.util.HashSet;
43-
import java.util.Iterator;
44-
import java.util.List;
45-
import java.util.Map;
46-
import java.util.Properties;
47-
import java.util.Set;
48-
import java.util.SortedSet;
49-
import java.util.TreeSet;
50-
5153
/**
5254
* Component that discovers request mappings in its application context and
5355
* reveals their meta data. Any {@link RequestMapping} annotations in controller
@@ -284,7 +286,7 @@ private Set<String> findUniqueUrls(Collection<String> inputs) {
284286
*
285287
* @return a map of URI pattern to request methods accepted
286288
*/
287-
@RequestMapping(value = { "/", "/home" }, method = RequestMethod.GET)
289+
@RequestMapping(value = { "/home" }, method = RequestMethod.GET)
288290
public String getResources(HttpServletRequest request, ModelMap model) {
289291

290292
String servletPath = this.servletPath;

spring-batch-admin-resources/src/main/resources/META-INF/spring/batch/servlet/resources/resources-context.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@
7979
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
8080

8181
<!-- Support for Freemarker beans as views -->
82-
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
82+
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
83+
<property name="order">
84+
<util:constant static-field="org.springframework.core.Ordered.HIGHEST_PRECEDENCE"/>
85+
</property>
86+
</bean>
8387

8488
<bean id="baseMenu" abstract="true">
8589
<property name="prefix" value="#{resourceService.servletPath}" />

spring-batch-admin-resources/src/main/resources/org/springframework/batch/admin/web/resources/servlet-config.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
3+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
44

55
<import resource="classpath*:/META-INF/spring/batch/servlet/resources/*.xml" />
66
<import resource="classpath*:/META-INF/spring/batch/servlet/manager/*.xml" />

0 commit comments

Comments
 (0)