Spring Batch Get Started
Spring Batch
Spring Batch provides batch processing, large volumes of records.
I tried this on Spring-Boot 1.3.5
The Main features are following
- Transaction management
- Chunk based processing
- Declarative I/O
- Start/Stop/Restart
- Retry/Skip
- Web based administration interface(Spring Batch Admin)
※ These are from Spring Documentation
Ref
These are nice web pages as reference
01.Spring Batchの基本概念(ステップ)
4. Configuring and Running a Job
Technical Words
| Name | Description |
|---|---|
| job | 1 batch 1 process. job is a unit of batch |
| step | batch process unit. job is composed of multiple steps. step has unit(ItemReader, ItemProcessor, ItemWriter) |
| ItemReader | |
| ItemProcessor | |
| ItemWriter | |
| JobInstance | JobInstance has Job unit, job name, run parameters |
| JobExecution | Model which has Job result |
| StepExecution | Model which has Step results |
Get Started
Install dependency
compile('org.springframework.boot:spring-boot-starter-batch')
※ 1.4.0 : jobBuildFactory requires dataSource instance.
compile("org.hsqldb:hsqldb")
To make it easy, add above dependency.
Configuration
Batch related things should be in configuration
The key is @Configuration, @EnableBatchProcessing
And in Spring Batch code there are 2 factory to create job and step
- JobBuilderFactory
- StepBuilderFactory
Step
In this time, I use simple tasklet. tasklet is simple task item
We have another choice to use chunk.
For the first step, I use tasklet.
Sample
This sample uses only Configuration and Main
BatchdemoApplication.java
@SpringBootApplication
public class BatchdemoApplication {
public static void main(String[] args) {
SpringApplication.run(BatchdemoApplication.class, args);
//System.exit(SpringApplication.exit(SpringApplication.run(BatchConfiguration.class, args)));
}
}
BatchConfiguration.java
@Configuration
@EnableBatchProcessing
@EnableAutoConfiguration
public class BatchConfiguration {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Bean
public Step step1() {
return stepBuilderFactory.get("step1")
.tasklet(new Tasklet(){
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) {
return RepeatStatus.FINISHED;
}
})
.build();
}
@Bean
public Step step2() {
return stepBuilderFactory.get("step2")
.tasklet(new Tasklet(){
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) {
return RepeatStatus.FINISHED;
}
})
.build();
}
@Bean
public Job job(Step step1, Step step2) throws Exception {
return jobBuilderFactory.get("job1")
.incrementer(new RunIdIncrementer())
.start(step1)
.next(step2)
.build();
}
}
Run 2 steps using next.
Use mysql as dataSource
Add following dependency
compile("mysql:mysql-connector-java:5.1.40")
application.yml(src/main/resources)
spring:
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost/test
username: username
password: password

