防止狀態持久化

預設情況下,所有 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 中建立任何條目。

© . This site is unofficial and not affiliated with VMware.