Configuring a Step

  • reader: 提供要处理的项的项目读取器。

  • writer: 处理项目读取器提供的项的项目写入器。

尽管必需依赖项列表较短,但 Step 类是一个复杂的类,可能包含许多协作者,例如:

  • transactionManager: 开始和提交处理期间事务的事务管理器。

  • repository: 定期存储 StepExecution 和 ExecutionContext 的存储库。

  • chunk: 在提交事务之前要处理的项数。

尽管 Step 的必需依赖项列表相对较短,但它是一个可能包含许多协作者的极其复杂的类。

Despite the relatively short list of required dependencies for a Step, it is an extremely complex class that can potentially contain many collaborators.

Java

在使用 Java 配置时,您可以使用 Spring Batch 构建器,如下例所示:

When using Java configuration, you can use the Spring Batch builders, as the following example shows:

Java Configuration
/**
 * Note the JobRepository is typically autowired in and not needed to be explicitly
 * configured
 */
@Bean
public Job sampleJob(JobRepository jobRepository, Step sampleStep) {
    return new JobBuilder("sampleJob", jobRepository)
                .start(sampleStep)
                .build();
}

/**
 * Note the TransactionManager is typically autowired in and not needed to be explicitly
 * configured
 */
@Bean
public Step sampleStep(JobRepository jobRepository, (2)
		PlatformTransactionManager transactionManager) { (1)
	return new StepBuilder("sampleStep", jobRepository)
				.<String, String>chunk(10, transactionManager) (3)
				.reader(itemReader())
				.writer(itemWriter())
				.build();
}
1 transactionManager: Spring’s PlatformTransactionManager that begins and commits transactions during processing.
2 repository: The Java-specific name of the JobRepository that periodically stores the StepExecution and ExecutionContext during processing (just before committing).
3 chunk: The Java-specific name of the dependency that indicates that this is an item-based step and the number of items to be processed before the transaction is committed.

请注意,repository 默认为 jobRepository(通过 @EnableBatchProcessing 提供),transactionManager 默认为 transactionManager(从应用程序上下文中提供)。此外,ItemProcessor 是可选的,因为可以将项目可以直接从读取器传递到写入器。

Note that repository defaults to jobRepository (provided through @EnableBatchProcessing) and transactionManager defaults to transactionManager (provided from the application context). Also, the ItemProcessor is optional, since the item could be directly passed from the reader to the writer.

XML

为了简化配置,您可以使用 Spring Batch XML 命名空间,如下例所示:

To ease configuration, you can use the Spring Batch XML namespace, as the following example shows:

XML Configuration
<job id="sampleJob" job-repository="jobRepository"> (2)
    <step id="step1">
        <tasklet transaction-manager="transactionManager"> (1)
            <chunk reader="itemReader" writer="itemWriter" commit-interval="10"/> (3)
        </tasklet>
    </step>
</job>
1 transaction-manager: Spring’s PlatformTransactionManager that begins and commits transactions during processing.
2 job-repository: The XML-specific name of the JobRepository that periodically stores the StepExecution and ExecutionContext during processing (just before committing). For an in-line <step/> (one defined within a <job/>), it is an attribute on the <job/> element. For a standalone <step/>, it is defined as an attribute of the <tasklet/>.
3 commit-interval: The XML-specific name of the number of items to be processed before the transaction is committed.

请注意,job-repository 默认为 jobRepositorytransaction-manager 默认为 transactionManager。此外,ItemProcessor 是可选的,因为可以将该项直接从读者直接传递给写作者。

Note that job-repository defaults to jobRepository and transaction-manager defaults to transactionManager. Also, the ItemProcessor is optional, since the item could be directly passed from the reader to the writer.

前述配置包括创建面向项目的步骤所需的唯一必需依赖项:

The preceding configuration includes the only required dependencies to create a item-oriented step:

  • reader: The ItemReader that provides items for processing.

  • writer: The ItemWriter that processes the items provided by the ItemReader.