Spring Cloud Stream 集成
任务本身可能很有用,但将任务集成到更大的生态系统中可以让它 对于更复杂的处理和编排很有用。本节 涵盖了 Spring Cloud Task 与 Spring Cloud Stream 的集成选项。
17. 从 Spring Cloud Stream 启动任务
您可以从流启动任务。为此,请创建一个侦听消息的接收器
其中包含一个TaskLaunchRequest
作为其有效载荷。这TaskLaunchRequest
包含:
-
uri
:到要执行的任务工件。 -
applicationName
:与任务关联的名称。如果没有 applicationName 设置时,则TaskLaunchRequest
生成任务名称 由以下内容组成:Task-<UUID>
. -
commandLineArguments
:包含任务的命令行参数的列表。 -
environmentProperties
:包含要由 任务。 -
deploymentProperties
:包含部署程序用于 部署任务。
如果有效负载属于不同类型,接收器会引发异常。 |
例如,可以创建一个流,该流具有从以下
HTTP 源代码并创建GenericMessage
其中包含TaskLaunchRequest
并发送
消息发送到其输出通道。然后,任务接收器将从其
输入 channnel,然后启动任务。
要创建 taskSink,您只需创建一个包含EnableTaskLauncher
注释,如以下示例所示:
@SpringBootApplication
@EnableTaskLauncher
public class TaskSinkApplication {
public static void main(String[] args) {
SpringApplication.run(TaskSinkApplication.class, args);
}
}
示例
Spring Cloud Task 项目的模块包含一个示例 Sink 和 Processor。安装
这些示例到本地 Maven 存储库中,从spring-cloud-task-samples
目录,并带有skipInstall
属性设置为false
如
如以下示例所示:
mvn clean install
这maven.remoteRepositories.springRepo.url 属性必须设置为
über-jar 所在的远程存储库。如果未设置,则没有遥控器
存储库,因此它仅依赖于本地存储库。 |
17.1. Spring Cloud 数据流
要在 Spring Cloud Data Flow 中创建流,您必须首先注册 Task Sink 我们创建的应用程序。在以下示例中,我们将注册处理器和 使用 Spring Cloud Data Flow shell 接收示例应用程序:
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 创建流:
stream create foo --definition "http --server.port=9000|taskProcessor|taskSink" --deploy
18. Spring Cloud 任务事件
Spring Cloud Task 提供了通过 Spring Cloud Stream 发出事件的能力
channel 当任务通过 Spring Cloud Stream 通道运行时。任务侦听器是
用于发布TaskExecution
在名为task-events
.此功能
自动连接到任何具有spring-cloud-stream
,spring-cloud-stream-<binder>
,
以及其类路径上定义的任务。
要禁用事件发出侦听器,请将spring.cloud.task.events.enabled 属性设置为false . |
定义适当的类路径后,以下任务将发出TaskExecution
作为
事件task-events
通道(在任务的开始和结束时):
@SpringBootApplication
public class TaskEventsApplication {
public static void main(String[] args) {
SpringApplication.run(TaskEventsApplication.class, args);
}
@Configuration
public static class TaskConfiguration {
@Bean
public CommandLineRunner commandLineRunner() {
return new CommandLineRunner() {
@Override
public void run(String... args) throws Exception {
System.out.println("The CommandLineRunner was executed");
}
};
}
}
}
还需要在类路径上有一个绑定器实现。 |
可以在示例模块中找到示例任务事件应用程序 Spring Cloud 任务项目,这里。 |
18.1. 禁用特定任务事件
要禁用任务事件,您可以将spring.cloud.task.events.enabled
属性设置为false
.
19. Spring Batch 事件
通过任务执行 Spring Batch 作业时,可以将 Spring Cloud Task 配置为 根据 Spring Batch 中可用的 Spring Batch 侦听器发出信息性消息。 具体来说,以下 Spring Batch 侦听器会自动配置到每个批处理作业中 并在通过 Spring 运行时在关联的 Spring Cloud Stream 通道上发出消息 云任务:
-
JobExecutionListener
监听job-execution-events
-
StepExecutionListener
监听step-execution-events
-
ChunkListener
监听chunk-events
-
ItemReadListener
监听item-read-events
-
ItemProcessListener
监听item-process-events
-
ItemWriteListener
监听item-write-events
-
SkipListener
监听skip-events
这些侦听器自动配置为任何AbstractJob
当适当的
Beans(一个Job
和TaskLifecycleListener
) 存在于上下文中。配置设置为
监听这些事件的处理方式与绑定到任何其他 Spring 的方式相同
Cloud Stream 通道完成。我们的任务(运行批处理作业的任务)充当Source
,监听应用程序充当Processor
或Sink
.
一个例子是让应用程序监听job-execution-events
渠道
用于作业的开始和停止。要配置监听应用程序,您可以
将输入配置为job-execution-events
如下:
spring.cloud.stream.bindings.input.destination=job-execution-events
还需要在类路径上有一个绑定器实现。 |
可以在示例模块中找到示例批处理事件应用程序 Spring Cloud 任务项目,这里。 |
19.1. 将批处理事件发送到不同的通道
Spring Cloud Task 为批处理事件提供的选项之一是能够更改
特定侦听器可以向其发出消息的通道。为此,请使用
以下配置:spring.cloud.stream.bindings.<the channel>.destination=<new destination>
.例如
如果StepExecutionListener
需要将其消息发出到另一个名为my-step-execution-events
而不是默认的step-execution-events
,您可以添加
以下配置:
spring.cloud.stream.bindings.step-execution-events.destination=my-step-execution-events
19.2. 禁用批处理事件
要禁用所有批处理事件的侦听器功能,请使用以下命令 配置:
spring.cloud.task.batch.events.enabled=false
要禁用特定的批处理事件,请使用以下配置:
spring.cloud.task.batch.events.<batch event listener>.enabled=false
:
以下列表显示了可以禁用的单个侦听器:
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
19.3. 批量事件的发出顺序
默认情况下,批处理事件具有Ordered.LOWEST_PRECEDENCE
.要更改此值(对于
例如,设置为 5 ),使用以下配置:
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