|
| 1 | +package demo; |
| 2 | + |
| 3 | +import javax.sql.DataSource; |
| 4 | + |
| 5 | +import org.springframework.batch.core.Job; |
| 6 | +import org.springframework.batch.core.Step; |
| 7 | +import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; |
| 8 | +import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; |
| 9 | +import org.springframework.batch.core.launch.support.RunIdIncrementer; |
| 10 | +import org.springframework.batch.item.ItemProcessor; |
| 11 | +import org.springframework.batch.item.ItemReader; |
| 12 | +import org.springframework.batch.item.ItemWriter; |
| 13 | +import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider; |
| 14 | +import org.springframework.batch.item.database.JdbcBatchItemWriter; |
| 15 | +import org.springframework.batch.item.file.FlatFileItemReader; |
| 16 | +import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper; |
| 17 | +import org.springframework.batch.item.file.mapping.DefaultLineMapper; |
| 18 | +import org.springframework.batch.item.file.transform.DelimitedLineTokenizer; |
| 19 | +import org.springframework.beans.factory.annotation.Autowired; |
| 20 | +import org.springframework.boot.autoconfigure.batch.JobExecutionEvent; |
| 21 | +import org.springframework.context.ApplicationListener; |
| 22 | +import org.springframework.context.annotation.Bean; |
| 23 | +import org.springframework.context.annotation.Configuration; |
| 24 | +import org.springframework.core.io.ClassPathResource; |
| 25 | +import org.springframework.jdbc.core.JdbcTemplate; |
| 26 | +import org.springframework.stereotype.Component; |
| 27 | + |
| 28 | +@Configuration |
| 29 | +public class ContactBatchJobConfiguration { |
| 30 | + |
| 31 | + @Bean |
| 32 | + public ItemProcessor<Contact, Contact> processor() { |
| 33 | + return (person) -> new Contact(person.getFirstName(), person.getLastName(), |
| 34 | + person.getEmail().toLowerCase()); |
| 35 | + } |
| 36 | + |
| 37 | + @Bean |
| 38 | + public ItemReader<Contact> reader() { |
| 39 | + FlatFileItemReader<Contact> reader = new FlatFileItemReader<>(); |
| 40 | + reader.setResource(new ClassPathResource("data.csv")); |
| 41 | + reader.setLineMapper(new DefaultLineMapper<Contact>() { |
| 42 | + { |
| 43 | + setLineTokenizer(new DelimitedLineTokenizer() { |
| 44 | + { |
| 45 | + setNames(new String[] { "firstName", "lastName", "email" }); |
| 46 | + } |
| 47 | + }); |
| 48 | + setFieldSetMapper(new BeanWrapperFieldSetMapper<Contact>() { |
| 49 | + { |
| 50 | + setTargetType(Contact.class); |
| 51 | + } |
| 52 | + }); |
| 53 | + } |
| 54 | + }); |
| 55 | + return reader; |
| 56 | + } |
| 57 | + |
| 58 | + @Bean |
| 59 | + public ItemWriter<Contact> writer(DataSource dataSource) { |
| 60 | + JdbcBatchItemWriter<Contact> writer = new JdbcBatchItemWriter<>(); |
| 61 | + writer.setItemSqlParameterSourceProvider( |
| 62 | + new BeanPropertyItemSqlParameterSourceProvider<>()); |
| 63 | + writer.setSql("INSERT INTO contact (first_name, last_name, email) " |
| 64 | + + "VALUES (:firstName, :lastName, :email)"); |
| 65 | + writer.setDataSource(dataSource); |
| 66 | + return writer; |
| 67 | + } |
| 68 | + |
| 69 | + @Bean |
| 70 | + public Job importUserJob(JobBuilderFactory jobs, Step s1) { |
| 71 | + return jobs.get("importUserJob").incrementer(new RunIdIncrementer()).flow(s1) |
| 72 | + .end().build(); |
| 73 | + } |
| 74 | + |
| 75 | + @Bean |
| 76 | + public Step step1(StepBuilderFactory stepBuilderFactory, ItemReader<Contact> reader, |
| 77 | + ItemWriter<Contact> writer, ItemProcessor<Contact, Contact> processor) { |
| 78 | + return stepBuilderFactory.get("step1").<Contact, Contact>chunk(10).reader(reader) |
| 79 | + .processor(processor).writer(writer).build(); |
| 80 | + } |
| 81 | + |
| 82 | + @Bean |
| 83 | + public JdbcTemplate jdbcTemplate(DataSource dataSource) { |
| 84 | + return new JdbcTemplate(dataSource); |
| 85 | + } |
| 86 | + |
| 87 | + @Component |
| 88 | + public static class BatchJobFinishedListener |
| 89 | + implements ApplicationListener<JobExecutionEvent> { |
| 90 | + |
| 91 | + @Autowired |
| 92 | + private JdbcTemplate jdbcTemplate; |
| 93 | + |
| 94 | + @Override |
| 95 | + public void onApplicationEvent(JobExecutionEvent event) { |
| 96 | + System.out.println("finished " + event.getJobExecution().toString()); |
| 97 | + jdbcTemplate |
| 98 | + .query("SELECT first_name, last_name, email FROM contact", |
| 99 | + (rs, i) -> new Contact(rs.getString("first_name"), |
| 100 | + rs.getString("last_name"), rs.getString("email"))) |
| 101 | + .forEach(System.out::println); |
| 102 | + } |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments