Table of contents
Requirements: Practice the RepeatStatus status above
introduction
Followed by the previous article: Spring Batch batch processing-execution context . After understanding the job execution context, this article will take a look at the Spring Batch batch processing step object and the task processing object Tasklet, and see how Spring Batch works.
step introduction
It is generally considered that a step is an independent functional component, because it contains all the content required by a unit of work, such as: input module, output module, data processing module, etc. What are the benefits of this design? The answer is: to bring developers more freedom to operate.
Currently Spring Batch supports 2 step processing modes:
-
Simple in the Tasklet processing mode
This mode is relatively simple, and all the above are about batch processing in this mode
@Bean
public Tasklet tasklet(){
return new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
System.out.println("Hello SpringBatch....");
return RepeatStatus.FINISHED;
}
};
}
You only need to implement the Tasklet interface to build a step code block. Loop the step logic until the tasklet.execute method returns RepeatStatus.FINISHED
-
In the block (chunk) processing mode
The block-based steps generally contain 2 or 3 components: 1>ItemReader 2>ItemProcessor (optional) 3>ItemWriter. After using these components, Spring Batch will process the data in blocks.
Simple Tasklet
After learning this, we have written a lot of simple Tasklet mode steps, but we haven't had a deep understanding of them. In this section, we will analyze the use of Tasklet steps in detail.
First look at the Tasklet source code
public interface Tasklet {
@Nullable
RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception;
}
The Tasklet interface has one and only one method: execute,
There are 2 parameters:
StepContribution: step information object, used to save the current step execution information, core usage: set step result status contribution.setExitStatus(ExitStatus status)
contribution.setExitStatus(ExitStatus.COMPLETED);
ChunkContext: The chuck context is the same as the previously learned StepContext JobContext, the difference is that it is used to record chunk block execution scenarios. Through it, the first two objects can be obtained.
Return value 1:
RepeatStatus: The status of the current step. It is an enumeration class with 2 values. One indicates that the execute method can be executed cyclically, and the other indicates that the execution has ended.
public enum RepeatStatus {
/**
* 当前步骤依然可以执行,如果步骤返回该值,会一直循环执行
*/
CONTINUABLE(true),
/**
* 当前步骤结束,可以为成功也可以表示不成,仅代表当前step执行结束了
*/
FINISHED(false);
}
Requirements: Practice the RepeatStatus status above
@SpringBootApplication
@EnableBatchProcessing
public class SimpleTaskletJob {
@Autowired
private JobLauncher jobLauncher;
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Bean
public Tasklet tasklet(){
return new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
System.out.println("------>" + System.currentTimeMillis());
//return RepeatStatus.CONTINUABLE; //循环执行
return RepeatStatus.FINISHED;
}
};
}
@Bean
public Step step1(){
return stepBuilderFactory.get("step1")
.tasklet(tasklet())
.build();
}
//定义作业
@Bean
public Job job(){
return jobBuilderFactory.get("step-simple-tasklet-job")
.start(step1())
.build();
}
public static void main(String[] args) {
SpringApplication.run(SimpleTaskletJob.class, args);
}
}
After running, return RepeatStatus.FINISHED; will print normally, and return RepeatStatus.CONTINUABLE; will keep looping.
At this point, this article is over. If you want to know what will happen next, please listen to the next chapter to break it down~
Turn to video version
If you are not addicted to reading text, you can switch to the video version: Spring Batch efficient batch processing framework in practice