0%

Spring Batch

There are two types of steps provided by Spring Batch.

  • Tasklet Step
  • Chunk-Oriented Step

There are different options we can launch our job. We can use REST API to trigger job and we can use Spring Scheduler to schedule Spring Batch Job. Also we can stop Job using REST API.

There are different Item Readers provided by Spring Batch.

  • CSV Item Reader
  • JSON Item Reader
  • XML Item Reader
  • JDBC Item Reader
  • REST API Item Reader

There are different Item Writers provided by Spring Batch.

  • CSV Item Writer
  • JSON Item Writer
  • XML Item Writer
  • JDBC Item Writer
  • REST API Item Writer

Spring Batch Provides Item Processor to process data. Item Processor is in between Item Reader & Item Writer.

image-20220517230657681

1.Demo Project

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
@Configuration
public class SampleJob {

@Autowired
private JobBuilderFactory jobBuilderFactory;

@Autowired
private StepBuilderFactory stepBuilderFactory;

@Autowired
private SecondTasklet secondTasklet;

@Bean
public Job firstJob() {
return jobBuilderFactory.get("First Job")
.start(firstStep())
.next(secondStep())
.build();
}

private Step firstStep() {
return stepBuilderFactory.get("First Step")
.tasklet(firstTask())
.build();
}

private Tasklet firstTask() {
return new Tasklet() {

@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
System.out.println("This is first tasklet step");
return RepeatStatus.FINISHED;
}
};
}

private Step secondStep() {
return stepBuilderFactory.get("Second Step")
.tasklet(secondTasklet)
.build();
}

}
1
2
3
4
5
6
7
8
9
10
@Service
public class SecondTasklet implements Tasklet {

@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
System.out.println("This is second tasklet step");
return RepeatStatus.FINISHED;
}

}