Use builders for reader/writers
This commit is contained in:
parent
e8ac004972
commit
1991d56d1b
@ -11,7 +11,9 @@ import org.springframework.batch.core.configuration.annotation.StepBuilderFactor
|
|||||||
import org.springframework.batch.core.launch.support.RunIdIncrementer;
|
import org.springframework.batch.core.launch.support.RunIdIncrementer;
|
||||||
import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
|
import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
|
||||||
import org.springframework.batch.item.database.JdbcBatchItemWriter;
|
import org.springframework.batch.item.database.JdbcBatchItemWriter;
|
||||||
|
import org.springframework.batch.item.database.builder.JdbcBatchItemWriterBuilder;
|
||||||
import org.springframework.batch.item.file.FlatFileItemReader;
|
import org.springframework.batch.item.file.FlatFileItemReader;
|
||||||
|
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
|
||||||
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
|
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
|
||||||
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
|
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
|
||||||
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
|
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
|
||||||
@ -31,23 +33,18 @@ public class BatchConfiguration {
|
|||||||
@Autowired
|
@Autowired
|
||||||
public StepBuilderFactory stepBuilderFactory;
|
public StepBuilderFactory stepBuilderFactory;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
public DataSource dataSource;
|
|
||||||
|
|
||||||
// tag::readerwriterprocessor[]
|
// tag::readerwriterprocessor[]
|
||||||
@Bean
|
@Bean
|
||||||
public FlatFileItemReader<Person> reader() {
|
public FlatFileItemReader<Person> reader() {
|
||||||
FlatFileItemReader<Person> reader = new FlatFileItemReader<Person>();
|
return new FlatFileItemReaderBuilder<Person>()
|
||||||
reader.setResource(new ClassPathResource("sample-data.csv"));
|
.name("personItemReader")
|
||||||
reader.setLineMapper(new DefaultLineMapper<Person>() {{
|
.resource(new ClassPathResource("sample-data.csv"))
|
||||||
setLineTokenizer(new DelimitedLineTokenizer() {{
|
.delimited()
|
||||||
setNames(new String[] { "firstName", "lastName" });
|
.names(new String[]{"firstName", "lastName"})
|
||||||
}});
|
.fieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {{
|
||||||
setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {{
|
|
||||||
setTargetType(Person.class);
|
setTargetType(Person.class);
|
||||||
}});
|
}})
|
||||||
}});
|
.build();
|
||||||
return reader;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ -56,33 +53,33 @@ public class BatchConfiguration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public JdbcBatchItemWriter<Person> writer() {
|
public JdbcBatchItemWriter<Person> writer(DataSource dataSource) {
|
||||||
JdbcBatchItemWriter<Person> writer = new JdbcBatchItemWriter<Person>();
|
return new JdbcBatchItemWriterBuilder<Person>()
|
||||||
writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Person>());
|
.itemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>())
|
||||||
writer.setSql("INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)");
|
.sql("INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)")
|
||||||
writer.setDataSource(dataSource);
|
.dataSource(dataSource)
|
||||||
return writer;
|
.build();
|
||||||
}
|
}
|
||||||
// end::readerwriterprocessor[]
|
// end::readerwriterprocessor[]
|
||||||
|
|
||||||
// tag::jobstep[]
|
// tag::jobstep[]
|
||||||
@Bean
|
@Bean
|
||||||
public Job importUserJob(JobCompletionNotificationListener listener) {
|
public Job importUserJob(JobCompletionNotificationListener listener, Step step1) {
|
||||||
return jobBuilderFactory.get("importUserJob")
|
return jobBuilderFactory.get("importUserJob")
|
||||||
.incrementer(new RunIdIncrementer())
|
.incrementer(new RunIdIncrementer())
|
||||||
.listener(listener)
|
.listener(listener)
|
||||||
.flow(step1())
|
.flow(step1)
|
||||||
.end()
|
.end()
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public Step step1() {
|
public Step step1(JdbcBatchItemWriter<Person> writer) {
|
||||||
return stepBuilderFactory.get("step1")
|
return stepBuilderFactory.get("step1")
|
||||||
.<Person, Person> chunk(10)
|
.<Person, Person> chunk(10)
|
||||||
.reader(reader())
|
.reader(reader())
|
||||||
.processor(processor())
|
.processor(processor())
|
||||||
.writer(writer())
|
.writer(writer)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
// end::jobstep[]
|
// end::jobstep[]
|
||||||
|
@ -1,18 +1,12 @@
|
|||||||
package hello;
|
package hello;
|
||||||
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import org.springframework.batch.core.BatchStatus;
|
import org.springframework.batch.core.BatchStatus;
|
||||||
import org.springframework.batch.core.JobExecution;
|
import org.springframework.batch.core.JobExecution;
|
||||||
import org.springframework.batch.core.listener.JobExecutionListenerSupport;
|
import org.springframework.batch.core.listener.JobExecutionListenerSupport;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
import org.springframework.jdbc.core.RowMapper;
|
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
@ -32,17 +26,11 @@ public class JobCompletionNotificationListener extends JobExecutionListenerSuppo
|
|||||||
if(jobExecution.getStatus() == BatchStatus.COMPLETED) {
|
if(jobExecution.getStatus() == BatchStatus.COMPLETED) {
|
||||||
log.info("!!! JOB FINISHED! Time to verify the results");
|
log.info("!!! JOB FINISHED! Time to verify the results");
|
||||||
|
|
||||||
List<Person> results = jdbcTemplate.query("SELECT first_name, last_name FROM people", new RowMapper<Person>() {
|
jdbcTemplate.query("SELECT first_name, last_name FROM people",
|
||||||
@Override
|
(rs, row) -> new Person(
|
||||||
public Person mapRow(ResultSet rs, int row) throws SQLException {
|
rs.getString(1),
|
||||||
return new Person(rs.getString(1), rs.getString(2));
|
rs.getString(2))
|
||||||
}
|
).forEach(person -> log.info("Found <" + person + "> in the database."));
|
||||||
});
|
|
||||||
|
|
||||||
for (Person person : results) {
|
|
||||||
log.info("Found <" + person + "> in the database.");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
package hello;
|
package hello;
|
||||||
|
|
||||||
public class Person {
|
public class Person {
|
||||||
|
|
||||||
private String lastName;
|
private String lastName;
|
||||||
private String firstName;
|
private String firstName;
|
||||||
|
|
||||||
public Person() {
|
public Person() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Person(String firstName, String lastName) {
|
public Person(String firstName, String lastName) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user