What’s new in Spring Batch 5.0

What’s New in Spring Batch 5.0

Spring Batch 5.0 has the following major themes:spring-doc.cn

Java 17 Requirement

Spring Batch follows Spring Framework’s baselines for both Java version and third party dependencies. With Spring Batch 5, the Spring Framework version is being upgraded to Spring Framework 6, which requires Java 17. As a result, the Java version requirement for Spring Batch is also increasing to Java 17.spring-doc.cn

Major dependencies upgrade

To continue the integration with supported versions of the third party libraries that Spring Batch uses, Spring Batch 5 is updating the dependencies across the board to the following versions:spring-doc.cn

This release also marks the migration to:spring-doc.cn

Batch Infrastructure Configuration Updates

Spring Batch 5 includes the following infrastructure configuration updates:spring-doc.cn

Data Source and Transaction manager Requirement Updates

Historically, Spring Batch provided a map-based job repository and job explorer implementations to work with an in-memory job repository. These implementations were deprecated in version 4 and completely removed in version 5. The recommended replacement is to use the JDBC-based implementations with an embedded database, such as H2, HSQL, and others.spring-doc.cn

In this release, the @EnableBatchProcessing annotation configures a JDBC-based JobRepository, which requires a DataSource and PlatformTransactionManager beans to be defined in the application context. The DataSource bean could refer to an embedded database to work with an in-memory job repository.spring-doc.cn

Transaction Manager Bean Exposure

Until version 4.3, the @EnableBatchProcessing annotation exposed a transaction manager bean in the application context. While this was convenient in many cases, the unconditional exposure of a transaction manager could interfere with a user-defined transaction manager. In this release, @EnableBatchProcessing no longer exposes a transaction manager bean in the application context.spring-doc.cn

New annotation attributes in EnableBatchProcessing

In this release, the @EnableBatchProcessing annotation provides new attributes to specify which components and parameters should be used to configure the Batch infrastructure beans. For example, it is now possible to specify which data source and transaction manager Spring Batch should configure in the job repository as follows:spring-doc.cn

@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();
	}

}

In this example, batchDataSource and batchTransactionManager refer to beans in the application context, and which will be used to configure the job repository and job explorer. There is no need to define a custom BatchConfigurer anymore, which was removed in this release.spring-doc.cn

New configuration class for infrastructure beans

In this release, a new configuration class named DefaultBatchConfiguration can be used as an alternative to using @EnableBatchProcessing for the configuration of infrastructure beans. This class provides infrastructure beans with default configuration which can be customized as needed. The following snippet shows a typical usage of this class:spring-doc.cn

@Configuration
class MyJobConfiguration extends DefaultBatchConfiguration {

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

}

In this example, the JobRepository bean injected in the Job bean definition is defined in the DefaultBatchConfiguration class. Custom parameters can be specified by overriding the corresponding getter. For example, the following example shows how to override the default character encoding used in the job repository and job explorer:spring-doc.cn

@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;
	}
}

Job parameters handling updates

Support for any type as a job parameter

This version adds support to use any type as a job parameter, and not only the 4 pre-defined types (long, double, string, date) as in v4. This change has an impact on how job parameters are persisted in the database (There are no more 4 distinct columns for each predefined type). Please check Column change in BATCH_JOB_EXECUTION_PARAMS for DDL changes. The fully qualified name of the type of the parameter is now persisted as a String, as well as the parameter value. String literals are converted to the parameter type with the standard Spring conversion service. The standard conversion service can be enriched with any required converter to convert user specific types to and from String literals.spring-doc.cn

Default job parameter conversion

The default notation of job parameters in v4 was specified as follows:spring-doc.cn

[+|-]parameterName(parameterType)=value

where parameterType is one of [string,long,double,date]. This notation is limited, constraining, does not play well with environment variables and is not friendly with Spring Boot.spring-doc.cn

In v5, there are two way to specify job parameters:spring-doc.cn

Default notation

The default notation is now specified as follows:spring-doc.cn

