Batch
本节更详细地介绍了 Spring Cloud Task 与 Spring 的集成 批。跟踪作业执行与作业所在任务之间的关联 执行以及通过 Spring Cloud Deployer 进行远程分区 本节。
将作业执行与执行作业的任务相关联
Spring Boot 提供了在 Spring Boot Uber-jar 中执行批处理作业的工具。 Spring Boot 对此功能的支持允许开发人员执行多个批处理作业 在那个执行中。Spring Cloud Task 提供了关联执行的能力 作业(作业执行)与任务的执行情况,以便可以追溯到 其他。
Spring Cloud Task 通过使用TaskBatchExecutionListener
.
默认情况下,
此侦听器在同时具有 Spring Batch Job 的任何上下文中自动配置
配置(通过具有类型为Job
定义在上下文中)和spring-cloud-task-batch
jar 在类路径上。侦听器被注入到所有作业中
满足这些条件。
重写 TaskBatchExecutionListener
防止侦听器被注入到当前 context,您可以使用标准 Spring Boot 机制禁用自动配置。
要仅将侦听器注入上下文中的特定作业,请覆盖batchTaskExecutionListenerBeanPostProcessor
并提供作业 Bean ID 列表,如图所示
在以下示例中:
public static TaskBatchExecutionListenerBeanPostProcessor batchTaskExecutionListenerBeanPostProcessor() {
TaskBatchExecutionListenerBeanPostProcessor postProcessor =
new TaskBatchExecutionListenerBeanPostProcessor();
postProcessor.setJobNames(Arrays.asList(new String[] {"job1", "job2"}));
return postProcessor;
}
您可以在 Spring Cloud 的 samples 模块中找到示例批处理应用程序 任务项目,这里。 |
远程分区
Spring Cloud Deployer 提供了用于在
大多数云基础设施。这DeployerPartitionHandler
和DeployerStepExecutionHandler
将工作器步骤执行的启动委托给 Spring
云部署器。
要配置DeployerStepExecutionHandler
,您必须提供Resource
表示要执行的 Spring Boot Uber-jar,一个TaskLauncherHandler
和一个JobExplorer
.您可以配置任何环境属性以及最大数量的
workers 要同时执行,轮询结果的间隔(默认为 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<String> 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 Uber-jar,具有DeployerStepExecutionHandler
配置为CommandLineRunner
在当前背景下。
前面示例中枚举的存储库应该是
Spring Boot Uber-jar 位于其中。经理和工作人员都应具有可见性
到用作作业存储库和任务存储库的同一数据存储中。一旦
底层基础设施已经引导了 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 Task 项目,这里。 |
异步启动远程批处理分区
默认情况下,批处理分区按顺序启动。但是,在某些情况下,这可能会影响性能,因为每次启动都会阻塞,直到资源(例如:在 Kubernetes 中配置 Pod)为止。
在这些情况下,您可以提供ThreadPoolTaskExecutor
到DeployerPartitionHandler
.这将根据ThreadPoolTaskExecutor
.
例如:
@Bean
public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(4);
executor.setThreadNamePrefix("default_task_executor_thread");
executor.setWaitForTasksToCompleteOnShutdown(true);
executor.initialize();
return executor;
}
@Bean
public PartitionHandler partitionHandler(TaskLauncher taskLauncher, JobExplorer jobExplorer,
TaskRepository taskRepository, ThreadPoolTaskExecutor executor) throws Exception {
Resource resource = this.resourceLoader
.getResource("maven://io.spring.cloud:partitioned-batch-job:2.2.0.BUILD-SNAPSHOT");
DeployerPartitionHandler partitionHandler =
new DeployerPartitionHandler(taskLauncher, jobExplorer, resource,
"workerStep", taskRepository, executor);
...
}
我们需要关闭上下文,因为使用ThreadPoolTaskExecutor 使线程保持活动状态,因此应用程序不会终止。要适当地关闭应用程序,我们需要将spring.cloud.task.closecontextEnabled 属性设置为true . |
批处理信息性消息
Spring Cloud Task 为批处理作业提供了发出信息性消息的能力。这 “Spring Batch Events”部分详细介绍了此功能。
批处理作业退出代码
如前所述,Spring Cloud 任务
应用程序支持记录任务执行的退出代码的功能。但是,在
在任务中运行 Spring Batch Job 的情况,无论 Batch Job 如何
执行完成,使用默认值时任务结果始终为零
批处理/启动行为。请记住,任务是启动应用程序,并且退出代码
从任务返回的应用程序与启动应用程序相同。
重写此行为并允许任务在
批处理作业返回 BatchStatusFAILED
设置spring.cloud.task.batch.fail-on-job-failure
自true
.然后是退出代码
可以是 1(默认值)或基于指定ExitCodeGenerator
)
此功能使用新的ApplicationRunner
取代 Spring 提供的那个
靴子。默认情况下,它配置为相同的顺序。但是,如果您想自定义
的顺序ApplicationRunner
运行时,您可以通过设置spring.cloud.task.batch.applicationRunnerOrder
财产。要让您的任务返回
根据批处理作业执行的结果退出代码,需要编写自己的CommandLineRunner
.