# Spring Cloud 任务参考指南
# [](#preface)[Preface](#preface)
本节提供了 Spring Cloud任务参考文档的简要概述。把它看作是文档其余部分的一张地图。你可以以线性方式阅读此参考指南,或者如果你对某些内容不感兴趣,可以跳过部分。
## [](#about-the-documentation)[1.关于文档](#about-the-documentation)
Spring Cloud任务参考指南在[html](https://docs.spring.io/spring-cloud-task/docs/current/reference)和[pdf](https://docs.spring.io/spring-cloud-task/docs/current/reference/index.pdf),[epub](https://docs.spring.io/spring-cloud-task/docs/current/reference/index.epub)中可用。最新版本可在[docs.spring.io/spring-cloud-task/docs/current-SNAPSHOT/reference/html/](https://docs.spring.io/spring-cloud-task/docs/current-SNAPSHOT/reference/html/)处获得。
本文件的副本可供你自己使用并分发给他人,但前提是你不对此类副本收取任何费用,并且还需每一份副本均包含本版权声明,无论是以印刷形式还是以电子方式分发。
## [](#task-documentation-getting-help)[2.获得帮助](#task-documentation-getting-help)
云任务有问题吗?我们愿意提供帮助!
* 问一个问题。我们监视[stackoverflow.com](https://stackoverflow.com)中带有[`spring-cloud-task`]标记的问题(https://stackoverflow.com/tags/ Spring-cloud-task)。
* 使用 Spring Cloud任务在[github.com/spring-cloud/spring-cloud-task/issues](https://github.com/spring-cloud/spring-cloud-task/issues)上报告错误。
| |所有的云任务都是开源的,包括文档。如果你发现
是 DOCS 的问题,或者你只是想改进它们,请[get
involved](https://github.com/spring-cloud/spring-cloud-task/tree/master)。|
|---|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
## [](#task-documentation-first-steps)[3.第一步](#task-documentation-first-steps)
如果你刚刚开始使用 Spring Cloud任务或一般的“ Spring”,我们建议你阅读[开始](#getting-started)章节。
要从头开始,请阅读以下部分:
* [Introducing Spring Cloud Task](#getting-started-introducing-spring-cloud-task)
* [系统要求](#getting-started-system-requirements)
要遵循本教程,请阅读[Developing Your First Spring Cloud Task Application](#getting-started-developing-first-task)以运行你的示例,请阅读[运行示例](#getting-started-running-the-example)
# [](#getting-started)[开始](#getting-started)
如果你刚刚开始使用 Spring Cloud Task,那么你应该阅读这一部分。在这里,我们回答基本的“什么?”、“怎么做?”和“为什么?”的问题。我们从温和地介绍云任务开始。然后,我们构建了一个 Spring Cloud任务应用程序,讨论了一些核心原则。
## [](#getting-started-introducing-spring-cloud-task)[4. Introducing Spring Cloud Task](#getting-started-introducing-spring-cloud-task)
Spring Cloud任务使创建短期微服务变得容易。它提供了允许在生产环境中按需执行短期 JVM 流程的功能。
## [](#getting-started-system-requirements)[5.系统要求](#getting-started-system-requirements)
你需要安装 Java(Java8 或更好)。要进行构建,还需要安装 Maven。
### [](#database-requirements)[5.1.数据库需求](#database-requirements)
Spring Cloud任务使用关系数据库来存储已执行任务的结果。虽然可以在没有数据库的情况下开始开发任务(任务的状态作为任务存储库更新的一部分记录),但对于生产环境,你希望使用受支持的数据库。 Spring Cloud任务当前支持以下数据库:
* DB2
* H2
* HSQLDB
* MySQL
* 甲骨文
* Postgres
* SQLServer
## [](#getting-started-developing-first-task)[6. Developing Your First Spring Cloud Task Application](#getting-started-developing-first-task)
一个很好的起点是使用一个简单的“你好,世界!”应用程序,因此我们创建了相当于突出该框架功能的 Spring Cloud 任务。大多数 IDE 都对 Apache Maven 有很好的支持,因此我们将它用作这个项目的构建工具。
| |Spring.io 网站包含许多使用 Spring boot 的[`Getting Started`“
guides](https:// Spring.io/guides)。如果你需要解决某个特定的问题,请先在此进行检查。
你可以通过执行[Spring Initializr](https://start.spring.io/)并创建一个新项目来快捷执行以下步骤。这样做
会自动生成一个新的项目结构,这样你就可以立即开始编码。
我们建议你尝试使用 Spring initializr 来熟悉它。|
|---|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
### [](#getting-started-creating-project)[6.1. Creating the Spring Task Project using Spring Initializr](#getting-started-creating-project)
现在,我们可以创建并测试一个将`Hello, World!`打印到控制台的应用程序。
这样做:
1. 访问[Spring Initialzr](https://start.spring.io/)网站。
1. 创建一个新的 Maven 项目,其**集团**的名称为`io.spring.demo`,而**人工制品**的名称为`helloworld`。
2. 在“依赖关系”文本框中,键入`task`,然后选择`Cloud Task`依赖关系。
3. 在“依赖关系”文本框中,键入`jdbc`,然后选择`JDBC`依赖关系。
4. 在“依赖关系”文本框中,键入`h2`,然后选择`H2`。(或者你最喜欢的数据库)
5. 点击**生成项目**按钮
2. 解压 helloworld.zip 文件并将项目导入到你最喜欢的 IDE 中。
### [](#getting-started-writing-the-code)[6.2.编写代码](#getting-started-writing-the-code)
要完成我们的应用程序,我们需要用以下内容更新生成的`HelloworldApplication`,以便它启动一个任务。
```
package io.spring.demo.helloworld;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
@EnableTask
public class HelloworldApplication {
@Bean
public CommandLineRunner commandLineRunner() {
return new HelloWorldCommandLineRunner();
}
public static void main(String[] args) {
SpringApplication.run(HelloworldApplication.class, args);
}
public static class HelloWorldCommandLineRunner implements CommandLineRunner {
@Override
public void run(String... strings) throws Exception {
System.out.println("Hello, World!");
}
}
}
```
虽然它看起来很小,但相当多的事情正在发生。有关 Spring 引导细节的更多信息,请参见[Spring Boot reference documentation](https://docs.spring.io/spring-boot/docs/current/reference/html/)。
现在我们可以在`src/main/resources`中打开`application.properties`文件。我们需要在`application.properties`中配置两个属性:
* `application.name`:设置应用程序名(已转换为任务名)
* `logging.level`:将 Spring Cloud任务的日志设置为`DEBUG`,以便查看正在发生的事情。
下面的示例展示了如何同时做到这两点:
```
logging.level.org.springframework.cloud.task=DEBUG
spring.application.name=helloWorld
```
#### [](#getting-started-at-task)[6.2.1.任务自动配置](#getting-started-at-task)
当包含 Spring Cloud Task Starter 依赖项时,Task Auto 会配置所有 bean 以引导其功能。此配置的一部分注册了`TaskRepository`及其使用的基础结构。
在我们的演示中,`TaskRepository`使用嵌入式 H2 数据库来记录任务的结果。这种 H2 嵌入式数据库对于生产环境不是一种实用的解决方案,因为一旦任务结束,H2DB 就会消失。然而,为了获得快速的入门体验,我们可以在示例中使用它,也可以将存储库中正在更新的内容与日志相呼应。在[配置](#features-configuration)小节(在本文档的后面)中,我们介绍了如何定制 Spring Cloud Task 提供的组件的配置。
当我们的示例应用程序运行时, Spring 启动我们的`HelloWorldCommandLineRunner`,并将我们的“你好,世界!”消息输出为标准输出。`TaskLifecycleListener`在存储库中记录任务的开始和结束。
#### [](#getting-started-main-method)[6.2.2.主要方法](#getting-started-main-method)
Main 方法是任何 Java 应用程序的入口点。我们的主方法将委托给 Spring Boot 的[SpringApplication](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-spring-application.html)类。
#### [](#getting-started-clr)[6.2.3.The CommandlineRunner](#getting-started-clr)
Spring 包括引导应用程序逻辑的许多方法。 Spring Boot 通过其`*Runner`接口(`CommandLineRunner`或`ApplicationRunner`)以有组织的方式提供了这样做的方便方法。一个表现良好的任务可以通过使用这两个运行器中的一个来引导任何逻辑。
任务的生命周期是从`*Runner#run`方法被执行到它们全部完成之前考虑的。 Spring 引导让应用程序使用多个`*Runner`实现,就像 Spring Cloud任务一样。
| |除`CommandLineRunner`或`ApplicationRunner`(例如通过使用`InitializingBean#afterPropertiesSet`)以外的机制引导的任何处理都不是由 Spring Cloud任务记录的
。|
|---|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
### [](#getting-started-running-the-example)[6.3.运行示例](#getting-started-running-the-example)
在这一点上,我们的应用程序应该可以工作。由于此应用程序是基于 Spring 引导的,因此我们可以从应用程序的根使用`$ mvn spring-boot:run`从命令行运行它,如下例所示(其输出):
```
$ mvn clean spring-boot:run
....... . . .
....... . . . (Maven log output here)
....... . . .
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.3.RELEASE)
2018-07-23 17:44:34.426 INFO 1978 --- [ main] i.s.d.helloworld.HelloworldApplication : Starting HelloworldApplication on Glenns-MBP-2.attlocal.net with PID 1978 (/Users/glennrenfro/project/helloworld/target/classes started by glennrenfro in /Users/glennrenfro/project/helloworld)
2018-07-23 17:44:34.430 INFO 1978 --- [ main] i.s.d.helloworld.HelloworldApplication : No active profile set, falling back to default profiles: default
2018-07-23 17:44:34.472 INFO 1978 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.spring[email protected]1d24f32d: startup date [Mon Jul 23 17:44:34 EDT 2018]; root of context hierarchy
2018-07-23 17:44:35.280 INFO 1978 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2018-07-23 17:44:35.410 INFO 1978 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2018-07-23 17:44:35.419 DEBUG 1978 --- [ main] o.s.c.t.c.SimpleTaskConfiguration : Using org.springframework.cloud.task.configuration.DefaultTaskConfigurer TaskConfigurer
2018-07-23 17:44:35.420 DEBUG 1978 --- [ main] o.s.c.t.c.DefaultTaskConfigurer : No EntityManager was found, using DataSourceTransactionManager
2018-07-23 17:44:35.522 DEBUG 1978 --- [ main] o.s.c.t.r.s.TaskRepositoryInitializer : Initializing task schema for h2 database
2018-07-23 17:44:35.525 INFO 1978 --- [ main] o.s.jdbc.datasource.init.ScriptUtils : Executing SQL script from class path resource [org/springframework/cloud/task/schema-h2.sql]
2018-07-23 17:44:35.558 INFO 1978 --- [ main] o.s.jdbc.datasource.init.ScriptUtils : Executed SQL script from class path resource [org/springframework/cloud/task/schema-h2.sql] in 33 ms.
2018-07-23 17:44:35.728 INFO 1978 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-07-23 17:44:35.730 INFO 1978 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Bean with name 'dataSource' has been autodetected for JMX exposure
2018-07-23 17:44:35.733 INFO 1978 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Located MBean 'dataSource': registering with JMX server as MBean [com.zaxxer.hikari:name=dataSource,type=HikariDataSource]
2018-07-23 17:44:35.738 INFO 1978 --- [ main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 0
2018-07-23 17:44:35.762 DEBUG 1978 --- [ main] o.s.c.t.r.support.SimpleTaskRepository : Creating: TaskExecution{executionId=0, parentExecutionId=null, exitCode=null, taskName='application', startTime=Mon Jul 23 17:44:35 EDT 2018, endTime=null, exitMessage='null', externalExecutionId='null', errorMessage='null', arguments=[]}
2018-07-23 17:44:35.772 INFO 1978 --- [ main] i.s.d.helloworld.HelloworldApplication : Started HelloworldApplication in 1.625 seconds (JVM running for 4.764)
Hello, World!
2018-07-23 17:44:35.782 DEBUG 1978 --- [ main] o.s.c.t.r.support.SimpleTaskRepository : Updating: TaskExecution with executionId=1 with the following {exitCode=0, endTime=Mon Jul 23 17:44:35 EDT 2018, exitMessage='null', errorMessage='null'}
```
前面的输出有三行我们感兴趣的内容:
* `SimpleTaskRepository`在`TaskRepository`中记录了条目的创建。
* 我们的`CommandLineRunner`的执行,通过输出“Hello,World!”进行了演示。
* `SimpleTaskRepository`在`TaskRepository`中记录任务的完成情况。
| |一个简单的任务应用程序可以在 Spring Cloud
任务项目[here](https://github.com/spring-cloud/spring-cloud-task/tree/master/spring-cloud-task-samples/timestamp)的样例模块中找到。|
|---|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
# [](#features)[Features](#features)
本节将更详细地介绍 Spring Cloud任务,包括如何使用它,如何配置它,以及适当的扩展点。
## [](#features-lifecycle)[7. The lifecycle of a Spring Cloud Task](#features-lifecycle)
在大多数情况下,现代云环境是围绕预期不会结束的流程的执行而设计的。如果它们结束了,它们通常会重新启动。虽然大多数平台确实有某种方式来运行一个在结束时不会重新启动的流程,但该运行的结果通常不会以可消耗的方式维护。 Spring Cloud任务提供了在环境中执行短期过程并记录结果的能力。这样做允许围绕短期流程的微服务架构,以及通过消息集成任务来运行较长时间的服务。
虽然这种功能在云环境中很有用,但在传统的部署模型中也可能出现同样的问题。 Spring 当启动应用程序与诸如 CRON 的调度程序一起运行时,能够在其完成后监视应用程序的结果是有用的。
Spring Cloud任务采取的方法是, Spring 引导应用程序可以有一个开始和一个结束,并且仍然是成功的。批处理应用程序就是一个例子,它说明了预期结束的过程(通常是短暂的)是如何有用的。
Spring Cloud任务记录给定任务的生命周期事件。以大多数 Web 应用程序为代表的大多数长时间运行的进程都不保存其生命周期事件。 Spring Cloud任务的核心任务就是这样做的。
生命周期由单个任务执行组成。这是一个 Spring 引导应用程序的物理执行,该应用程序被配置为一个任务(即,它具有 Sprint 云任务的依赖性)。
在任务开始时,在运行任何`CommandLineRunner`或`ApplicationRunner`实现之前,将在`TaskRepository`中创建一个记录开始事件的条目。此事件是通过由 Spring 框架触发的`SmartLifecycle#start`触发的。这向系统指示,所有 bean 都已准备好使用,并且在运行 Spring boot 提供的`CommandLineRunner`或`ApplicationRunner`实现之前就会出现。
| |只有在成功引导`ApplicationContext`时,才会记录任务。如果上下文根本无法引导,则不会记录任务的运行
。|
|---|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
在完成来自 Spring 引导的所有`*Runner#run`调用或`ApplicationContext`失败(由`ApplicationFailedEvent`表示)后,将在存储库中使用结果更新任务执行。
| |如果应用程序要求在
处关闭`ApplicationContext`任务的完成(所有`*Runner#run`方法都已调用,并且任务
存储库已更新),则将属性`spring.cloud.task.closecontextEnabled`设置为 true。|
|---|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
### [](#features-task-execution-details)[7.1.任务执行](#features-task-execution-details)
存储在`TaskRepository`中的信息在`TaskExecution`类中建模,并由以下信息组成:
| Field |说明|
|--------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|`executionid` |任务运行的唯一 ID。|
| `exitCode` |从`ExitCodeExceptionMapper`实现生成的退出代码。如果没有生成
退出代码,但抛出了`ApplicationFailedEvent`,则设置 1。否则,它是
假定为 0。|
| `taskName` |任务的名称,由配置的`TaskNameResolver`确定。|
| `startTime` |任务启动的时间,如`SmartLifecycle#start`调用所示。|
| `endTime` |任务完成的时间,如`ApplicationReadyEvent`所示。|
|`exitMessage` |退出时可获得的任何信息。这可以通过`TaskExecutionListener`以编程方式设置。|
|`errorMessage`|如果异常是任务结束的原因(如`ApplicationFailedEvent`所示),则该异常的堆栈跟踪存储在此。|
| `arguments` |一个`List`的字符串命令行参数,因为它们被传递到可执行的
引导应用程序中。|
### [](#features-lifecycle-exit-codes)[7.2.映射退出代码](#features-lifecycle-exit-codes)
当任务完成时,它会尝试将退出代码返回到操作系统。如果我们看一下我们的[原始示例](#getting-started-developing-first-task),我们可以看到,我们并没有控制我们应用程序的那个方面。因此,如果抛出了异常,JVM 将返回一段代码,该代码在调试中可能对你有任何用处,也可能对你没有任何用处。
因此, Spring Boot 提供了一个接口`ExitCodeExceptionMapper`,它允许你将未捕获的异常映射到退出代码。这样做可以让你在退出代码的层面上指出出了什么问题。另外,通过以这种方式映射退出代码, Spring Cloud任务记录返回的退出代码。
如果任务以 SIG-INT 或 SIG-项结束,则除非代码中另有指定,否则退出代码为零。
| |在任务运行时,退出代码以空的形式存储在存储库中。
一旦任务完成,相应的退出代码将根据本节前面描述的
准则进行存储。|
|---|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
## [](#features-configuration)[8.配置](#features-configuration)
Spring Cloud任务提供了一种现成的配置,如在`DefaultTaskConfigurer`和`SimpleTaskConfiguration`类中所定义的那样。本节将介绍默认值以及如何根据你的需要定制 Spring Cloud任务。
### [](#features-data-source)[8.1.数据源](#features-data-source)
Spring Cloud任务使用数据源来存储任务执行的结果。默认情况下,我们提供了一个 H2 的内存实例,以提供一种简单的引导开发方法。但是,在生产环境中,你可能希望配置自己的`DataSource`。
如果你的应用程序只使用一个`DataSource`并同时作为你的业务模式和任务存储库,那么你所需要做的就是提供任何`DataSource`(这样做的最简单方法是通过 Spring Boot 的配置约定)。 Spring Cloud Task 为存储库自动使用这个`DataSource`。
如果应用程序使用多个`DataSource`,则需要使用适当的`DataSource`配置任务存储库。这种定制可以通过`TaskConfigurer`的实现来完成。
### [](#features-table-prefix)[8.2.表格前缀](#features-table-prefix)
`TaskRepository`的一个可修改的属性是任务表的表前缀。默认情况下,它们都以`TASK_`开头。`TASK_EXECUTION`和`TASK_EXECUTION_PARAMS`是两个例子。然而,有潜在的理由修改这个前缀。如果需要将模式名前置到表名,或者如果同一模式中需要一组以上的任务表,则必须更改表前缀。可以将`spring.cloud.task.tablePrefix`设置为所需的前缀,如下所示:
`spring.cloud.task.tablePrefix=yourPrefix`
通过使用`spring.cloud.task.tablePrefix`,用户承担了创建任务表的责任,这些任务表满足任务表模式的两个标准,但需要进行用户业务需求所需的修改。在创建你自己的任务 DDL 时,可以使用 Spring Cloud任务模式 DDL 作为指导,如[here](https://github.com/spring-cloud/spring-cloud-task/tree/master/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task)所示。
### [](#features-table-initialization)[8.3.启用/禁用表初始化](#features-table-initialization)
如果你正在创建任务表,并且不希望 Spring Cloud Task 在任务启动时创建它们,请将`spring.cloud.task.initialize-enabled`属性设置为`false`,如下所示:
`spring.cloud.task.initialize-enabled=false`
它的默认值为`true`。
| |属性`spring.cloud.task.initialize.enable`已被弃用。|
|---|-----------------------------------------------------------------------|
### [](#features-generated_task_id)[8.4.外部生成的任务 ID](#features-generated_task_id)
在某些情况下,你可能希望在请求任务和基础设施实际启动任务之间留出时间差。 Spring Cloud任务允许你在任务被请求时创建`TaskExecution`。然后将生成的`TaskExecution`的执行 ID 传递给任务,以便它可以在任务的生命周期中更新`TaskExecution`。
可以通过在`TaskRepository`的实现上调用`createTaskExecution`方法来创建`TaskExecution`,该实现引用了保存`TaskExecution`对象的数据存储。
为了将任务配置为使用生成的`TaskExecutionId`,请添加以下属性:
`spring.cloud.task.executionid=yourtaskId`
### [](#features-external_task_id)[8.5.外部任务 ID](#features-external_task_id)
Spring Cloud Task 允许你为每个`TaskExecution`存储一个外部任务 ID。这方面的一个例子是,当一个任务在平台上启动时,Cloud Foundry 提供了一个任务 ID。为了将任务配置为使用生成的`TaskExecutionId`,请添加以下属性:
`spring.cloud.task.external-execution-id=`
### [](#features-parent_task_id)[8.6.父任务 ID](#features-parent_task_id)
Spring Cloud Task 允许你为每个`TaskExecution`存储父任务 ID。这方面的一个例子是一个执行另一个或多个任务的任务,你希望记录每个子任务启动了哪个任务。为了将任务配置为设置父`TaskExecutionId`,在子任务上添加以下属性:
`spring.cloud.task.parent-execution-id=`
### [](#features-task-configurer)[8.7.任务配置器](#features-task-configurer)
`TaskConfigurer`是一个策略接口,它允许你定制 Spring Cloud任务组件的配置方式。默认情况下,我们提供了提供逻辑默认值的`DefaultTaskConfigurer`:基于`Map`的内存中组件(如果不提供`DataSource`,则对开发有用)和基于 JDBC 的组件(如果有`DataSource`可用,则很有用)。
`TaskConfigurer`允许你配置三个主要组件:
| Component |说明| Default (provided by `DefaultTaskConfigurer`) |
|----------------------------|------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------|
| `TaskRepository` |实现`TaskRepository`所要使用的。| `SimpleTaskRepository` |
| `TaskExplorer` |要使用的`TaskExplorer`(用于对任务
存储库进行只读访问的组件)的实现。| `SimpleTaskExplorer` |
|`PlatformTransactionManager`|运行任务更新时使用的事务管理器。|`DataSourceTransactionManager` if a `DataSource` is used.`ResourcelessTransactionManager` if it is not.|
你可以通过创建`TaskConfigurer`接口的自定义实现来定制上表中描述的任何组件。通常,扩展`DefaultTaskConfigurer`(如果没有找到`TaskConfigurer`,则提供该扩展)并重写所需的吸气器就足够了。然而,可能需要从头开始实现你自己的功能。
| |用户不应该直接使用来自`TaskConfigurer`的 getter 方法直接
,除非他们正在使用它来提供将被公开为 Spring bean 的实现。|
|---|-------------------------------------------------------------------------------------------------------------------------------------------------------------------|
### [](#features-task-name)[8.8.任务名称](#features-task-name)
在大多数情况下,任务的名称是在 Spring 引导中配置的应用程序名称。但是,在某些情况下,你可能希望将任务的运行映射到不同的名称。 Spring Cloud数据流就是这方面的一个例子(因为你可能希望以任务定义的名称运行该任务)。因此,我们提供了通过`TaskNameResolver`接口定制任务命名方式的能力。
默认情况下, Spring Cloud Task 提供`SimpleTaskNameResolver`,它使用以下选项(按优先顺序排列):
1. Spring 引导属性(以 Spring 引导允许的任何方式配置)称为`spring.cloud.task.name`。
2. 使用 Spring 引导规则解析的应用程序名称(通过`ApplicationContext#getId`获得)。
### [](#features-task-execution-listener)[8.9.任务执行监听器](#features-task-execution-listener)
`TaskExecutionListener`允许你为任务生命周期中发生的特定事件注册侦听器。为此,创建一个实现`TaskExecutionListener`接口的类。实现`TaskExecutionListener`接口的类将收到以下事件的通知:
* `onTaskStartup`:在将`TaskExecution`存储到`TaskRepository`之前。
* `onTaskEnd`:在更新`TaskExecution`中的`TaskExecution`条目并标记任务的最终状态之前。
* `onTaskFailed`:在任务抛出未处理的异常时调用`onTaskEnd`方法之前。
Spring Cloud Task 还允许你通过使用以下方法注释将`TaskExecution`侦听器添加到 Bean 内的方法:
* `@BeforeTask`:将之前的`TaskExecution`存储到`TaskRepository`中
* `@AfterTask`:在更新`TaskExecution`条目之前,在`TaskRepository`中标记任务的最终状态。
* `@FailedTask`:在任务抛出未处理的异常时调用`@AfterTask`方法之前。
下面的示例显示了正在使用的三种注释:
```
public class MyBean {
@BeforeTask
public void methodA(TaskExecution taskExecution) {
}
@AfterTask
public void methodB(TaskExecution taskExecution) {
}
@FailedTask
public void methodC(TaskExecution taskExecution, Throwable throwable) {
}
}
```
| |在链中比`TaskLifecycleListener`存在更早地插入`ApplicationListener`可能会导致意外的影响。|
|---|-------------------------------------------------------------------------------------------------------------------------|
#### [](#features-task-execution-listener-Exceptions)[8.9.1.任务执行侦听器抛出的异常](#features-task-execution-listener-Exceptions)
如果`TaskExecutionListener`事件处理程序引发异常,则该事件处理程序的所有侦听器处理都将停止。例如,如果三个`onTaskStartup`侦听器已经启动,并且第一个`onTaskStartup`事件处理程序抛出一个异常,则不调用另外两个`onTaskStartup`方法。但是,调用`TaskExecutionListeners`的其他事件处理程序(`onTaskEnd`和`onTaskFailed`)。
当`TaskExecutionListener`事件处理程序抛出异常时返回的退出代码是[exitcodeevent](https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/ExitCodeEvent.html)报告的退出代码。如果没有`ExitCodeEvent`发出,则对抛出的异常进行评估,以查看它是否为[exitcodegenerator](https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-application-exit)类型。如果是,则返回来自`ExitCodeGenerator`的退出代码。否则,将返回`1`。
在`onTaskStartup`方法中抛出异常的情况下,应用程序的退出代码将是`1`。如果在`onTaskEnd`或`onTaskFailed`方法中抛出异常,则应用程序的退出代码将是使用上面列举的规则建立的代码。
| |在`onTaskStartup`、`onTaskEnd`或`onTaskFailed`中抛出异常的情况下,你无法使用`ExitCodeExceptionMapper`覆盖应用程序的退出代码。|
|---|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
#### [](#features-task-execution-listener-exit-messages)[8.9.2.退出消息](#features-task-execution-listener-exit-messages)
可以通过使用`TaskExecutionListener`以编程方式设置任务的退出消息。这是通过设置`TaskExecution’s``exitMessage`来完成的,然后将其传递到`TaskExecutionListener`中。下面的示例显示了一个用`@AfterTask``ExecutionListener`进行注释的方法:
```
@AfterTask
public void afterMe(TaskExecution taskExecution) {
taskExecution.setExitMessage("AFTER EXIT MESSAGE");
}
```
可以在任何侦听器事件(`onTaskStartup`、`onTaskFailed`和`onTaskEnd`)上设置`ExitMessage`。这三个侦听器的优先顺序如下:
1. `onTaskEnd`
2. `onTaskFailed`
3. `onTaskStartup`
例如,如果你为`onTaskStartup`和`onTaskFailed`侦听器设置了`exitMessage`,并且任务没有失败就结束了,则来自`onTaskStartup`的`exitMessage`将存储在存储库中。否则,如果发生故障,则存储来自`onTaskFailed`的`exitMessage`。同样,如果你使用`onTaskEnd`侦听器设置`exitMessage`,则来自`onTaskEnd`的`exitMessage`将取代来自`onTaskStartup`和`onTaskFailed`的退出消息。
### [](#features-single-instance-enabled)[8.10. Restricting Spring Cloud Task Instances](#features-single-instance-enabled)
Spring Cloud任务允许你确定一次只能运行一个具有给定任务名的任务。要做到这一点,你需要为每个任务执行建立[task name](#features-task-name)并设置`spring.cloud.task.single-instance-enabled=true`。当第一个任务执行正在运行时,当你尝试运行具有相同[task name](#features-task-name)和`spring.cloud.task.single-instance-enabled=true`的任务时,该任务将失败,并出现以下错误消息:`Task with name "application" is already running.``spring.cloud.task.single-instance-enabled`的默认值为`false`。下面的示例展示了如何将`spring.cloud.task.single-instance-enabled`设置为`true`:
`spring.cloud.task.single-instance-enabled=true or false`
要使用此功能,你必须向应用程序添加以下 Spring 集成依赖项:
```
org.springframework.integration
spring-integration-core
org.springframework.integration
spring-integration-jdbc
```
| |如果任务失败,则应用程序的退出代码将为 1,因为启用了此功能
,并且另一个任务正在以相同的任务名运行。|
|---|------------------------------------------------------------------------------------------------------------------------------------------------------|
### [](#disabling-spring-cloud-task-auto-configuration)[8.11. Disabling Spring Cloud Task Auto Configuration](#disabling-spring-cloud-task-auto-configuration)
在不应该为某个实现自动配置 Spring Cloud任务的情况下,你可以禁用 Task 的自动配置。这可以通过向任务应用程序添加以下注释来完成:
```
@EnableAutoConfiguration(exclude={SimpleTaskAutoConfiguration.class})
```
还可以通过将`spring.cloud.task.autoconfiguration.enabled`属性设置为`false`来禁用任务自动配置。
### [](#closing-the-context)[8.12.结束上下文](#closing-the-context)
如果应用程序要求在任务完成时关闭`ApplicationContext`(所有`*Runner#run`方法都已被调用,并且任务存储库已更新),则将属性`spring.cloud.task.closecontextEnabled`设置为`true`。
关闭上下文的另一种情况是当任务执行完成但应用程序没有终止时。在这些情况下,上下文是开放的,因为已经分配了一个线程(例如:如果你正在使用 TaskExecutor)。在这些情况下,在启动任务时将`spring.cloud.task.closecontextEnabled`属性设置为`true`。一旦任务完成,这将关闭应用程序的上下文。从而允许应用程序终止。
# [](#batch)[Batch](#batch)
本节将更详细地介绍 Spring Cloud Task 与 Spring Batch 的集成。跟踪作业执行与其执行的任务之间的关联,以及通过 Spring Cloud Deployer 进行远程分区,将在本节中介绍。
## [](#batch-association)[9.将作业执行与其执行的任务关联起来](#batch-association)
Spring Boot 为在 anüber- jar 内执行批处理作业提供了便利。 Spring Boot 对该功能的支持使开发人员能够在该执行中执行多个批处理任务。 Spring Cloud任务提供了将作业(作业执行)的执行与任务的执行相关联的能力,以便一个可以追溯到另一个。
Spring Cloud任务通过使用`TaskBatchExecutionListener`来实现这一功能。默认情况下,此侦听器在任何同时配置了 Spring 批作业(通过在上下文中定义了类型`Job`的 Bean)和 Classpath 上的`spring-cloud-task-batch` jar 的上下文中自动配置。所有符合这些条件的工作都会被注入监听器。
### [](#batch-association-override)[9.1.覆盖 TaskBatchExecutionListener](#batch-association-override)
为了防止侦听器被注入到当前上下文中的任何批处理作业中,你可以使用标准的 Spring 引导机制禁用自动配置。
要仅将侦听器注入到上下文中的特定作业中,请覆盖`batchTaskExecutionListenerBeanPostProcessor`并提供作业 Bean ID 的列表,如以下示例所示:
```
public TaskBatchExecutionListenerBeanPostProcessor batchTaskExecutionListenerBeanPostProcessor() {
TaskBatchExecutionListenerBeanPostProcessor postProcessor =
new TaskBatchExecutionListenerBeanPostProcessor();
postProcessor.setJobNames(Arrays.asList(new String[] {"job1", "job2"}));
return postProcessor;
}
```
| |你可以在 Spring Cloud
任务项目的样例模块中找到样例批处理应用程序,[here](https://github.com/spring-cloud/spring-cloud-task/tree/master/spring-cloud-task-samples/batch-job)。|
|---|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
## [](#batch-partitioning)[10.远程分区](#batch-partitioning)
Spring Cloud Deployer 提供了用于在大多数云基础设施上启动 Spring 基于引导的应用程序的设施。`DeployerPartitionHandler`和`DeployerStepExecutionHandler`将工人步骤执行的启动委托给 Spring Cloud Deployer。
要配置`DeployerStepExecutionHandler`,必须提供表示要执行的 Spring bootüber- jar 的`Resource`、`TaskLauncher`和`JobExplorer`。你可以配置任何环境属性,以及一次执行的工作人员的最大数量、轮询结果的间隔(默认为 10 秒)和超时(默认为-1 或无超时)。下面的示例显示了如何配置`PartitionHandler`:
```
@Bean
public PartitionHandler partitionHandler(TaskLauncher taskLauncher,
JobExplorer jobExplorer) throws Exception {
MavenProperties mavenProperties = new MavenProperties();
mavenProperties.setRemoteRepositories(new HashMap<>(Collections.singletonMap("springRepo",
new MavenProperties.RemoteRepository(repository))));
Resource resource =
MavenResource.parse(String.format("%s:%s:%s",
"io.spring.cloud",
"partitioned-batch-job",
"1.1.0.RELEASE"), mavenProperties);
DeployerPartitionHandler partitionHandler =
new DeployerPartitionHandler(taskLauncher, jobExplorer, resource, "workerStep");
List commandLineArgs = new ArrayList<>(3);
commandLineArgs.add("--spring.profiles.active=worker");
commandLineArgs.add("--spring.cloud.task.initialize.enable=false");
commandLineArgs.add("--spring.batch.initializer.enabled=false");
partitionHandler.setCommandLineArgsProvider(
new PassThroughCommandLineArgsProvider(commandLineArgs));
partitionHandler.setEnvironmentVariablesProvider(new NoOpEnvironmentVariablesProvider());
partitionHandler.setMaxWorkers(2);
partitionHandler.setApplicationName("PartitionedBatchJobTask");
return partitionHandler;
}
```
| |当将环境变量传递给分区时,每个分区可能
位于具有不同环境设置的不同机器上。因此,你应该仅传递所需的那些环境变量。|
|---|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
请注意,在上面的示例中,我们将工人的最大数量设置为 2。设置工人的最大值可以确定一次应该运行的分区的最大数量。
要执行的`Resource`预计是一个 Spring Bootüber- jar,其`DeployerStepExecutionHandler`在当前上下文中被配置为`CommandLineRunner`。前面示例中列举的存储库应该是 über- jar 所在的远程存储库。Manager 和 Worker 都应该对作为作业存储库和任务存储库使用的同一数据存储具有可见性。一旦底层基础结构引导了 Spring boot jar,并且 Spring boot 启动了`DeployerStepExecutionHandler`,步骤处理程序将执行请求的`Step`。下面的示例展示了如何配置`DeployerStepExecutionHandler`:
```
@Bean
public DeployerStepExecutionHandler stepExecutionHandler(JobExplorer jobExplorer) {
DeployerStepExecutionHandler handler =
new DeployerStepExecutionHandler(this.context, jobExplorer, this.jobRepository);
return handler;
}
```
| |你可以在
Spring Cloud任务项目的样例模块[here](https://github.com/spring-cloud/spring-cloud-task/tree/master/spring-cloud-task-samples/partitioned-batch-job)中找到一个样例远程分区应用程序。|
|---|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
### [](#notes-on-developing-a-batch-partitioned-application-for-the-kubernetes-platform)[10.1.关于为 Kubernetes 平台开发批处理分区应用程序的说明](#notes-on-developing-a-batch-partitioned-application-for-the-kubernetes-platform)
* 在 Kubernetes 平台上部署分区应用程序时,对于 Spring Cloud Kubernetes 部署程序,必须使用以下依赖项:
```
org.springframework.cloud
spring-cloud-starter-deployer-kubernetes
```
* 任务应用程序及其分区的应用程序名称需要遵循以下正则表达式模式:`[a-z0-9]([-a-z0-9]*[a-z0-9])`。否则,将抛出异常。
### [](#notes-on-developing-a-batch-partitioned-application-for-the-cloud-foundry-platform)[10.2.为 Cloud Foundry 平台开发批处理分区应用程序的注意事项](#notes-on-developing-a-batch-partitioned-application-for-the-cloud-foundry-platform)
* 在 Cloud Foundry 平台上部署分区应用程序时,对于 Spring Cloud Foundry 部署人员,你必须使用以下依赖关系:
```
org.springframework.cloud
spring-cloud-deployer-cloudfoundry
io.projectreactor
reactor-core
3.1.5.RELEASE
io.projectreactor.ipc
reactor-netty
0.7.5.RELEASE
```
* 在配置分区处理程序时,需要建立 Cloud Foundry 部署环境变量,以便分区处理程序可以启动分区。下面的列表显示了所需的环境变量:
* `spring_cloud_deployer_cloudfoundry_url`
* `spring_cloud_deployer_cloudfoundry_org`
* `spring_cloud_deployer_cloudfoundry_space`
* `spring_cloud_deployer_cloudfoundry_domain`
* `spring_cloud_deployer_cloudfoundry_username`
* `spring_cloud_deployer_cloudfoundry_password`
* `spring_cloud_deployer_cloudfoundry_services`
* `spring_cloud_deployer_cloudfoundry_taskTimeout`
使用`mysql`数据库服务的分区任务的部署环境变量示例集可能类似于以下内容:
```
spring_cloud_deployer_cloudfoundry_url=https://api.local.pcfdev.io
spring_cloud_deployer_cloudfoundry_org=pcfdev-org
spring_cloud_deployer_cloudfoundry_space=pcfdev-space
spring_cloud_deployer_cloudfoundry_domain=local.pcfdev.io
spring_cloud_deployer_cloudfoundry_username=admin
spring_cloud_deployer_cloudfoundry_password=admin
spring_cloud_deployer_cloudfoundry_services=mysql
spring_cloud_deployer_cloudfoundry_taskTimeout=300
```
| |当使用 PCF-Dev 时,还需要以下环境变量:`spring_cloud_deployer_cloudfoundry_skipSslValidation=true`|
|---|-----------------------------------------------------------------------------------------------------------------------------------|
## [](#batch-informational-messages)[11.批处理信息消息](#batch-informational-messages)
Spring Cloud任务提供了批处理作业发送信息消息的能力。“[Spring Batch Events](#stream-integration-batch-events)”部分详细介绍了此功能。
## [](#batch-failures-and-tasks)[12.批处理作业退出代码](#batch-failures-and-tasks)
正如[earlier](#features-lifecycle-exit-codes)所讨论的, Spring Cloud任务应用程序支持记录任务执行的退出代码的能力。然而,在任务中运行 Spring 批处理作业的情况下,无论批处理作业如何执行,当使用默认的批处理/引导行为时,任务的结果始终为零。请记住,任务是一个引导应用程序,从该任务返回的退出代码与引导应用程序相同。要重写此行为并允许任务在批处理作业返回`FAILED`的[BatchStatus](https://docs.spring.io/spring-batch/4.0.x/reference/html/step.html#batchStatusVsExitStatus)时返回除零以外的退出代码,请将`spring.cloud.task.batch.fail-on-job-failure`设置为`true`。然后退出代码可以是 1(默认)或基于[指定的`ExitCodeGenerator`](https://DOCS. Spring.io/ Spring-boot/DOCS/current/reference/html/boot-features- Spring-application.html#boot-features-application-exit))))
这个功能使用了一个新的`CommandLineRunner`,它取代了 Spring boot 提供的功能。默认情况下,它的配置顺序相同。但是,如果你希望自定义运行`CommandLineRunner`的顺序,则可以通过设置`spring.cloud.task.batch.commandLineRunnerOrder`属性来设置其顺序。要让你的任务返回基于批处理作业执行结果的退出代码,你需要编写自己的`CommandLineRunner`。
# [](#batch-job-starter)[单步批处理作业启动器](#batch-job-starter)
本节将讨论如何通过使用 Spring Cloud任务中包含的启动器来开发具有单个`Step`的 Spring 批处理`Job`。这个启动器允许你使用配置来定义`ItemReader`、`ItemWriter`或完整的单步 Spring 批处理`Job`。有关 Spring 批处理及其功能的更多信息,请参见[Spring Batch documentation](https://spring.io/projects/spring-batch)。
要获得 Maven 的启动器,请在构建中添加以下内容:
```
org.springframework.cloud
spring-cloud-starter-single-step-batch-job
2.3.0
```
要获得 Gradle 的启动器,请在构建中添加以下内容:
```
compile "org.springframework.cloud:spring-cloud-starter-single-step-batch-job:2.3.0"
```
## [](#job-definition)[13.定义工作](#job-definition)
你可以使用 starter 来定义很少的`ItemReader`或`ItemWriter`,也可以定义很多的`Job`。在本节中,我们将定义需要定义哪些属性来配置`Job`。
### [](#job-definition-properties)[13.1.属性](#job-definition-properties)
首先,Starter 提供了一组属性,让你只需一步就可以配置作业的基本知识:
| Property | Type |Default Value|说明|
|----------------------------|---------|-------------|----------------------------------------------------|
| `spring.batch.job.jobName` |`String` | `null` |工作的名字。|
|`spring.batch.job.stepName` |`String` | `null` |步骤的名称。|
|`spring.batch.job.chunkSize`|`Integer`| `null` |每笔交易要处理的项目数量。|
配置了上述属性后,你就可以使用一个基于块的步骤来执行作业了。这个基于块的步骤读取、处理和写入`Map`实例作为项。然而,这一步还没有起到任何作用。你需要配置一个`ItemReader`、一个可选的`ItemProcessor`和一个`ItemWriter`,以便让它做一些事情。要配置其中之一,你可以使用属性并配置已提供自动配置的选项之一,也可以使用标准 Spring 配置机制配置你自己的选项。
| |如果你配置自己的,则输入和输出类型必须与步骤中的其他类型匹配。
在此启动器中的`ItemReader`实现和`ItemWriter`实现都使用
a`Map`作为输入和输出项。|
|---|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
## [](#item-readers)[14.ItemReader 实现的自动配置](#item-readers)
此启动器为四种不同的`ItemReader`实现提供自动配置:`AmqpItemReader`、`FlatFileItemReader`、`JdbcCursorItemReader`和`KafkaItemReader`。在本节中,我们将概述如何通过使用提供的自动配置来配置其中的每一个。
### [](#amqpitemreader)[14.1.AMQPitemReader](#amqpitemreader)
你可以使用`AmqpItemReader`使用 AMQP 读取队列或主题。这个`ItemReader`实现的自动配置依赖于两组配置。第一个是`AmqpTemplate`的配置。你可以自己对此进行配置,也可以使用 Spring Boot 提供的自动配置。参见[Spring Boot AMQP documentation](https://docs.spring.io/spring-boot/docs/2.4.x/reference/htmlsingle/#boot-features-amqp)。一旦配置了`AmqpTemplate`,就可以通过设置以下属性来启用批处理功能来支持它:
| Property | Type |Default Value|说明|
|------------------------------------------------------|---------|-------------|---------------------------------------------------------------------------------------|
| `spring.batch.job.amqpitemreader.enabled` |`boolean`| `false` |如果`true`,则自动配置将执行。|
|`spring.batch.job.amqpitemreader.jsonConverterEnabled`|`boolean`| `true` |指示是否应注册`Jackson2JsonMessageConverter`以解析消息。|
有关更多信息,请参见[`AmqpItemReader`文档](https://DOCS. Spring.io/ Spring-batch/DOCS/4.3.x/api/org/springframework/batch/item/amqp/amqpitemreader.html)。
### [](#flatfileitemreader)[14.2.平板文件阅读器](#flatfileitemreader)
`FlatFileItemReader`允许你从平面文件(例如 CSV 和其他文件格式)进行读取。要从文件中读取,你可以通过正常的 Spring 配置(`LineTokenizer`,`RecordSeparatorPolicy`,`FieldSetMapper`,`LineMapper`,或`SkippedLinesCallback`)自己提供一些组件。你还可以使用以下属性来配置阅读器:
| Property | Type | Default Value |说明|
|------------------------------------------------------|---------------|------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `spring.batch.job.flatfileitemreader.saveState` | `boolean` | `true` |确定是否应将状态保存以重新启动。|
| `spring.batch.job.flatfileitemreader.name` | `String` | `null` |用于在`ExecutionContext`中提供唯一键的名称。|
| `spring.batch.job.flatfileitemreader.maxItemcount` | `int` | `Integer.MAX_VALUE` |从文件中读取的项目的最大数量。|
|`spring.batch.job.flatfileitemreader.currentItemCount`| `int` | 0 |已读项目的数量。用于重启。|
| `spring.batch.job.flatfileitemreader.comments` |`List` | empty List |指示文件中已注释的行(要忽略的行)的字符串列表。|
| `spring.batch.job.flatfileitemreader.resource` | `Resource` | `null` |要读取的资源。|
| `spring.batch.job.flatfileitemreader.strict` | `boolean` | `true` |如果设置为`true`,则在未找到资源的情况下,读取器将抛出一个异常。|
| `spring.batch.job.flatfileitemreader.encoding` | `String` | `FlatFileItemReader.DEFAULT_CHARSET` |读取文件时使用的编码。|
| `spring.batch.job.flatfileitemreader.linesToSkip` | `int` | 0 |指示文件开始时要跳过的行数。|
| `spring.batch.job.flatfileitemreader.delimited` | `boolean` | `false` |指示该文件是否为分隔文件(CSV 和其他格式)。此属性中只有一个或`spring.batch.job.flatfileitemreader.fixedLength`可以同时是`true`。|
| `spring.batch.job.flatfileitemreader.delimiter` | `String` | `DelimitedLineTokenizer.DELIMITER_COMMA` |如果读取分隔符文件,则指示要解析的分隔符。|
| `spring.batch.job.flatfileitemreader.quoteCharacter` | `char` |`DelimitedLineTokenizer.DEFAULT_QUOTE_CHARACTER`|用于确定用于引用值的字符。|
| `spring.batch.job.flatfileitemreader.includedFields` |`List`| empty list |一个索引列表,用于确定记录中的哪些字段要包含在项中。|
| `spring.batch.job.flatfileitemreader.fixedLength` | `boolean` | `false` |表示文件的记录是否由列号解析。此属性中只有一个或`spring.batch.job.flatfileitemreader.delimited`可以同时是`true`。|
| `spring.batch.job.flatfileitemreader.ranges` | `List` | empty list |用于解析固定宽度记录的列范围列表。参见[范围文档](https://docs.spring.io/spring-batch/docs/4.3.x/api/org/springframework/batch/item/file/transform/Range.html)。|
| `spring.batch.job.flatfileitemreader.names` | `String []` | `null` |从记录中解析的每个字段的名称列表。这些名称是从这个`ItemReader`返回的项中的`Map`中的键。|
| `spring.batch.job.flatfileitemreader.parsingStrict` | `boolean` | `true` |如果设置为`true`,则如果无法映射字段,则映射失败。|
参见[`FlatFileItemReader`文档](https://DOCS. Spring.io/ Spring-batch/DOCS/4.3.x/api/org/springframework/batch/item/file/flatfileitemreader.html)。
### [](#jdbcCursorItemReader)[14.3.JDBCCursoritemReader](#jdbcCursorItemReader)
`JdbcCursorItemReader`对关系数据库运行查询,并在结果游标(`ResultSet`)上进行迭代,以提供结果项。此自动配置允许你提供`PreparedStatementSetter`、`RowMapper`或两者。你还可以使用以下属性来配置`JdbcCursorItemReader`:
| Property | Type | Default Value |说明|
|-------------------------------------------------------------------|---------|-------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `spring.batch.job.jdbccursoritemreader.saveState` |`boolean`| `true` |确定是否应将状态保存以重新启动。|
| `spring.batch.job.jdbccursoritemreader.name` |`String` | `null` |用于在`ExecutionContext`中提供唯一密钥的名称。|
| `spring.batch.job.jdbccursoritemreader.maxItemcount` | `int` |`Integer.MAX_VALUE`|从文件中读取的项目的最大数量。|
| `spring.batch.job.jdbccursoritemreader.currentItemCount` | `int` | 0 |已读项目的数量。用于重启。|
| `spring.batch.job.jdbccursoritemreader.fetchSize` | `int` | |给驱动程序的一个提示,指示每次调用数据库系统要检索多少记录。为了获得最佳性能,你通常希望将其设置为与块大小匹配。|
| `spring.batch.job.jdbccursoritemreader.maxRows` | `int` | |从数据库中读取的项数的最大值。|
| `spring.batch.job.jdbccursoritemreader.queryTimeout` | `int` | |查询超时的毫秒数。|
| `spring.batch.job.jdbccursoritemreader.ignoreWarnings` |`boolean`| `true` |确定读取器在处理时是否应忽略 SQL 警告。|
| `spring.batch.job.jdbccursoritemreader.verifyCursorPosition` |`boolean`| `true` |指示是否应在每次读取后验证光标的位置,以验证`RowMapper`没有使光标前进。|
| `spring.batch.job.jdbccursoritemreader.driverSupportsAbsolute` |`boolean`| `false` |指示驱动程序是否支持光标的绝对定位。|
|`spring.batch.job.jdbccursoritemreader.useSharedExtendedConnection`|`boolean`| `false` |指示连接是否与其他处理共享(因此是事务的一部分)。|
| `spring.batch.job.jdbccursoritemreader.sql` |`String` | `null` |要读取的 SQL 查询。|
参见[`JdbcCursorItemReader`文档](https://DOCS. Spring.io/ Spring-batch/DOCS/4.3.x/api/org/springframework/batch/item/database/jdbcccursoritemreader.html)。
### [](#kafkaItemReader)[14.4.Kafkaitemreader](#kafkaItemReader)
从 Kafka 主题中获取数据分区是有用的,也正是`KafkaItemReader`所能做的。要配置`KafkaItemReader`,需要进行两部分配置。首先,需要使用 Spring Boot 的 Kafka 自动配置来配置 Kafka(参见[Spring Boot Kafka documentation](https://docs.spring.io/spring-boot/docs/2.4.x/reference/htmlsingle/#boot-features-kafka))。在配置了 Spring boot 中的 Kafka 属性之后,可以通过设置以下属性来配置`KafkaItemReader`本身:
| Property | Type |Default Value|说明|
|-------------------------------------------------------|---------------|-------------|-----------------------------------------------------------|
| `spring.batch.job.kafkaitemreader.name` | `String` | `null` |用于在`ExecutionContext`中提供唯一键的名称。|
| `spring.batch.job.kafkaitemreader.topic` | `String` | `null` |阅读主题的名称。|
| `spring.batch.job.kafkaitemreader.partitions` |`List`| empty list |要读取的分区索引的列表。|
|`spring.batch.job.kafkaitemreader.pollTimeOutInSeconds`| `long` | 30 |`poll()`操作的超时。|
| `spring.batch.job.kafkaitemreader.saveState` | `boolean` | `true` |确定是否应将状态保存以重新启动。|
参见[`KafkaItemReader`文档](https://DOCS. Spring.io/ Spring-batch/DOCS/4.3.x/api/org/springframework/batch/item/kafka/kafkaitemreader.html)。
## [](#item-processors)[15.项目处理器配置](#item-processors)
如果`ApplicationContext`中有一个选项可用,那么单步批处理作业自动配置接受`ItemProcessor`。如果找到了正确的类型(`ItemProcessor