What’s New in Spring Batch 5.1
本节展示了 Spring Batch 5.1 的主要亮点。有关完整更改列表,请参阅 release notes。
This section shows the major highlights of Spring Batch 5.1. For the complete list of changes, please refer to the release notes.
Spring Batch 5.1 包含以下特性:
Spring Batch 5.1 includes the following features:
此外,此版本还引入了以下实验特性:
Moreover, this release introduces the following experimental features:
Dependencies upgrade
在此版本中,Spring 依赖项已升级到以下版本:
In this release, the Spring dependencies are upgraded to the following versions:
-
Spring Framework 6.1.0
-
Spring Integration 6.2.0
-
Spring Data 3.2.0
-
Spring LDAP 3.2.0
-
Spring AMQP 3.1.0
-
Spring Kafka 3.1.0
-
Micrometer 1.12.0
Virtual Threads support
采用 JDK 21 LTS 是 Spring Batch 5.1 的主要主题之一,尤其是对 Project Loom 中的虚拟线程的支持。在此版本中,虚拟线程可用于框架的所有区域,例如使用虚拟线程运行并发步骤或使用虚拟线程并行启动多个步骤。
Embracing JDK 21 LTS is one of the main themes for Spring Batch 5.1, especially the support of virtual threads from Project Loom. In this release, virtual threads can be used in all areas of the framework, like running a concurrent step with virtual threads or launching multiple steps in parallel using virtual threads.
得益于 Spring Batch 中设计精良的关注点分离功能,线程不受直接管理。相反,线程管理委派给 Spring Framework 中的 TaskExecutor
实现。这种编程到接口的方法允许您以透明且灵活的方式在 TaskExecutor
实现之间切换。
Thanks to the well designed separation of concerns in Spring Batch, threads are not managed directly. Thread
management is rather delegated to TaskExecutor
implementations from Spring Framework. This programming-to-interface
approach allows you to switch between TaskExecutor
implementations in a transparent and a flexible way.
在 Spring Framework 6.1 中,引入了一个基于虚拟线程的新 TaskExecutor
实现,即 VirtualThreadTaskExecutor
。可以在 TaskExecutor
所需的任何地方使用此 TaskExecutor
。
In Spring Framework 6.1, a new TaskExecutor
implementation based on virtual threads has been introduced, which is the
VirtualThreadTaskExecutor
. This TaskExecutor
can be used in Spring Batch wherever a TaskExecutor
is required.
Memory management improvement in the JpaItemWriter
在使用 JpaItemWriter
时,如果块大小足够大,JPA 持久性上下文可能会快速增长。如果不及时适当地将其清除,则可能导致 OutOfMemoryError
错误。
When using the JpaItemWriter
, the JPA persistence context can quickly grow when the chunk size
is large enough. This might lead to OutOfMemoryError
errors if not cleared appropriately in a timely manner.
在此版本中,在 JpaItemWriter
中引入了名为 clearPersistenceContext
的新选项,以在写入每块项目后清除持久性上下文。此选项改进了处理大量数据和大块大小的分块导向步骤的内存管理。
In this release, a new option named clearPersistenceContext
has been introduced in the JpaItemWriter
to clear the persistence context after writing each chunk of items. This option improves the memory management
of chunk-oriented steps dealing with large amounts of data and big chunk sizes.
New synchronized decorators for item readers and writers
在 5.0 版本之前,Spring Batch 提供了两个装饰器 SynchronizedItemStreamReader
和 SynchronizedItemStreamWriter
,以同步线程对 ItemStreamReader#read
和 ItemStreamWriter#write
的访问。在多线程步骤中使用非线程安全的项目流时,这些装饰器很有用。
Up to version 5.0, Spring Batch provided two decorators SynchronizedItemStreamReader
and SynchronizedItemStreamWriter
to synchronize thread access to ItemStreamReader#read
and ItemStreamWriter#write
. Those decorators are useful when
using non thread-safe item streams in multi-threaded steps.
虽然这些装饰器与 ItemStream
实现一起使用,但它们不能与非项目流一起使用。例如,这些装饰器不能用于同步对 ListItemReader#read
或 KafkaItemWriter#write
的访问。
While those decorators work with ItemStream
implementations, they are not usable with non-item streams. For example,
those decorators cannot be used to synchronize access to ListItemReader#read
or KafkaItemWriter#write
.
为了使用户更加便利,此版本还为非项目流引入了新的装饰器。借助此新特性,Spring Batch 中的所有项目读取器和写入器现在可以在无需编写自定义装饰器的情况下进行同步。
For users convenience, this release introduces new decorators for non-item streams as well. With this new feature, all item readers and writers in Spring Batch can now be synchronized without having to write custom decorators.
New Cursor-based MongoItemReader
在 5.0 版本之前,Spring Batch 提供的 MongoItemReader
使用了基于 MongoDB 的 skip
操作的分页。虽然这种方法适用于中小型数据集,但对于大型数据集而言,它的性能开始变差。
Up to version 5.0, the MongoItemReader
provided by Spring Batch used pagination, which is based on MongoDB’s skip
operation.
While this works well for small/medium data sets, it starts to perform poorly with large data sets.
此版本引入了 MongoCursorItemReader
,这是 MongoDB 的一个基于游标的新项目读取器。此实现使用游标代替分页来读取 MongoDB 中的数据,这改善了对大型集合的读取性能。为了与其他游标/分页读取器保持一致,现已将 MongoItemReader
重命名为 MongoPagingItemReader
。
This release introduces the MongoCursorItemReader
, a new cursor-based item reader for MongoDB. This implementation
uses cursors instead paging to read data from MongoDB, which improves the performance of reads on large collections.
For consistency with other cursor/paging readers, the current MongoItemReader
has been renamed to MongoPagingItemReader
.
Bulk inserts support in MongoItemWriter
在 5.0 版本之前,MongoItemWriter
支持两个操作:upsert
和 delete
。虽然 upsert
操作可以正常用于插入和更新,但对于已知在目标集合中为新项目的项目,它的性能不佳。
Up to version 5.0, the MongoItemWriter
supported two operations: upsert
and delete
. While the upsert
operation works well for both inserts and updates, it does not perform well for items that are known to be new
in the target collection.
类似于 JpaItemWriter
中的 persist
和 merge
操作,此版本在 MongoItemWriter
中添加了一个名为 insert
的新操作,专用于批量插入。对于新项目,此新选项的性能优于 upsert
,因为它不需要额外查找以检查项目是否已存在于目标集合中。
Similar to the persist
and merge
operations in the JpaItemWriter
, this release adds a new operation named
insert
in the MongoItemWriter
, which is designed for bulk inserts. This new option performs better than
upsert
for new items as it does not require an additional lookup to check if items already exist in the target collection.
New item reader and writer for Redis
新 RedisItemReader
现已在内置项目读取器库中提供。此读取器基于 Spring Data Redis,可以使用 ScanOptions
进行配置,以扫描要从中读取 Redis 的键集。
A new RedisItemReader
is now available in the library of built-in item readers. This reader is based on Spring Data Redis
and can be configured with a ScanOptions
to scan the key set to read from Redis.
类似地,基于 Spring Data Redis 的新 RedisItemWriter
现在是写入器库的一部分。可以使用 RedisTemplate
配置此写入器,以将项目写入 Redis。
Similarly, a new RedisItemWriter
based on Spring Data Redis is now part of the writers library. This writer can be configured
with a RedisTemplate
to write items to Redis.
Automatic configuration of JobRegistryBeanPostProcessor
在 Spring Batch 应用程序中配置 JobOperator
时,需要在操作员的 JobRegistry
中注册作业。此注册过程要么手动完成,要么通过向应用程序上下文添加 JobRegistryBeanPostProcessor
bean 来自动完成。
When configuring a JobOperator
in a Spring Batch application, it is necessary to register the jobs in the operator’s JobRegistry
.
This registration process is either done manually or automatically by adding a JobRegistryBeanPostProcessor
bean to the application
context.
在此版本中,Spring Batch 的默认配置(即通过使用 @EnableBatchProcessing
或扩展 DefaultBatchConfiguration
)现在会自动向应用程序上下文中注册一个 JobRegistryBeanPostProcessor
bean。这简化了配置过程,并在使用 JobOperator
时改善了用户体验。
In this release, the default configuration of Spring Batch (ie by using @EnableBatchProcessing
or extending DefaultBatchConfiguration
)
now automatically registers a JobRegistryBeanPostProcessor
bean to the application context. This simplifies the configuration process
and improves the user experience when using a JobOperator
.
Ability to start a job flow with a decision
在使用 XML 配置样式时,借助 <decision>
元素启动带有决策器的作业流程是可行的。但是,在 5.0 版本之前,无法使用 Java API 实现相同的流程定义。
When using the XML configuration style, it is possible to start a job flow with a decider thanks to the <decision>
element.
However, up to version 5.0, it was not possible to achieve the same flow definition with the Java API.
在此版本中,已向 JobBuilder
API 添加了启动带有 JobExecutionDecider
的作业流程的新选项。这样使两种配置样式更一致。
In this release, a new option to start a job flow with a JobExecutionDecider
was added to the JobBuilder
API.
This makes both configuration styles more consistent.
Ability to provide a custom JobKeyGenerator
默认情况下,Spring Batch 通过计算识别作业参数的 MD5 哈希值来识别作业实例。虽然几乎不需要自定义此识别过程,但 Spring Batch 仍然提供了一个策略接口,以便用户通过 JobKeyGenerator
API 覆盖默认机制。
By default, Spring Batch identifies job instances by calculating an MD5 hash of the identifying job parameters. While it is unlikely to
need to customize this identification process, Spring Batch still provide a strategy interface for users to override the default mechanism
through the JobKeyGenerator
API.
在 5.0 版本之前,如果没有创建自定义 JobRepository
和 JobExplorer
,则无法提供自定义键生成器。在此版本中,现在可以通过 JobRepository
和 JobExplorer
的工厂 Bean 提供自定义 JobKeyGenerator
。
Up to version 5.0, it was not possible to provide a custom key generator without having to create a custom JobRepository
and JobExplorer
.
In this version, it is now possible to provide a custom JobKeyGenerator
through the factory beans of JobRepository
and JobExplorer
.
New documentation based on Antora
参考文档已更新为使用 Antora。此更新引入多项改进,包括但不限于:
The reference documentation was updated to use Antora. This update introduces a number of improvements, including but not limited to:
-
Multi-version documentation: it is now possible to navigate from one version to another thanks to the drop down version list in the left side menu.
-
Integrated search experience: powered by Algolia, the search experience in now better thanks to the integrated search box at the top left of the page
-
Improved configuration style toggle: the toggle to switch between the XML and Java configuration styles for code snippets is now located near each sample, rather than the top of each page
Improved Getting Started experience
在此版本中,入门体验在许多方面得到了改善:
In this release, the getting started experience was improved in many ways:
-
Samples are now packaged by feature and are provided in two configuration styles: XML and Java configuration
-
A new Two minutes tutorial was added to the README
-
The Getting Started Guide was updated to the latest and greatest Spring Batch and Spring Boot versions
-
The Issue Reporting Guide was updated with detailed instructions and project templates to help you easily report issues
MongoDB Job Repository (Experimental)
此功能引入了由 MongoDB 支持的 JobRepository
和 JobExplorer
的新实现。此项长期期待的功能现在已可用作为实验功能,并标志着 Spring Batch 中引入了第一个 NoSQL 元数据存储。
This feature introduces new implementations of JobRepository
and JobExplorer
backed by MongoDB. This long-awaited feature is now available
as experimental and marks the introduction of the first NoSQL meta-data store for Spring Batch.
有关此功能的更多详细信息,请参阅 Spring Batch Experimental 储存库。
Please refer to the Spring Batch Experimental repository for more details about this feature.
Composite Item Reader (Experimental)
此功能引入了一个复合 ItemReader
实现。与 CompositeItemProcessor
和 CompositeItemWriter
相似,其思想是有序地将读取委派给项目读取器列表。当需要从不同的源(文件、数据库等)读取相同格式的数据时,这会十分有用。
This feature introduces a composite ItemReader
implementation. Similar to the CompositeItemProcessor
and CompositeItemWriter
, the idea is to delegate reading to a list of item readers in order.
This is useful when there is a requirement to read data having the same format from different sources (files, databases, etc).
有关此功能的更多详细信息,请参阅 Spring Batch Experimental 储存库。
Please refer to the Spring Batch Experimental repository for more details about this feature.
New Chunk-Oriented Step implementation (Experimental)
这不是一项新功能,而是按块进行处理模型的新实现。其目标是解决当前实现中报告的问题,并为即将重新设计的并发模型提供一个新的基础。
This is not a new feature, but rather a new implementation of the chunk-oriented processing model. The goal is to address the reported issues with the current implementation and to provide a new base for the upcoming re-designed concurrency model.
有关此新实现的更多详细信息,请参阅 Spring Batch Experimental 储存库。
Please refer to the Spring Batch Experimental repository for more details about this new implementation.