对于最新稳定版本,请使用Spring Batch Documentation 6.0.0spring-doc.cadn.net.cn

提交间隔

如前所述,步会读取并写出项目,并定期提交通过使用提供的PlatformTransactionManager. 其中提交间隔1 的在写入每个单独的项目后提交。这在许多情况下并不理想,因为开始和提交事务的成本很高。理想情况下,最好在每个事务中处理尽可能多的项目,这完全取决于正在处理的数据类型以及该步骤交互的资源。因此,你可以配置提交中处理的项目数量。spring-doc.cadn.net.cn

以下示例展示了一个谁的任务小提交间隔根据 Java 定义的 10 值:spring-doc.cadn.net.cn

Java 配置
@Bean
public Job sampleJob(JobRepository jobRepository, Step step1) {
    return new JobBuilder("sampleJob", jobRepository)
                     .start(step1)
                     .build();
}

@Bean
public Step step1(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
	return new StepBuilder("step1", jobRepository)
				.<String, String>chunk(10, transactionManager)
				.reader(itemReader())
				.writer(itemWriter())
				.build();
}

以下示例展示了一个谁的任务小提交间隔在XML中定义的值为10:spring-doc.cadn.net.cn

XML 配置
<job id="sampleJob">
    <step id="step1">
        <tasklet>
            <chunk reader="itemReader" writer="itemWriter" commit-interval="10"/>
        </tasklet>
    </step>
</job>

在上述例子中,每笔交易中处理10项。在处理开始时,开始了一项交易。此外,每次ItemReader计数器被递增。当计数器达到10时,聚合项列表传递给ItemWriter,交易已提交。spring-doc.cadn.net.cn