# Spring Batch - Spring Boot runs `schema-@@platform@@.sql` automatically during startup. - `-all` is the default for all platforms. ## schema-all.sql ```sql DROP TABLE people IF EXISTS; CREATE TABLE people ( person_id BIGINT IDENTITY NOT NULL PRIMARY KEY, first_name VARCHAR(20), last_name VARCHAR(20) ); ``` ``` Jill,Doe Joe,Doe Justin,Doe Jane,Doe John,Doe ``` ## Dependency ```groovy implementation 'org.springframework.boot:spring-boot-starter-batch' runtimeOnly 'org.hsqldb:hsqldb' ``` ## Code - Intermediate Processor ```java import org.springframework.batch.item.ItemProcessor; public class PersonItemProcessor implements ItemProcessor { } ``` - Batch Job ```java @Configuration @EnableBatchProcessing public class BatchConfiguration { @Autowired public JobBuilderFactory jobBuilderFactory; @Autowired public StepBuilderFactory stepBuilderFactory; ... } ``` ```java @Bean public FlatFileItemReader reader() { return new FlatFileItemReaderBuilder() .name("personItemReader") .resource(new ClassPathResource("sample-data.csv")) .delimited() .names(new String[]{"firstName", "lastName"}) .fieldSetMapper(new BeanWrapperFieldSetMapper() {{ setTargetType(Person.class); }}) .build(); } @Bean public PersonItemProcessor processor() { return new PersonItemProcessor(); } @Bean public JdbcBatchItemWriter writer(DataSource dataSource) { return new JdbcBatchItemWriterBuilder() .itemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>()) .sql("INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)") .dataSource(dataSource) .build(); } ``` - Job listener - The `JobCompletionNotificationListener` listens for when a job is BatchStatus.COMPLETED and then uses JdbcTemplate to inspect the results. ```java @Component public class JobCompletionNotificationListener extends JobExecutionListenerSupport { private static final Logger log = LoggerFactory.getLogger(JobCompletionNotificationListener.class); private final JdbcTemplate jdbcTemplate; @Autowired public JobCompletionNotificationListener(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } @Override public void afterJob(JobExecution jobExecution) { if(jobExecution.getStatus() == BatchStatus.COMPLETED) { log.info("!!! JOB FINISHED! Time to verify the results"); jdbcTemplate.query("SELECT first_name, last_name FROM people", (rs, row) -> new Person( rs.getString(1), rs.getString(2)) ).forEach(person -> log.info("Found <" + person + "> in the database.")); } } } ``` ## ref - https://spring.io/guides/gs/batch-processing/