-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sample app for spring-projects/spring-batch#1064
- Loading branch information
1 parent
33d528d
commit 5ee34b4
Showing
4 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.example</groupId> | ||
<artifactId>demo</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>demo</name> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<java.version>1.8</java.version> | ||
<spring-batch-core.version>4.2.1.RELEASE</spring-batch-core.version> | ||
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.batch</groupId> | ||
<artifactId>spring-batch-core</artifactId> | ||
<version>${spring-batch-core.version}</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>${maven-compiler-plugin.version}</version> | ||
<configuration> | ||
<source>${java.version}</source> | ||
<target>${java.version}</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
40 changes: 40 additions & 0 deletions
40
gh1064/src/main/java/com/example/demo/FixedLengthBufferedReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.example.demo; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.Reader; | ||
|
||
public class FixedLengthBufferedReader extends BufferedReader { | ||
|
||
public static final int DEFAULT_LINE_LENGTH = 32; | ||
private int length; | ||
|
||
public FixedLengthBufferedReader(Reader in) { | ||
this(in, DEFAULT_LINE_LENGTH); | ||
} | ||
|
||
public FixedLengthBufferedReader(Reader in, int length) { | ||
super(in); | ||
this.length = length; | ||
} | ||
|
||
// FIXME quick and dirty: add sanity checks, etc | ||
@Override | ||
public String readLine() throws IOException { | ||
int next = read(); | ||
if (next == -1) { | ||
return null; | ||
} | ||
StringBuilder buffer = new StringBuilder(); | ||
buffer.append((char)next); | ||
for (int i = 1; i < length; i++) { | ||
next = read(); | ||
if (next != -1) { | ||
buffer.append((char)next); | ||
} else { | ||
break; | ||
} | ||
} | ||
return buffer.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.example.demo; | ||
|
||
import java.io.InputStreamReader; | ||
|
||
import org.springframework.batch.core.Job; | ||
import org.springframework.batch.core.JobParameters; | ||
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; | ||
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; | ||
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; | ||
import org.springframework.batch.core.launch.JobLauncher; | ||
import org.springframework.batch.item.ItemWriter; | ||
import org.springframework.batch.item.file.FlatFileItemReader; | ||
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder; | ||
import org.springframework.batch.item.file.mapping.PassThroughLineMapper; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.io.ClassPathResource; | ||
|
||
@Configuration | ||
@EnableBatchProcessing | ||
public class MyJob { | ||
|
||
@Bean | ||
public FlatFileItemReader<String> itemReader() { | ||
return new FlatFileItemReaderBuilder<String>() | ||
.name("fragmentReader") | ||
.resource(new ClassPathResource("data.txt")) | ||
.lineMapper(new PassThroughLineMapper()) | ||
.bufferedReaderFactory((resource, encoding) -> | ||
new FixedLengthBufferedReader(new InputStreamReader(resource.getInputStream(), encoding))) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public ItemWriter<String> itemWriter() { | ||
return items -> items.forEach(System.out::println); | ||
} | ||
|
||
@Bean | ||
public Job job(JobBuilderFactory jobs, StepBuilderFactory steps) { | ||
return jobs.get("job") | ||
.start(steps.get("step") | ||
.<String, String>chunk(2) | ||
.reader(itemReader()) | ||
.writer(itemWriter()) | ||
.build()) | ||
.build(); | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
ApplicationContext context = new AnnotationConfigApplicationContext(MyJob.class); | ||
JobLauncher jobLauncher = context.getBean(JobLauncher.class); | ||
Job job = context.getBean(Job.class); | ||
jobLauncher.run(job, new JobParameters()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Sale012016 1 00000011000000000Sale022017 2 00000022000000000Sale032018 3 00000033000000000 |