parameterName=parameterValue,parameterType,identificationFlag

where parameterType is the fully qualified name of the type of the parameter. Spring Batch provides the DefaultJobParametersConverter to support this notation.spring-doc.cn

Extended notation

While the default notation is well suited for the majority of use cases, it might not be convenient when the value contains a comma for example. In this case, the extended notation can be used, which is inspired by Spring Boot’s Json Application Properties and is specified as follows:spring-doc.cn

parameterName='{"value": "parameterValue", "type":"parameterType", "identifying": "booleanValue"}'

where parameterType is the fully qualified name of the type of the parameter. Spring Batch provides the JsonJobParametersConverter to support this notation.spring-doc.cn

Execution context serialization updates

Starting from v5, the DefaultExecutionContextSerializer was updated to serialize/deserialize the context to/from Base64.spring-doc.cn

Moreover, the default ExecutionContextSerializer configured by @EnableBatchProcessing or DefaultBatchConfiguration was changed from JacksonExecutionContextStringSerializer to DefaultExecutionContextSerializer. The dependency to Jackson was made optional. In order to use the JacksonExecutionContextStringSerializer, jackson-core should be added to the classpath.spring-doc.cn

SystemCommandTasklet updates

The SystemCommandTasklet has been revisited in this release and was changed as follows:spring-doc.cn

  • A new strategy interface named CommandRunner was introduced in order to decouple the command execution from the tasklet execution. The default implementation is the JvmCommandRunner which uses the java.lang.Runtime#exec API to run system commands. This interface can be implemented to use any other API to run system commands.spring-doc.cn

  • The method that runs the command now accepts an array of `String`s representing the command and its arguments. There is no need anymore to tokenize the command or do any pre-processing. This change makes the API more intuitive, and less prone to errors.spring-doc.cn

Batch Testing Configuration Updates

Spring Batch 5 includes the following testing configuration updates:spring-doc.cn

Removal of autowiring from test utilities

Up to version 4.3, the JobLauncherTestUtils and JobRepositoryTestUtils used to autowire the job under test as well as the test datasource to facilitate the testing infrastructure setup. While this was convenient for most use cases, it turned out to cause several issues for test contexts where multiple jobs or multiple data sources are defined.spring-doc.cn

In this release, we introduced a few changes to remove the autowiring of such dependencies in order to avoid any issues while importing those utilities either manually or through the @SpringBatchTest annotation.spring-doc.cn

Migration to JUnit Jupiter

In this release, the entire test suite of Spring Batch has been migrated to JUnit 5. While this does not impact end users directly, it helps the Batch team as well as community contributors to use the next generation of JUnit to write better tests.spring-doc.cn

New features

Transaction support in JobExplorer and JobOperator

This release introduces transaction support in the JobExplorer created through the JobExplorerFactoryBean. It is now possible to specify which transaction manager to use to drive the ready-only transactions when querying the Batch meta-data as well as customizing the transaction attributes.spring-doc.cn

The same transaction support was added to the JobOperator through a new factory bean named JobOperatorFactoryBean.spring-doc.cn

Automatic registration of a JobOperator with EnableBatchProcessing

As of version 4, the EnableBatchProcessing annotation provided all the basic infrastructure beans that are required to launch Spring Batch jobs. However, it did not register a job operator bean, which is the main entry point to stop, restart and abandon job executions.spring-doc.cn

While these utilities are not used as often as launching jobs, adding a job operator automatically in the application context can be useful to avoid a manual configuration of such a bean by end users.spring-doc.cn

Improved Java records support

The support for Java records as items in a chunk-oriented step has initially been introduced in v4.3, but that support was limited due to the fact that v4 has Java 8 as a baseline. The initial support was based on reflection tricks to create Java records and populate them with data, without having access to the java.lang.Record API that was finalised in Java 16.spring-doc.cn

