防止状态持久化

默认情况下,所有 ItemReaderItemWriter 实现都会在提交之前将其当前状态存储在 ExecutionContext 中。但是,这可能并不总是期望的行为。例如,许多开发人员选择通过使用进程指示器来使他们的数据库读取器“可重新运行”。在输入数据中添加了一个额外的列来指示它是否已被处理。当读取(或写入)特定记录时,已处理标志将从 false 翻转到 true。然后,SQL 语句可以在 where 子句中包含一个额外的语句,例如 where PROCESSED_IND = false,从而确保在重新启动的情况下仅返回未处理的记录。在这种情况下,最好不要存储任何状态,例如当前行号,因为它在重新启动时无关紧要。出于这个原因,所有读取器和写入器都包含“saveState”属性。

  • Java

  • XML

以下 Bean 定义显示了如何在 Java 中阻止状态持久化

Java 配置
@Bean
public JdbcCursorItemReader playerSummarizationSource(DataSource dataSource) {
	return new JdbcCursorItemReaderBuilder<PlayerSummary>()
				.dataSource(dataSource)
				.rowMapper(new PlayerSummaryMapper())
				.saveState(false)
				.sql("SELECT games.player_id, games.year_no, SUM(COMPLETES),"
				  + "SUM(ATTEMPTS), SUM(PASSING_YARDS), SUM(PASSING_TD),"
				  + "SUM(INTERCEPTIONS), SUM(RUSHES), SUM(RUSH_YARDS),"
				  + "SUM(RECEPTIONS), SUM(RECEPTIONS_YARDS), SUM(TOTAL_TD)"
				  + "from games, players where players.player_id ="
				  + "games.player_id group by games.player_id, games.year_no")
				.build();

}

以下 Bean 定义显示了如何在 XML 中阻止状态持久化

XML 配置
<bean id="playerSummarizationSource" class="org.spr...JdbcCursorItemReader">
    <property name="dataSource" ref="dataSource" />
    <property name="rowMapper">
        <bean class="org.springframework.batch.samples.PlayerSummaryMapper" />
    </property>
    <property name="saveState" value="false" />
    <property name="sql">
        <value>
            SELECT games.player_id, games.year_no, SUM(COMPLETES),
            SUM(ATTEMPTS), SUM(PASSING_YARDS), SUM(PASSING_TD),
            SUM(INTERCEPTIONS), SUM(RUSHES), SUM(RUSH_YARDS),
            SUM(RECEPTIONS), SUM(RECEPTIONS_YARDS), SUM(TOTAL_TD)
            from games, players where players.player_id =
            games.player_id group by games.player_id, games.year_no
        </value>
    </property>
</bean>

上面配置的 ItemReader 不会在它参与的任何执行中在 ExecutionContext 中进行任何条目。