防止状态持久化
默认情况下,所有ItemReader
和ItemWriter
实现都会在提交前将其当前状态存储在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
中进行任何条目。