Skip to content

Commit 7e76cb0

Browse files
mminellacppwfs
authored andcommitted
Initial commit of Single Step Spring Batch Job Starter
This commit adds a new starter for creating a single step Spring Batch job. It allows users to configure a FlatFileItemReader and a FlatFileItemWriter as well as the job and step with just properties.
1 parent 8b41515 commit 7e76cb0

19 files changed

+2820
-0
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
<module>spring-cloud-task-samples</module>
107107
<module>spring-cloud-task-integration-tests</module>
108108
<module>spring-cloud-task-docs</module>
109+
<module>spring-cloud-starter-single-step-batch-job</module>
109110
</modules>
110111

111112
<properties>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>spring-cloud-task-parent</artifactId>
7+
<groupId>org.springframework.cloud</groupId>
8+
<version>2.3.0.BUILD-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>spring-cloud-starter-single-step-batch-job</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.springframework.boot</groupId>
17+
<artifactId>spring-boot-starter-batch</artifactId>
18+
</dependency>
19+
<dependency>
20+
<groupId>org.springframework.boot</groupId>
21+
<artifactId>spring-boot-configuration-processor</artifactId>
22+
<optional>true</optional>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-configuration-processor</artifactId>
27+
<optional>true</optional>
28+
<version>${spring-boot.version}</version>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-test</artifactId>
34+
<scope>test</scope>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.batch</groupId>
38+
<artifactId>spring-batch-test</artifactId>
39+
<scope>test</scope>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.hsqldb</groupId>
43+
<artifactId>hsqldb</artifactId>
44+
<version>2.4.0</version>
45+
<scope>test</scope>
46+
</dependency>
47+
</dependencies>
48+
49+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
* Copyright 2019-2020 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+
* https://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.cloud.task.batch.autoconfigure;
18+
19+
import java.util.Map;
20+
21+
import org.springframework.batch.item.file.FlatFileItemReader;
22+
import org.springframework.batch.item.file.LineCallbackHandler;
23+
import org.springframework.batch.item.file.LineMapper;
24+
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
25+
import org.springframework.batch.item.file.mapping.FieldSetMapper;
26+
import org.springframework.batch.item.file.separator.RecordSeparatorPolicy;
27+
import org.springframework.batch.item.file.transform.FieldSet;
28+
import org.springframework.batch.item.file.transform.LineTokenizer;
29+
import org.springframework.batch.item.file.transform.Range;
30+
import org.springframework.beans.factory.annotation.Autowired;
31+
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
32+
import org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration;
33+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
34+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
35+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
36+
import org.springframework.context.annotation.Bean;
37+
import org.springframework.context.annotation.Configuration;
38+
39+
/**
40+
* Autconfiguration for a {@code FlatFileItemReader}.
41+
*
42+
* @author Michael Minella
43+
* @since 2.3
44+
*/
45+
@Configuration
46+
@EnableConfigurationProperties(FlatFileItemReaderProperties.class)
47+
@AutoConfigureAfter(BatchAutoConfiguration.class)
48+
public class FlatFileItemReaderAutoConfiguration {
49+
50+
private final FlatFileItemReaderProperties properties;
51+
52+
@Autowired(required = false)
53+
private LineTokenizer lineTokenizer;
54+
55+
@Autowired(required = false)
56+
private FieldSetMapper<Map<Object, Object>> fieldSetMapper;
57+
58+
@Autowired(required = false)
59+
private LineMapper<Map<Object, Object>> lineMapper;
60+
61+
@Autowired(required = false)
62+
private LineCallbackHandler skippedLinesCallback;
63+
64+
@Autowired(required = false)
65+
private RecordSeparatorPolicy recordSeparatorPolicy;
66+
67+
public FlatFileItemReaderAutoConfiguration(FlatFileItemReaderProperties properties) {
68+
this.properties = properties;
69+
}
70+
71+
@Bean
72+
@ConditionalOnMissingBean
73+
@ConditionalOnProperty(prefix = "spring.batch.job.flatfilereader", name = "name")
74+
public FlatFileItemReader<Map<Object, Object>> itemReader() {
75+
FlatFileItemReaderBuilder<Map<Object, Object>> mapFlatFileItemReaderBuilder = new FlatFileItemReaderBuilder<Map<Object, Object>>()
76+
.name(this.properties.getName()).resource(this.properties.getResource())
77+
.saveState(this.properties.isSaveState())
78+
.maxItemCount(this.properties.getMaxItemCount())
79+
.currentItemCount(this.properties.getCurrentItemCount())
80+
.strict(this.properties.isStrict())
81+
.encoding(this.properties.getEncoding())
82+
.linesToSkip(this.properties.getLinesToSkip())
83+
.comments(this.properties.getComments()
84+
.toArray(new String[this.properties.getComments().size()]));
85+
86+
if (this.lineTokenizer != null) {
87+
mapFlatFileItemReaderBuilder.lineTokenizer(this.lineTokenizer);
88+
}
89+
90+
if (this.recordSeparatorPolicy != null) {
91+
mapFlatFileItemReaderBuilder
92+
.recordSeparatorPolicy(this.recordSeparatorPolicy);
93+
}
94+
95+
if (this.fieldSetMapper != null) {
96+
mapFlatFileItemReaderBuilder.fieldSetMapper(this.fieldSetMapper);
97+
}
98+
99+
if (this.lineMapper != null) {
100+
mapFlatFileItemReaderBuilder.lineMapper(this.lineMapper);
101+
}
102+
103+
if (this.skippedLinesCallback != null) {
104+
mapFlatFileItemReaderBuilder.skippedLinesCallback(skippedLinesCallback);
105+
}
106+
107+
if (this.properties.isDelimited()) {
108+
mapFlatFileItemReaderBuilder.delimited()
109+
.quoteCharacter(this.properties.getQuoteCharacter())
110+
.delimiter(this.properties.getDelimiter())
111+
.includedFields(
112+
this.properties.getIncludedFields().toArray(new Integer[0]))
113+
.names(this.properties.getNames())
114+
.beanMapperStrict(this.properties.isParsingStrict())
115+
.fieldSetMapper(new MapFieldSetMapper());
116+
}
117+
else if (this.properties.isFixedLength()) {
118+
mapFlatFileItemReaderBuilder.fixedLength()
119+
.columns(this.properties.getRanges().toArray(new Range[0]))
120+
.names(this.properties.getNames())
121+
.fieldSetMapper(new MapFieldSetMapper())
122+
.beanMapperStrict(this.properties.isParsingStrict());
123+
}
124+
125+
return mapFlatFileItemReaderBuilder.build();
126+
}
127+
128+
/**
129+
* A {@link FieldSetMapper} that takes a {@code FieldSet} and returns the
130+
* {@code Map<Object, Object>} of its contents.
131+
*/
132+
public static class MapFieldSetMapper implements FieldSetMapper<Map<Object, Object>> {
133+
134+
@Override
135+
public Map<Object, Object> mapFieldSet(FieldSet fieldSet) {
136+
return fieldSet.getProperties();
137+
}
138+
139+
}
140+
141+
}

0 commit comments

Comments
 (0)