Now that v5 has Java 17 as a baseline, we have improved records support in Spring Batch by leveraging the Record API in different parts of the framework. For example, the FlatFileItemReaderBuilder is now able to detect if the item type is a record or a regular class and configure the corresponding FieldSetMapper implementation accordingly (ie RecordFieldSetMapper for records and BeanWrapperFieldSetMapper for regular classes). The goal here is to make the configuration of the required FieldSetMapper type transparent to the user.spring-doc.cn

Batch tracing with Micrometer

With the upgrade to Micrometer 1.10, you can now get Batch tracing in addition to Batch metrics. Spring Batch will create a span for each job and a span for each step within a job. This tracing meta-data can be collected and viewed on a dashboard like Zipkin for example.spring-doc.cn

Moreover, this release introduces new metrics like the currently active step, as well as the job launch count through the provided JobLauncher.spring-doc.cn

Java 8 features updates

We took the opportunity of this major release to improve the code base with features from Java 8+, for example:spring-doc.cn

Support for SAP HANA a job repository

This release introduces the support of SAP HANA as an additional supported database for the job repository.spring-doc.cn

Full support for MariaDB as a separate product

Up until v4.3, Spring Batch provided support for MariaDB by considering it as MySQL. In this release, MariaDB is treated as an independent product with its own DDL script and DataFieldMaxValueIncrementer.spring-doc.cn

New Maven Bill Of Materials for Spring Batch modules

This feature has been requested several times and is finally shipped in v5. It is now possible to use the newly added Maven BOM to import Spring Batch modules with a consistent version number.spring-doc.cn

UTF-8 by default

Several issues related to characters encoding have been reported over the years in different areas of the framework, like inconsistent default encoding between file-based item readers and writers, serialization/deserialization issues when dealing with multi-byte characters in the execution context, etc.spring-doc.cn

In the same spirit as JEP 400 and following the UTF-8 manifesto, this release updates the default encoding to UTF-8 in all areas of the framework and ensures this default is configurable as needed.spring-doc.cn

Full GraalVM native support

The effort towards providing support to compile Spring Batch applications as native executables using the GraalVM native-image compiler has started in v4.2 and was shipped as experimental in v4.3.spring-doc.cn

In this release, the native support has been improved significantly by providing the necessary runtime hints to natively compile Spring Batch applications with GraalVM and is now considered out of beta.spring-doc.cn

Execution context Meta-data improvement

In addition to what Spring Batch already persists in the execution context with regard to runtime information (like the step type, restart flag, etc), this release adds an important detail in the execution context which is the Spring Batch version that was used to serialize the context.spring-doc.cn

While this seems a detail, it has a huge added value when debugging upgrade issue with regard to execution context serialization and deserialization.spring-doc.cn

Improved documentation

In this release, the documentation was updated to use the Spring Asciidoctor Backend. This backend ensures that all projects from the portfolio follow the same documentation style. For consistency with other projects, the reference documentation of Spring Batch was updated to use this backend in this release.spring-doc.cn

Pruning

Spring Batch 5 removes a number of items that are no longer needed, including:spring-doc.cn

API deprecation and removal

In this major release, all APIs that were deprecated in previous versions have been removed. Moreover, some APIs have been deprecated in v5.0 and are scheduled for removal in v5.2. Finally, some APIs have been moved or removed without deprecation for practical reasons.spring-doc.cn

Please refer to the migration guide for more details about these changes.spring-doc.cn

SQLFire Support Removal

SqlFire has been announced to be EOL as of November 1st, 2014. The support of SQLFire as a job repository was deprecated in version v4.3 and removed in version v5.0.spring-doc.cn

GemFire support removal

Based on the [decision to discontinue](https://github.com/spring-projects/spring-data-geode#notice ) the support of Spring Data for Apache Geode, the support for Geode in Spring Batch was removed. The code was moved to the [spring-batch-extensions](https://github.com/spring-projects/spring-batch-extensions) repository as a community-driven effort.spring-doc.cn

JSR-352 Implementation Removal

Due to a lack of adoption, the implementation of JSR-352 has been discontinued in this release.spring-doc.cn