此版本仍在开发中,尚未被视为稳定版本。最新的稳定版请使用 spring-cloud-task 3.1.1Spring中文文档

此版本仍在开发中,尚未被视为稳定版本。最新的稳定版请使用 spring-cloud-task 3.1.1Spring中文文档

任务本身可能很有用,但将任务集成到更大的生态系统中可以使其 对于更复杂的处理和业务流程很有用。本节 介绍了 Spring Cloud Task 与 Spring Cloud Stream 的集成选项。Spring中文文档

从 Spring Cloud Stream 启动任务

您可以从流中启动任务。为此,请创建一个侦听消息的接收器 包含 A 作为其有效负载。包含:TaskLaunchRequestTaskLaunchRequestSpring中文文档

  • uri:到要执行的任务工件。Spring中文文档

  • applicationName:与任务关联的名称。如果没有 applicationName 设置后,生成任务名称 由以下部分组成: .TaskLaunchRequestTask-<UUID>Spring中文文档

  • commandLineArguments:包含任务的命令行参数的列表。Spring中文文档

  • environmentProperties:包含要由 任务。Spring中文文档

  • deploymentProperties:包含部署程序用于的属性的映射 部署任务。Spring中文文档

如果有效负载属于其他类型,则接收器将引发异常。

例如,可以创建一个流,该流具有一个处理器,该处理器从 HTTP 源并创建一个包含 和 发送 发送到其输出通道的消息。然后,任务接收器将接收来自其 输入通道,然后启动任务。GenericMessageTaskLaunchRequestSpring中文文档

若要创建 taskSink,只需创建包含注释的 Spring Boot 应用程序,如以下示例所示:EnableTaskLauncherSpring中文文档

@SpringBootApplication
@EnableTaskLauncher
public class TaskSinkApplication {
	public static void main(String[] args) {
		SpringApplication.run(TaskSinkApplication.class, args);
	}
}

示例 Spring Cloud Task 项目的模块包含示例 Sink 和 Processor。安装 这些示例放入本地 Maven 存储库中,从属性设置为 的目录运行 Maven 构建,因为 如以下示例所示:spring-cloud-task-samplesskipInstallfalseSpring中文文档

mvn clean installSpring中文文档

该属性必须设置为位置 Spring Boot Uber-jar 所在的远程存储库。如果未设置,则没有遥控器 repository,因此它仅依赖于本地存储库。maven.remoteRepositories.springRepo.url

Spring Cloud 数据流

要在 Spring Cloud Data Flow 中创建流,必须先注册任务接收器 我们创建的应用程序。在以下示例中,我们将注册处理器和 使用 Spring Cloud Data Flow shell 接收示例应用程序:Spring中文文档

app register --name taskSink --type sink --uri maven://io.spring.cloud:tasksink:<version>
app register --name taskProcessor --type processor --uri maven:io.spring.cloud:taskprocessor:<version>

以下示例显示了如何从 Spring Cloud Data Flow shell 创建流:Spring中文文档

stream create foo --definition "http --server.port=9000|taskProcessor|taskSink" --deploy
如果有效负载属于其他类型,则接收器将引发异常。
该属性必须设置为位置 Spring Boot Uber-jar 所在的远程存储库。如果未设置,则没有遥控器 repository,因此它仅依赖于本地存储库。maven.remoteRepositories.springRepo.url

Spring Cloud 任务事件

Spring Cloud Task 提供了通过 Spring Cloud Stream 发出事件的功能 通道,当任务通过Spring Cloud Stream通道运行时。任务侦听器是 用于在名为 的消息通道上发布 。此功能 自动连接到任何具有 、 、 以及其类路径上定义的任务。TaskExecutiontask-eventsspring-cloud-streamspring-cloud-stream-<binder>Spring中文文档

若要禁用事件发出侦听器,请将该属性设置为 。spring.cloud.task.events.enabledfalse

定义适当的类路径后,以下任务将作为 频道上的事件(在任务的开始和结束时):TaskExecutiontask-eventsSpring中文文档

@SpringBootApplication
public class TaskEventsApplication {

	public static void main(String[] args) {
		SpringApplication.run(TaskEventsApplication.class, args);
	}

