Add java configuration for the fixed length file import sample

Issue #3663
This commit is contained in:
Mahmoud Ben Hassine
2023-09-26 09:35:05 +02:00
parent 2750238462
commit 43e7c8c9f1
7 changed files with 219 additions and 85 deletions

View File

@@ -0,0 +1,85 @@
package org.springframework.batch.sample.file.fixed;
import javax.sql.DataSource;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.FlatFileItemWriter;
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
import org.springframework.batch.item.file.builder.FlatFileItemWriterBuilder;
import org.springframework.batch.item.file.transform.Range;
import org.springframework.batch.sample.domain.trade.CustomerCredit;
import org.springframework.batch.sample.domain.trade.internal.CustomerCreditIncreaseProcessor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.WritableResource;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.jdbc.support.JdbcTransactionManager;
@Configuration
@EnableBatchProcessing
public class FixedLengthJobConfiguration {
@Bean
@StepScope
public FlatFileItemReader<CustomerCredit> itemReader(@Value("#{jobParameters[inputFile]}") Resource resource) {
return new FlatFileItemReaderBuilder<CustomerCredit>().name("itemReader")
.resource(resource)
.fixedLength()
.columns(new Range(1, 9), new Range(10, 11))
.names("name", "credit")
.targetType(CustomerCredit.class)
.build();
}
@Bean
@StepScope
public FlatFileItemWriter<CustomerCredit> itemWriter(
@Value("#{jobParameters[outputFile]}") WritableResource resource) {
return new FlatFileItemWriterBuilder<CustomerCredit>().name("itemWriter")
.resource(resource)
.formatted()
.format("%-9s%-2.0f")
.names("name", "credit")
.build();
}
@Bean
public Job job(JobRepository jobRepository, JdbcTransactionManager transactionManager,
ItemReader<CustomerCredit> itemReader, ItemWriter<CustomerCredit> itemWriter) {
return new JobBuilder("ioSampleJob", jobRepository)
.start(new StepBuilder("step1", jobRepository).<CustomerCredit, CustomerCredit>chunk(2, transactionManager)
.reader(itemReader)
.processor(new CustomerCreditIncreaseProcessor())
.writer(itemWriter)
.build())
.build();
}
// Infrastructure beans
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL)
.addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql")
.addScript("/org/springframework/batch/core/schema-hsqldb.sql")
.generateUniqueName(true)
.build();
}
@Bean
public JdbcTransactionManager transactionManager(DataSource dataSource) {
return new JdbcTransactionManager(dataSource);
}
}

View File

@@ -0,0 +1,41 @@
### Fixed Length File Import Job
## About
The goal is to demonstrate a typical scenario of reading data
from a fixed length file, processing it and writing it to another file.
In this example we are using a simple fixed length record structure
that can be found in the project at
`src/main/resources/org/springframework/batch/sample/file/fixed/data`.
The fixed length records look like this:
```
customer110
customer220
customer330
customer440
customer550
customer660
```
Looking back to the configuration of the reader you will this is
configured in the fixed column ranges:
FieldName | Range
--------- | :----:
name | 1,9
credit | 10,11
## Run the sample
You can run the sample from the command line as following:
```
$>cd spring-batch-samples
# Launch the sample using the XML configuration
$>../mvnw -Dtest=FixedLengthFunctionalTests#testLaunchJobWithXmlConfig test
# Launch the sample using the Java configuration
$>../mvnw -Dtest=FixedLengthFunctionalTests#testLaunchJobWithJavaConfig test
```

View File

@@ -1,9 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:batch="http://www.springframework.org/schema/batch"
xsi:schemaLocation="
http://www.springframework.org/schema/batch https://www.springframework.org/schema/batch/spring-batch.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
<batch:job id="ioSampleJob" xmlns="http://www.springframework.org/schema/batch">
<batch:step id="step1">
<batch:tasklet>
<batch:chunk reader="itemReader" processor="itemProcessor" writer="itemWriter"
commit-interval="2"/>
</batch:tasklet>
</batch:step>
</batch:job>
<bean id="itemProcessor" class="org.springframework.batch.sample.domain.trade.internal.CustomerCreditIncreaseProcessor" />
<bean id="itemReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
<property name="resource" value="#{jobParameters[inputFile]}" />
<property name="lineMapper">