Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
fmbenhassine committed Feb 19, 2020
1 parent 33d528d commit 5ee34b4
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 0 deletions.
41 changes: 41 additions & 0 deletions gh1064/pom.xml
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>
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();
}
}
59 changes: 59 additions & 0 deletions gh1064/src/main/java/com/example/demo/MyJob.java
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());
}

}
1 change: 1 addition & 0 deletions gh1064/src/main/resources/data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Sale012016 1 00000011000000000Sale022017 2 00000022000000000Sale032018 3 00000033000000000

0 comments on commit 5ee34b4

Please sign in to comment.