|
该版本仍在开发中,尚未被视为稳定。对于最新稳定版本,请使用 spring-cloud-task 5.0.0! |
Batch
本节将更详细介绍 Spring Cloud Task 与 Spring 的集成 批。跟踪作业执行与其所属任务之间的关联 通过 Spring Cloud 部署器执行及远程分区都涵盖在 这一部分。
将作业执行与执行任务关联
Spring Boot 提供了在 Spring Boot Uber jar 内执行批处理作业的设施。 Spring Boot 对此功能的支持允许开发者执行多个批处理作业 在那个执行之中。Spring Cloud Task提供了关联执行的功能 一个作业(作业执行)与任务的执行,因此可以追溯到一个任务的执行 其他。
Spring Cloud Task通过使用以下方式实现此功能TaskBatchExecutionListener.
默认情况下,
该监听器在任何同时拥有 Spring Batch 作业的上下文中自动配置
配置(通过拥有 类型的豆子工作在上下文中定义)以及Spring-cloud-task-batchJar on the classpath。监听者被注入所有工作
符合这些条件。
覆盖 TaskBatchExecutionListener
以防止监听器被注入当前中的任何批处理作业 上下文中,你可以使用标准的 Spring Boot 机制禁用自动配置。
如果只让监听者在上下文中被注入特定作业,可以覆盖batchTaskExecutionListenerBeanPostProcessor并提供工作豆ID列表,如图所示
以下示例:
public static TaskBatchExecutionListenerBeanPostProcessor batchTaskExecutionListenerBeanPostProcessor() {
TaskBatchExecutionListenerBeanPostProcessor postProcessor =
new TaskBatchExecutionListenerBeanPostProcessor();
postProcessor.setJobNames(Arrays.asList(new String[] {"job1", "job2"}));
return postProcessor;
}
| 你可以在Spring Cloud的样本模块中找到一个批处理示例应用 任务项目,在这里。 |
远程分区
Spring Cloud Deployer 提供了在
大多数云基础设施。这DeployerPartitionHandler和部署器StepExecutionHandler将工作阶级执行的启动委托给 Spring
云部署器。
要配置部署器StepExecutionHandler你必须提供一个资源代表即将执行的Spring靴超级罐,一个任务Starters处理程序,以及一个JobExplorer.你可以配置任何环境属性以及最大数量的
工人将同时执行,轮询结果的间隔(默认为10
秒数),以及一个超时(默认为-1或无超时)。以下示例展示了如何
配置此项分区处理可能的做法是:
@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人。 设定工人的最大数量即确定最大 分区本应同时运行。
这资源预计将执行的是一个带有部署器StepExecutionHandler配置为命令线跑者在当前的语境下。
前例中枚举的仓库应为 的远程仓库
而春季靴超级罐就位于这里。经理和员工都应具备可见性
并进入同一个作为作业仓库和任务仓库使用的数据存储。一旦
底层基础设施启动了 Spring Boot jar,而 Spring Boot 也
启动了部署器StepExecutionHandler,步处理程序执行请求的步.以下示例展示了如何配置部署器StepExecutionHandler:
@Bean
public DeployerStepExecutionHandler stepExecutionHandler(JobExplorer jobExplorer) {
DeployerStepExecutionHandler handler =
new DeployerStepExecutionHandler(this.context, jobExplorer, this.jobRepository);
return handler;
}
| 你可以在 春云任务项目,点击这里。 |
异步启动远程批处理分区
默认情况下,批处理分区是顺序启动的。然而,在某些情况下,这可能会影响性能,因为每次启动都会阻塞,直到资源(例如:在 Kubernetes 中配置一个 pod)被配置完成。
在这种情况下,你可以提供线程池任务执行者前往DeployerPartitionHandler.这将根据配置启动远程批处理分区线程池任务执行者.
例如:
@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);
...
}
我们需要闭合上下文,因为使用线程池任务执行者保持一个线程活跃,因此应用不会终止。为了适当关闭应用,我们需要设置spring.cloud.task.closecontextEnabled属性到true. |
批处理信息消息
Spring Cloud Task 支持批处理作业发送信息消息。这 “Spring Session活动”部分详细介绍了这一功能。
批处理作业退出代码
如前所述,春云任务
应用程序支持记录任务执行的退出代码。然而,
在任务中运行春季批处理作业的情况,无论该批处理作业如何
执行完成后,使用默认时任务结果总是为零
批次/启动行为。请记住,任务是一个启动应用程序,退出代码
从任务返回的过程与启动应用程序相同。
要覆盖此行为,允许任务在
batch job 返回 BatchStatus 为失败设置Spring.cloud.task.batch.fail-on-job-failure自true.然后是出口代码
可以是1(默认值),也可以基于指定出口代码生成器)
该功能使用了新的应用运行者取代了Spring提供的
靴子。默认情况下,它配置的顺序是相同的。不过,如果你想自定义
顺序是应用运行者运行时,你可以通过设置 来设置它的顺序spring.cloud.task.batch.applicationRunnerOrder财产。要让你的任务返回
基于批处理作业结果的退出代码,你需要自己写命令线跑者.