Features

本节将更详细地介绍 Spring Cloud Task,包括如何使用、如何配置它以及合适的扩展点。

This section goes into more detail about Spring Cloud Task, including how to use it, how to configure it, and the appropriate extension points.

The lifecycle of a Spring Cloud Task

在大多数情况下,现代云环境是围绕执行预期不会结束的进程而设计的。如果它们结束,通常会重新启动它们。虽然大多数平台确实有一定方式可以运行一个在结束时不会重新启动的进程,但该运行的结果通常不会以可消耗的方式维护。Spring Cloud Task 提供了在环境中执行短期进程并记录结果的能力。通过消息集成任务,这样做允许围绕短期进程以及更长时间运行的服务构建微服务架构。

In most cases, the modern cloud environment is designed around the execution of processes that are not expected to end. If they do end, they are typically restarted. While most platforms do have some way to run a process that is not restarted when it ends, the results of that run are typically not maintained in a consumable way. Spring Cloud Task offers the ability to execute short-lived processes in an environment and record the results. Doing so allows for a microservices architecture around short-lived processes as well as longer running services through the integration of tasks by messages.

虽然此功能在云环境中很有用,但同样的问题也可能出现在传统的部署模型中。在使用诸如 cron 之类的调度程序运行 Spring Boot 应用程序时,能够在完成应用程序后监控其结果会很有用。

While this functionality is useful in a cloud environment, the same issues can arise in a traditional deployment model as well. When running Spring Boot applications with a scheduler such as cron, it can be useful to be able to monitor the results of the application after its completion.

Spring Cloud Task 采取一种方法,即 Spring Boot 应用程序可以有开始和结束,并且仍然成功。批量应用程序就是一个示例,说明预期结束(并且通常是短期的)的进程如何派上用场。

Spring Cloud Task takes the approach that a Spring Boot application can have a start and an end and still be successful. Batch applications are one example of how processes that are expected to end (and that are often short-lived) can be helpful.

Spring Cloud Task 记录给定任务的生命周期事件。大多数长时间运行的进程(大多数 web 应用程序的典型特征)不会保存它们的生命周期事件。Spring Cloud Task 核心中的任务会保存。

Spring Cloud Task records the lifecycle events of a given task. Most long-running processes, typified by most web applications, do not save their lifecycle events. The tasks at the heart of Spring Cloud Task do.

生命周期包括一个单独的任务执行。这是配置为任务(即它具有 Sprint Cloud Task 依赖项)的 Spring Boot 应用程序的物理执行。

The lifecycle consists of a single task execution. This is a physical execution of a Spring Boot application configured to be a task (that is, it has the Sprint Cloud Task dependencies).

在任务开始时,在运行任何 CommandLineRunnerApplicationRunner 实现之前,将创建 TaskRepository 中记录开始事件的条目。此事件通过 Spring Framework 触发 SmartLifecycle#start 触发。这向系统指示所有 bean 均已准备就绪,并且在运行 Spring Boot 提供的任何 CommandLineRunnerApplicationRunner 实现之前。

At the beginning of a task, before any CommandLineRunner or ApplicationRunner implementations have been run, an entry in the TaskRepository that records the start event is created. This event is triggered through SmartLifecycle#start being triggered by the Spring Framework. This indicates to the system that all beans are ready for use and comes before running any of the CommandLineRunner or ApplicationRunner implementations provided by Spring Boot.

任务的记录只在 ApplicationContext 的成功引导时发生。如果上下文的引导完全失败,则任务的运行不会被记录。

The recording of a task only occurs upon the successful bootstrapping of an ApplicationContext. If the context fails to bootstrap at all, the task’s run is not recorded.

完成 Spring Boot 或 ApplicationContext 的所有 *Runner#run 调用(由 ApplicationFailedEvent 指示)后,任务执行将在存储库中更新结果。

Upon completion of all of the *Runner#run calls from Spring Boot or the failure of an ApplicationContext (indicated by an ApplicationFailedEvent), the task execution is updated in the repository with the results.