	@Configuration
	public static class TaskConfiguration {

		@Bean
		public ApplicationRunner applicationRunner() {
			return new ApplicationRunner() {
				@Override
				public void run(ApplicationArguments args) {
					System.out.println("The ApplicationRunner was executed");
				}
			};
		}
	}
}
类路径上还需要绑定器实现。
可以在示例模块中找到示例任务事件应用程序 Spring Cloud 任务项目,请点击此处

禁用特定任务事件

若要禁用任务事件,可以将该属性设置为 。spring.cloud.task.events.enabledfalseSpring中文文档

若要禁用事件发出侦听器,请将该属性设置为 。spring.cloud.task.events.enabledfalse
类路径上还需要绑定器实现。
可以在示例模块中找到示例任务事件应用程序 Spring Cloud 任务项目,请点击此处

Spring Batch 事件

通过任务执行Spring Batch作业时,Spring Cloud Task可以配置为 根据 Spring Batch 中提供的 Spring Batch 侦听器发出信息性消息。 具体而言,以下 Spring Batch 侦听器将自动配置到每个批处理作业中 并在 Spring 运行时在关联的 Spring Cloud Stream 频道上发出消息 云任务:Spring中文文档

这些侦听器在适当的时候自动配置为任何 bean (a 和 a) 存在于上下文中。配置为 侦听这些事件的处理方式与绑定到任何其他 Spring 的方式相同 Cloud Stream 通道已完成。我们的任务(运行批处理作业的任务)充当 ,侦听应用程序充当 或 。AbstractJobJobTaskLifecycleListenerSourceProcessorSinkSpring中文文档

例如,让应用程序侦听通道 用于作业的开始和停止。要配置侦听应用程序,您需要 将输入配置如下:job-execution-eventsjob-execution-eventsSpring中文文档

spring.cloud.stream.bindings.input.destination=job-execution-eventsSpring中文文档

类路径上还需要绑定器实现。
可以在示例模块中找到示例批处理事件应用程序 Spring Cloud 任务项目,请点击此处

将批处理事件发送到不同的通道

Spring Cloud Task 为批处理事件提供的选项之一是能够更改 特定侦听器可以向其发送消息的通道。为此,请使用 配置如下: .例如 如果需要将其消息发送到另一个称为而不是默认的通道,则可以添加 配置如下:spring.cloud.stream.bindings.<the channel>.destination=<new destination>StepExecutionListenermy-step-execution-eventsstep-execution-eventsSpring中文文档

spring.cloud.task.batch.events.step-execution-events-binding-name=my-step-execution-eventsSpring中文文档

禁用批处理事件

若要禁用所有批处理事件的侦听器功能,请使用以下命令 配置:Spring中文文档

spring.cloud.task.batch.events.enabled=falseSpring中文文档

若要禁用特定批处理事件,请使用以下配置:Spring中文文档

spring.cloud.task.batch.events.<batch event listener>.enabled=false:Spring中文文档

以下列表显示了可以禁用的单个侦听器:Spring中文文档

spring.cloud.task.batch.events.job-execution.enabled=false
spring.cloud.task.batch.events.step-execution.enabled=false
spring.cloud.task.batch.events.chunk.enabled=false
spring.cloud.task.batch.events.item-read.enabled=false
spring.cloud.task.batch.events.item-process.enabled=false
spring.cloud.task.batch.events.item-write.enabled=false
spring.cloud.task.batch.events.skip.enabled=false

为批处理事件发出顺序

默认情况下,批处理事件具有 .要更改此值 (for 例如,到 5 ),使用以下配置:Ordered.LOWEST_PRECEDENCESpring中文文档

spring.cloud.task.batch.events.job-execution-order=5
spring.cloud.task.batch.events.step-execution-order=5
spring.cloud.task.batch.events.chunk-order=5
spring.cloud.task.batch.events.item-read-order=5
spring.cloud.task.batch.events.item-process-order=5
spring.cloud.task.batch.events.item-write-order=5
spring.cloud.task.batch.events.skip-order=5
类路径上还需要绑定器实现。
可以在示例模块中找到示例批处理事件应用程序 Spring Cloud 任务项目,请点击此处