Spring Batch 5.0.0-M6 和 4.3.7 發布了!

發布 | Mahmoud Ben Hassine | 2022 年 9 月 22 日 | ...

我很高興地宣布 Spring Batch 5.0.0-M6 現在可以從我們的 里程碑儲存庫 取得,而 4.3.7 可以從 Maven Central 取得。版本 4.3.7 是一個修補版本,可以用來取代 4.3.6。您可以在此處找到其發布說明。這篇部落格文章主要關於新的里程碑版本 5.0.0-M6。在這個里程碑中,我們專注於改進 Spring Batch 的配置流程,使其更靈活和直接。這篇部落格文章將介紹框架中這個領域的主要變更,並介紹此里程碑版本中引入的新功能。如需完整的變更清單,請參閱發布說明

EnableBatchProcessing 中新的註解屬性

在此版本中,@EnableBatchProcessing 註解引入了新的屬性,用於指定哪些元件和參數應用於配置 Batch 基礎架構 bean。 例如,您現在可以指定 Spring Batch 應該在作業儲存庫中配置哪個資料來源和交易管理器。 以下程式碼片段展示了這種配置的新方法

@Configuration
@EnableBatchProcessing(dataSourceRef = "batchDataSource", transactionManagerRef = "batchTransactionManager")
public class MyJobConfiguration {

	@Bean
	public Job job(JobRepository jobRepository) {
		return new JobBuilder("myJob", jobRepository)
			 //define job flow as needed
			 .build();
	}

}

在此範例中,batchDataSourcebatchTransactionManager 指的是應用程式上下文中的 bean,用於配置作業儲存庫和作業探索器。您不再需要定義自訂的 BatchConfiguer,這個介面已在此版本中移除。例如,在 Spring Batch v4 中,可以透過提供自訂的 BatchConfigurer 來提供自訂的執行上下文序列化器,如下所示

@Configuration
@EnableBatchProcessing
public class MyJobConfigWithCustomSerializer {

    @Bean
    public BatchConfigurer batchConfigurer() {
        return new DefaultBatchConfigurer() {
            @Override
            public JobRepository getJobRepository() {
                JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
                factory.setSerializer(createCustomSerializer());
                // set other properties on the factory bean
                try {
                    factory.afterPropertiesSet();
                    return factory.getObject();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }

            @Override
            public JobExplorer getJobExplorer() {
                JobExplorerFactoryBean factoryBean = new JobExplorerFactoryBean();
                factoryBean.setSerializer(createCustomSerializer());
                // set other properties on the factory bean
                try {
                    factoryBean.afterPropertiesSet();
                    return factoryBean.getObject();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }

            private ExecutionContextSerializer createCustomSerializer() {
                Jackson2ExecutionContextStringSerializer serializer = new Jackson2ExecutionContextStringSerializer();
                // customize serializer
                return serializer;
            }
        };
    }

}

在 Spring Batch v5 中,您可以提供自訂的序列化器,如下所示

@Configuration
@EnableBatchProcessing(executionContextSerializerRef = "myCustomSerializer")
public class MyJobConfigWithCustomSerializer {

    @Bean
    public Job job(JobRepository jobRepository) {
        return new JobBuilder("myJob", jobRepository)
                //define job flow as needed
                .build();
    }
    
    @Bean
    public ExecutionContextSerializer myCustomSerializer() {
        Jackson2ExecutionContextStringSerializer serializer = new Jackson2ExecutionContextStringSerializer();
        // customize serializer
        return serializer;
    }

}

我們相信這種配置 Spring Batch 的新方法更直觀、更直接,且更不容易出錯。

基礎架構 Bean 的新配置類別

在此版本中,您可以使用名為 DefaultBatchConfiguration 的新配置類別,作為使用 @EnableBatchProcessing 配置基礎架構 bean 的替代方案。此類別提供具有預設配置的基礎架構 bean,您可以根據需要進行自訂。以下程式碼片段展示了此類別的典型用法

@Configuration
class MyJobConfiguration extends DefaultBatchConfiguration {

	@Bean
	public Job job(JobRepository jobRepository) {
		return new JobBuilder("myJob", jobRepository)
				//define job flow as needed
				.build();
	}

}

在此範例中,注入到 Job bean 定義中的 JobRepository bean 在 DefaultBatchConfiguration 類別中定義。您可以透過覆寫相應的 getter 來指定自訂參數。例如,以下範例顯示如何覆寫作業儲存庫和作業探索器中使用的預設字元編碼

@Configuration
class MyJobConfiguration extends DefaultBatchConfiguration {

	@Bean
	public Job job(JobRepository jobRepository) {
		return new JobBuilder("job", jobRepository)
				// define job flow as needed
				.build();
	}

	@Override
	protected Charset getCharset() {
		return StandardCharsets.ISO_8859_1;
	}
}

JobExplorer 和 JobOperator 中的交易支援

此版本在透過 JobExplorerFactoryBean 建立的 JobExplorer 中引入了交易支援。您現在可以指定使用哪個交易管理器來驅動查詢 Batch 元資料時的唯讀交易。此外,您現在可以自訂交易屬性。相同的交易支援已透過一個名為 JobOperatorFactoryBean 的新 factory bean 添加到 JobOperator 中。

棄用和 API 變更

此里程碑版本引入了以下棄用和 API 變更

  • JobBuilderFactoryStepBuilderFactory 現在已被棄用。相反地,您應該使用 JobBuilderStepBuilder
  • BatchConfigurerDefaultBatchConfigurer 已被移除。您不應再使用此介面及其預設實作來自訂 @EnableBatchProcessing 的行為

相依性升級

主要相依性已升級到以下版本

  • 升級到 Spring Framework 6.0.0-M6
  • 升級到 Spring Data 2022.0.0-M6
  • 升級到 Spring Integration 6.0.0-M5
  • 升級到 Spring AMQP 3.0.0-M4
  • 升級到 Spring for Apache Kafka 3.0.0-M6
  • 升級到 Spring Retry 2.0.0-M1
  • 升級到 Spring LDAP 3.0.0-M4
  • 升級到 Micrometer 1.10.0-M5

回饋

我要感謝所有參與此版本的貢獻者!當我們繼續 Spring Batch 5 的工作時,我們期待您在 GithubTwitterStackOverflow 上提供您的回饋。


Spring Batch 首頁|Github 上的原始碼|參考文件

取得 Spring 電子報

與 Spring 電子報保持聯繫

訂閱

領先一步

VMware 提供訓練和認證,以加速您的進度。

瞭解更多

取得支援

Tanzu Spring 在一個簡單的訂閱中提供 OpenJDK™、Spring 和 Apache Tomcat® 的支援和二進位檔。

瞭解更多

即將到來的活動

查看 Spring 社群中所有即將到來的活動。

查看全部