如果应用程序要求任务完成后关闭 ApplicationContext(所有 *Runner#run 方法都已调用,并且已更新任务存储库),请将属性 spring.cloud.task.closecontextEnabled 设置为 true。

If the application requires the ApplicationContext to be closed at the completion of a task (all *Runner#run methods have been called and the task repository has been updated), set the property spring.cloud.task.closecontextEnabled to true.

The TaskExecution

存储在 TaskRepository 中的信息在 TaskExecution 类中建模,并包含以下信息:

The information stored in the TaskRepository is modeled in the TaskExecution class and consists of the following information:

Field Description

executionid

The unique ID for the task’s run.

exitCode

The exit code generated from an ExitCodeExceptionMapper implementation. If there is no exit code generated but an ApplicationFailedEvent is thrown, 1 is set. Otherwise, it is assumed to be 0.

taskName

The name for the task, as determined by the configured TaskNameResolver.

startTime

The time the task was started, as indicated by the SmartLifecycle#start call.

endTime

The time the task was completed, as indicated by the ApplicationReadyEvent.

exitMessage

Any information available at the time of exit. This can programmatically be set by a TaskExecutionListener.

errorMessage

If an exception is the cause of the end of the task (as indicated by an ApplicationFailedEvent), the stack trace for that exception is stored here.

arguments

A List of the string command line arguments as they were passed into the executable boot application.

Mapping Exit Codes

任务完成后,它会尝试向操作系统返回退出代码。如果我们看一下我们的 original example,我们可以看到我们无法控制我们应用程序的该方面。因此,如果抛出异常,JVM 会返回一个代码,但该代码对于你的调试来说可能有用或无用。

When a task completes, it tries to return an exit code to the OS. If we take a look at our original example, we can see that we are not controlling that aspect of our application. So, if an exception is thrown, the JVM returns a code that may or may not be of any use to you in debugging.

因此,Spring Boot 提供了一个界面 ExitCodeExceptionMapper,它允许你将未捕获的异常映射到退出代码。这样做让你能够以退出代码的级别指示出现什么问题。此外,通过这种方式映射退出代码,Spring Cloud Task 记录返回的退出代码。

Consequently, Spring Boot provides an interface, ExitCodeExceptionMapper, that lets you map uncaught exceptions to exit codes. Doing so lets you indicate, at the level of exit codes, what went wrong. Also, by mapping exit codes in this manner, Spring Cloud Task records the returned exit code.

如果任务以 SIG-INT 或 SIG-TERM 终止,则退出代码为零,除非代码中另有指定。

If the task terminates with a SIG-INT or a SIG-TERM, the exit code is zero unless otherwise specified within the code.

在任务运行期间,退出代码作为 null 存储在存储库中。任务完成后,将根据本节前面描述的准则存储适当的退出代码。

While the task is running, the exit code is stored as a null in the repository. Once the task completes, the appropriate exit code is stored based on the guidelines described earlier in this section.

Configuration

Spring Cloud Task 提供了开箱即用的配置,如 DefaultTaskConfigurerSimpleTaskConfiguration 类中定义的配置。本节将介绍默认值以及如何根据需要自定义 Spring Cloud Task。

Spring Cloud Task provides a ready-to-use configuration, as defined in the DefaultTaskConfigurer and SimpleTaskConfiguration classes. This section walks through the defaults and how to customize Spring Cloud Task for your needs.

DataSource

Spring Cloud Task 使用数据源存储任务执行的结果。默认情况下,我们提供 H2 的内存中实例,以提供一种简单的引导开发方法。但是,在生产环境中,你可能希望配置自己的 DataSource

Spring Cloud Task uses a datasource for storing the results of task executions. By default, we provide an in-memory instance of H2 to provide a simple method of bootstrapping development. However, in a production environment, you probably want to configure your own DataSource.

如果你的应用只使用一个 DataSource,既可以用作业务模式,又可以用作任务存储库,那么你只需提供一个 DataSource(最简单的方式是通过 Spring Boot 的配置约定)。这个 DataSource 会被 Spring Cloud Task 自动用作存储库。

If your application uses only a single DataSource and that serves as both your business schema and the task repository, all you need to do is provide any DataSource (the easiest way to do so is through Spring Boot’s configuration conventions). This DataSource is automatically used by Spring Cloud Task for the repository.

如果你的应用使用多个 DataSource,则需要使用合适的 DataSource 来配置任务存储库。这种自定义可以通过 TaskConfigurer 的实现来完成。

If your application uses more than one DataSource, you need to configure the task repository with the appropriate DataSource. This customization can be done through an implementation of TaskConfigurer.

Table Prefix

TaskRepository 可修改的属性之一是任务表的表前缀。默认情况下,所有前缀都是 TASK_TASK_EXECUTIONTASK_EXECUTION_PARAMS 就是两个例子。不过,可能出于多种原因需要修改此前缀。如果需要将模式名称添加到表名之前,或者在同一模式中需要多组任务表,你必须更改表前缀。你可以通过将 spring.cloud.task.tablePrefix 设置为所需的表前缀来完成此操作,如下所示:

One modifiable property of TaskRepository is the table prefix for the task tables. By default, they are all prefaced with TASK_. TASK_EXECUTION and TASK_EXECUTION_PARAMS are two examples. However, there are potential reasons to modify this prefix. If the schema name needs to be prepended to the table names or if more than one set of task tables is needed within the same schema, you must change the table prefix. You can do so by setting the spring.cloud.task.tablePrefix to the prefix you need, as follows:

spring.cloud.task.tablePrefix=yourPrefix

通过使用 spring.cloud.task.tablePrefix,用户承担责任来创建符合任务表架构标准的任务表,但会做出用户业务需要的一些修改。在创建自己的任务 DDL 时,您可以将 Spring Cloud 任务架构 DDL 用作指南,如 here 所示。

By using the spring.cloud.task.tablePrefix, a user assumes the responsibility to create the task tables that meet both the criteria for the task table schema but with modifications that are required for a user’s business needs. You can utilize the Spring Cloud Task Schema DDL as a guide when creating your own Task DDL as seen here.

Enable/Disable table initialization

如果你要创建任务表,但不希望 Spring Cloud Task 在任务启动时创建这些表,请将 spring.cloud.task.initialize-enabled 属性设置为 false,如下所示:

In cases where you are creating the task tables and do not wish for Spring Cloud Task to create them at task startup, set the spring.cloud.task.initialize-enabled property to false, as follows:

spring.cloud.task.initialize-enabled=false

默认值为 true

It defaults to true.

属性 spring.cloud.task.initialize.enable 已弃用。

The property spring.cloud.task.initialize.enable has been deprecated.

Externally Generated Task ID

在某些情况下,你可能希望允许在请求任务以及基础架构实际启动任务之间的时间差。Spring Cloud Task 让你可以在请求任务时创建 TaskExecution。然后,将生成 TaskExecution 的执行 ID 传递给任务,以便它可以在任务的生命周期中更新 TaskExecution

In some cases, you may want to allow for the time difference between when a task is requested and when the infrastructure actually launches it. Spring Cloud Task lets you create a TaskExecution when the task is requested. Then pass the execution ID of the generated TaskExecution to the task so that it can update the TaskExecution through the task’s lifecycle.

可以通过调用引用存储 TaskExecution 对象的数据存储的 TaskRepository 实现中的 createTaskExecution 方法来创建一个 TaskExecution

A TaskExecution can be created by calling the createTaskExecution method on an implementation of the TaskRepository that references the datastore that holds the TaskExecution objects.

为了配置你的任务以使用生成的 TaskExecutionId,添加如下属性:

In order to configure your Task to use a generated TaskExecutionId, add the following property:

spring.cloud.task.executionid=yourtaskId

External Task Id

Spring Cloud Task 让你可以为每个 TaskExecution 存储一个外部任务 ID。为了配置你的任务以使用生成的 TaskExecutionId,添加如下属性:

Spring Cloud Task lets you store an external task ID for each TaskExecution. In order to configure your Task to use a generated TaskExecutionId, add the following property:

spring.cloud.task.external-execution-id=<externalTaskId>

Parent Task Id

Spring Cloud Task 让你可以为每个 TaskExecution 存储一个父任务 ID。例如,执行另一个任务或任务的任务,并且你想要记录哪个任务启动了各个子任务。为了配置你的任务以设置父 TaskExecutionId,请在子任务中添加如下属性:

Spring Cloud Task lets you store a parent task ID for each TaskExecution. An example of this would be a task that executes another task or tasks and you want to record which task launched each of the child tasks. In order to configure your Task to set a parent TaskExecutionId add the following property on the child task:

spring.cloud.task.parent-execution-id=<parentExecutionTaskId>

TaskConfigurer

TaskConfigurer 是一个策略接口,让你可以自定义配置 Spring Cloud Task 组件的方法。默认情况下,我们提供提供的 DefaultTaskConfigurer,它提供逻辑默认值:基于 Map 的内存组件(如果未提供 DataSource,则对开发很有用)和基于 JDBC 的组件(如果 DataSource 可用,则很有用)。

The TaskConfigurer is a strategy interface that lets you customize the way components of Spring Cloud Task are configured. By default, we provide the DefaultTaskConfigurer that provides logical defaults: Map-based in-memory components (useful for development if no DataSource is provided) and JDBC based components (useful if there is a DataSource available).

TaskConfigurer 让你可以配置三个主要组件:

The TaskConfigurer lets you configure three main components:

Component Description Default (provided by DefaultTaskConfigurer)

TaskRepository

The implementation of the TaskRepository to be used.

SimpleTaskRepository

TaskExplorer

The implementation of the TaskExplorer (a component for read-only access to the task repository) to be used.

SimpleTaskExplorer

PlatformTransactionManager

A transaction manager to be used when running updates for tasks.

JdbcTransactionManager if a DataSource is used. ResourcelessTransactionManager if it is not.

你可以通过创建 TaskConfigurer 接口的自定义实现来自定义前表中描述的任何组件。通常,扩展 DefaultTaskConfigurer(在找不到 TaskConfigurer 时提供)并重写所需的 getter 就可以了。不过,这可能还需要从头开始实现你自己的任务。

You can customize any of the components described in the preceding table by creating a custom implementation of the TaskConfigurer interface. Typically, extending the DefaultTaskConfigurer (which is provided if a TaskConfigurer is not found) and overriding the required getter is sufficient. However, implementing your own from scratch may be required.

除非用户正在使用它来提供要作为 Spring Bean 公开的实现,否则不应直接使用 TaskConfigurer 的 getter 方法。

Users should not directly use getter methods from a TaskConfigurer directly unless they are using it to supply implementations to be exposed as Spring Beans.

Task Execution Listener

TaskExecutionListener 让你可以为任务生命周期中发生的特定事件注册侦听器。要进行此操作,请创建一个实现 TaskExecutionListener 接口的类。实现 TaskExecutionListener 接口的类会收到以下事件的通知:

TaskExecutionListener lets you register listeners for specific events that occur during the task lifecycle. To do so, create a class that implements the TaskExecutionListener interface. The class that implements the TaskExecutionListener interface is notified of the following events:

  • onTaskStartup: Prior to storing the TaskExecution into the TaskRepository.

  • onTaskEnd: Prior to updating the TaskExecution entry in the TaskRepository and marking the final state of the task.

  • onTaskFailed: Prior to the onTaskEnd method being invoked when an unhandled exception is thrown by the task.

Spring Cloud Task 也允许您使用以下方法注释将 TaskExecution 事件侦听器添加到 bean 中的方法:

Spring Cloud Task also lets you add TaskExecution Listeners to methods within a bean by using the following method annotations:

  • @BeforeTask: Prior to the storing the TaskExecution into the TaskRepository

  • @AfterTask: Prior to the updating of the TaskExecution entry in the TaskRepository marking the final state of the task.

  • @FailedTask: Prior to the @AfterTask method being invoked when an unhandled exception is thrown by the task.

以下示例展示了三种注释的用法:

The following example shows the three annotations in use:

 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 可能会导致意外的影响。

Inserting an ApplicationListener earlier in the chain than TaskLifecycleListener exists may cause unexpected effects.

Exceptions Thrown by Task Execution Listener

如果 TaskExecutionListener 事件处理程序引发异常,则该事件处理程序的所有侦听器处理将停止。例如,如果三个 onTaskStartup 侦听器已经启动,并且第一个 onTaskStartup 事件处理程序引发异常,则不会调用其他两个 onTaskStartup 方法。但是,TaskExecutionListeners 的其他事件处理程序(onTaskEndonTaskFailed)将被调用。

If an exception is thrown by a TaskExecutionListener event handler, all listener processing for that event handler stops. For example, if three onTaskStartup listeners have started and the first onTaskStartup event handler throws an exception, the other two onTaskStartup methods are not called. However, the other event handlers (onTaskEnd and onTaskFailed) for the TaskExecutionListeners are called.

TaskExecutionListener`事件处理程序抛出异常时返回的退出代码是 ExitCodeEvent 报告的退出代码。如果没有发出 `ExitCodeEvent,则会评估抛出的异常以查看它是否为 ExitCodeGenerator 类型。如果是,则它返回来自 ExitCodeGenerator 的退出代码。否则,返回 1

The exit code returned when a exception is thrown by a TaskExecutionListener event handler is the exit code that was reported by the ExitCodeEvent. If no ExitCodeEvent is emitted, the Exception thrown is evaluated to see if it is of type ExitCodeGenerator. If so, it returns the exit code from the ExitCodeGenerator. Otherwise, 1 is returned.

如果在 onTaskStartup 方法中引发异常,则该应用程序的退出代码将为 1。如果在 onTaskEndonTaskFailed 方法中引发异常,则该应用程序的退出代码将是使用上述规则确立的代码。

In the case that an exception is thrown in an onTaskStartup method, the exit code for the application will be 1. If an exception is thrown in either a onTaskEnd or onTaskFailed method, the exit code for the application will be the one established using the rules enumerated above.

如果在 onTaskStartuponTaskEndonTaskFailed 中抛出异常,则无法使用 ExitCodeExceptionMapper 覆盖应用程序的退出代码。

In the case of an exception being thrown in a onTaskStartup, onTaskEnd, or onTaskFailed you can not override the exit code for the application using ExitCodeExceptionMapper.

Exit Messages

您可以使用 TaskExecutionListener 以编程方式设置任务的退出消息。这是通过设置 TaskExecution’s exitMessage 完成的,然后将其传递给 TaskExecutionListener。以下示例展示了使用 @AfterTask ExecutionListener 注释的方法:

You can set the exit message for a task programmatically by using a TaskExecutionListener. This is done by setting the TaskExecution’s exitMessage, which then gets passed into the TaskExecutionListener. The following example shows a method that is annotated with the @AfterTask ExecutionListener :

@AfterTask
public void afterMe(TaskExecution taskExecution) {
    taskExecution.setExitMessage("AFTER EXIT MESSAGE");
}

可以在任何侦听器事件(onTaskStartuponTaskFailedonTaskEnd)设置 ExitMessage。三个侦听器的优先级顺序如下:

An ExitMessage can be set at any of the listener events (onTaskStartup, onTaskFailed, and onTaskEnd). The order of precedence for the three listeners follows:

  1. onTaskEnd

  2. onTaskFailed

  3. onTaskStartup

例如,如果您为 onTaskStartuponTaskFailed 侦听器设置了 exitMessage,并且任务在没有失败的情况下结束,则来自 onTaskStartupexitMessage 将存储在存储库中。否则,如果发生故障,则来自 onTaskFailedexitMessage 将被存储。此外,如果您使用 onTaskEnd 侦听器设置了 exitMessage,则来自 onTaskEndexitMessage 将取代来自 onTaskStartuponTaskFailed 的退出消息。

For example, if you set an exitMessage for the onTaskStartup and onTaskFailed listeners and the task ends without failing, the exitMessage from the onTaskStartup is stored in the repository. Otherwise, if a failure occurs, the exitMessage from the onTaskFailed is stored. Also if you set the exitMessage with an onTaskEnd listener, the exitMessage from the onTaskEnd supersedes the exit messages from both the onTaskStartup and onTaskFailed.

Restricting Spring Cloud Task Instances

Spring Cloud Task 允许您确定一次只能运行一个具有给定任务名称的任务。为此,您需要为每个任务执行建立 features-task-name 并在 cloud-task 中设置 spring.cloud.task.single-instance-enabled=true。虽然第一个任务执行正在运行,但您尝试重新运行具有相同 features-task-namespring.cloud.task.single-instance-enabled=true 的任务的任何其他时间,则该任务将失败,并出现以下错误消息:Task with name "application" is alreadyrunning. spring.cloud.task.single-instance-enabled 的默认值为 false。以下示例展示了如何将 spring.cloud.task.single-instance-enabled 设置为 true

Spring Cloud Task lets you establish that only one task with a given task name can be run at a time. To do so, you need to establish the features-task-name and set spring.cloud.task.single-instance-enabled=true for each task execution. While the first task execution is running, any other time you try to run a task with the same features-task-name and spring.cloud.task.single-instance-enabled=true, the task fails with the following error message: Task with name "application" is already running. The default value for spring.cloud.task.single-instance-enabled is false. The following example shows how to set spring.cloud.task.single-instance-enabled to true:

spring.cloud.task.single-instance-enabled=true or false

要使用此功能,您必须将以下 Spring Integration 依赖项添加到应用程序:

To use this feature, you must add the following Spring Integration dependencies to your application:

<dependency>
    <groupId>org.springframework.integration</groupId>
    <artifactId>spring-integration-core</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.integration</groupId>
    <artifactId>spring-integration-jdbc</artifactId>
</dependency>

如果任务失败是因为启用了此功能并且另一个任务正在使用相同的任务名称运行,则应用程序的退出代码将为 1。

The exit code for the application will be 1 if the task fails because this feature is enabled and another task is running with the same task name.

Single Instance Usage for Spring AOT And Native Compilation

在创建原生编译应用程序时要使用 Spring Cloud Task 的单实例功能,您需要在构建时启用该功能。为此,请添加 process-aot 执行,并按照以下步骤将 spring.cloud.task.single-step-instance-enabled=true 设置为 JVM 参数:

To use Spring Cloud Task’s single-instance feature when creating a natively compiled app, you need to enable the feature at build time. To do so, add the process-aot execution and set spring.cloud.task.single-step-instance-enabled=true as a JVM argument, as follows:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>process-aot</id>
            <goals>
                <goal>process-aot</goal>
            </goals>
            <configuration>
                <jvmArguments>
                    -Dspring.cloud.task.single-instance-enabled=true
                </jvmArguments>
            </configuration>
        </execution>
    </executions>
</plugin>

Enabling Observations for ApplicationRunner and CommandLineRunner

要为 ApplicationRunnerCommandLineRunner 启用任务观察,请将 spring.cloud.task.observation.enabled 设置为 true。

To Enable Task Observations for ApplicationRunner or CommandLineRunner set spring.cloud.task.observation.enabled to true.

带观测功能的示例任务应用程序允许使用 SimpleMeterRegistry 可在 here 中找到。

An example task application with observations enables using the SimpleMeterRegistry can be found here.

Disabling Spring Cloud Task Auto Configuration

如果 Spring Cloud Task 不应针对某个实现进行自动配置,则可以禁用任务的自动配置。这可以通过将以下注释添加到您的任务应用程序来实现:

In cases where Spring Cloud Task should not be autoconfigured for an implementation, you can disable Task’s auto configuration. This can be done either by adding the following annotation to your Task application:

@EnableAutoConfiguration(exclude={SimpleTaskAutoConfiguration.class})

您还可以通过将 spring.cloud.task.autoconfiguration.enabled 属性设置为 false 来禁用 Task 自动配置。

You may also disable Task auto configuration by setting the spring.cloud.task.autoconfiguration.enabled property to false.

Closing the Context

如果应用程序需要在任务完成后关闭 ApplicationContext(所有 *Runner#run 方法都已调用并且任务存储库已更新),请将属性 spring.cloud.task.closecontextEnabled 设置为 true

If the application requires the ApplicationContext to be closed at the completion of a task (all *Runner#run methods have been called and the task repository has been updated), set the property spring.cloud.task.closecontextEnabled to true.

关闭上下文的另一个情况是当任务执行完成但应用程序未终止时。在这些情况下,上下会保持开启状态,因为已分配了一个线程(例如:如果您正在使用 TaskExecutor)。在这些情况下,请在启动任务时将 spring.cloud.task.closecontextEnabled 属性设置为 true。这将在任务完成后关闭应用程序的上下文。从而允许应用程序终止。

Another case to close the context is when the Task Execution completes however the application does not terminate. In these cases the context is held open because a thread has been allocated (for example: if you are using a TaskExecutor). In these cases set the spring.cloud.task.closecontextEnabled property to true when launching your task. This will close the application’s context once the task is complete. Thus allowing the application to terminate.

Enable Task Metrics

Spring Cloud Task 与 Micrometer 集成,并为它执行的任务创建观察。要启用任务可观察性集成,您必须将 spring-boot-starter-actuator、您首选的注册表实现(如果您要发布指标)和 micrometer-tracing(如果您要发布跟踪数据)添加到您的任务应用程序。使用 Influx 启用任务可观察性和指标的一组示例 Maven 依赖项如下:

Spring Cloud Task integrates with Micrometer and creates observations for the Tasks it executes. To enable Task Observability integration, you must add spring-boot-starter-actuator, your preferred registry implementation (if you want to publish metrics), and micrometer-tracing (if you want to publish tracing data) to your task application. An example maven set of dependencies to enable task observability and metrics using Influx would be:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-influx</artifactId>
    <scope>runtime</scope>
</dependency>

Spring Task and Spring Cloud Task Properties

该行业中经常使用术语“task”。在一个这样的示例中,Spring Boot 提供 spring.task,而 Spring Cloud Task 提供 spring.cloud.task 属性。过去,这引起了某种困惑,认为这两组属性直接相关。然而,它们代表了 Spring 生态系统中提供的两组不同的功能。

The term task is frequently used word in the industry. In one such example Spring Boot offers the spring.task while Spring Cloud Task offers the spring.cloud.task properties. This has caused some confusion in the past that these two groups of properties are directly related. However, they represent 2 different set of features offered in the Spring ecosystem.

  • spring.task refers to the properties that configure the ThreadPoolTaskScheduler.

  • spring.cloud.task refers to the properties that configure features of Spring Cloud Task.