我正在学习如何使用SpringBatch
撰写SpringBoot
。
示例我的春季批次如下所示
<batch:job id="job" >
<batch:step id="deleteFile" next="generateFile">
<batch:tasklet ref="fileDeleting" />
</batch:step>
<batch:step id="generateFile" next="emailFile" >
<batch:tasklet transaction-manager="transactionManager">
<batch:chunk reader="reader" writer="writer" processor="processor" commit-interval="10" />
</batch:tasklet>
</batch:step>
<batch:step id="emailFile">
<batch:tasklet ref="emailTasklet" />
</batch:step>
<batch:listeners>
<batch:listener ref="jobListener" />
</batch:listeners>
</batch:job>
正如您所看到的,在读者被调用之前,将首先调用deleteFile
。完成generateFile
后,emailFile
将被调用。
这就是generateFile
转向SpringBoot时的样子。
@Configuration
public class BatchConfig {
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
public StepBuilderFactory stepBuilderFactory;
@Bean
public Job job() {
return jobBuilderFactory.get("job")
.incrementer(new RunIdIncrementer())
.flow(step1())
.end()
.build();
}
@Bean
public Step step1() {
System.out.println("In step1");
return stepBuilderFactory.get("step1")
.<String, String>chunk(1)
.reader(new Reader())
.processor(new Processor())
.writer(new Writer())
.build();
}
}
我如何让deleteFile
和emailFile
参与SpringBoot
?
答案 0 :(得分:1)
您可以像面向项目的步骤一样声明tasklet步骤,例如:
@Configuration
public class BatchConfig {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Bean
public Step step1() {
return stepBuilderFactory.get("step1")
.tasklet(deleteFileTasklet())
.build();
}
@Bean
public Step step2() {
return stepBuilderFactory.get("step2")
.<String, String>chunk(1)
.reader(new Reader())
.processor(new Processor())
.writer(new Writer())
.build();
}
@Bean
public Step step3() {
return stepBuilderFactory.get("step3")
.tasklet(emailTasklet())
.build();
}
@Bean
@StepScope
public Tasklet deleteFileTasklet() {
return null; // TODO implement delete file tasklet
}
@Bean
@StepScope
public Tasklet emailTasklet() {
return null; // TODO implement email tasklet
}
@Bean
public Job job() {
return jobBuilderFactory.get("job")
.incrementer(new RunIdIncrementer())
.start(step1())
.next(step2())
.next(step3())
.build();
}
}
我希望这会有所帮助。