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

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

本节将更详细地介绍 Spring Cloud Task 与 Spring 的集成 批。跟踪作业执行与其所在任务之间的关联 中介绍了通过Spring Cloud Deployer执行的远程分区 本节。Spring中文文档

将作业执行与执行该作业的任务相关联

Spring Boot 提供了在 Spring Boot Uber-jar 中执行批处理作业的工具。 Spring Boot 对此功能的支持允许开发人员执行多个批处理作业 在该执行中。Spring Cloud Task 提供了关联执行的功能 作业(作业执行)与任务的执行,以便可以追溯到 其他。Spring中文文档

Spring Cloud Task 通过使用 . 默认情况下, 此侦听器在同时具有 Spring Batch 作业的任何上下文中自动配置 配置(通过在上下文中定义类型的 Bean)和 CLASSPATH 上的 JAR。侦听器将注入到所有作业中 满足这些条件。TaskBatchExecutionListenerJobspring-cloud-task-batchSpring中文文档

重写 TaskBatchExecutionListener

防止侦听器被注入到当前 context,您可以使用标准 Spring Boot 机制禁用自动配置。Spring中文文档

要仅将侦听器注入到上下文中的特定作业中,请重写并提供作业 Bean ID 列表,如下所示 在以下示例中:batchTaskExecutionListenerBeanPostProcessorSpring中文文档

public static TaskBatchExecutionListenerBeanPostProcessor batchTaskExecutionListenerBeanPostProcessor() {
	TaskBatchExecutionListenerBeanPostProcessor postProcessor =
		new TaskBatchExecutionListenerBeanPostProcessor();

	postProcessor.setJobNames(Arrays.asList(new String[] {"job1", "job2"}));

	return postProcessor;
}
您可以在 Spring Cloud 的 samples 模块中找到示例批处理应用程序 任务项目,请点击此处
您可以在 Spring Cloud 的 samples 模块中找到示例批处理应用程序 任务项目,请点击此处

远程分区

Spring Cloud Deployer 提供了用于启动基于 Spring Boot 的应用程序的工具 大多数云基础架构。并将工作步骤执行的启动委托给 Spring Cloud Deployer。DeployerPartitionHandlerDeployerStepExecutionHandlerSpring中文文档

要配置 ,您必须提供 a 表示要执行的 Spring Boot Uber-jar、a 和 .您可以配置任何环境属性以及最大数量 要立即执行的工作线程,轮询结果的时间间隔(默认为 10 秒)和超时(默认为 -1 或无超时)。以下示例演示如何 配置此内容可能如下所示:DeployerStepExecutionHandlerResourceTaskLauncherHandlerJobExplorerPartitionHandlerSpring中文文档

@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。 设置最大工人数可确定最大工人数 应该一次运行的分区。Spring中文文档

要执行的应是一个 Spring Boot Uber-jar,在当前上下文中配置为 a。 前面示例中枚举的存储库应是 Spring Boot Uber-jar 位于其中。经理和工人都应该有可见性 添加到用作作业存储库和任务存储库的同一数据存储中。一旦 底层基础设施已经引导了 Spring Boot jar,而 Spring Boot 已经 启动了 ,步骤处理程序执行请求的 .以下示例演示如何配置:ResourceDeployerStepExecutionHandlerCommandLineRunnerDeployerStepExecutionHandlerStepDeployerStepExecutionHandlerSpring中文文档

@Bean
public DeployerStepExecutionHandler stepExecutionHandler(JobExplorer jobExplorer) {
	DeployerStepExecutionHandler handler =
		new DeployerStepExecutionHandler(this.context, jobExplorer, this.jobRepository);

	return handler;
}
您可以在 samples 模块中找到示例远程分区应用程序 Spring Cloud Task 项目,请点击此处

异步启动远程批处理分区

默认情况下,批处理分区按顺序启动。但是,在某些情况下,这可能会影响性能,因为每次启动都会阻塞,直到资源(例如:在 Kubernetes 中预配 Pod)被预配。 在这些情况下,您可以向 .这将根据 . 例如:ThreadPoolTaskExecutorDeployerPartitionHandlerThreadPoolTaskExecutorSpring中文文档

	@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);
	...
	}
我们需要关闭上下文,因为使用会使线程处于活动状态,因此应用程序不会终止。要适当地关闭应用程序,我们需要将 property 设置为 。ThreadPoolTaskExecutorspring.cloud.task.closecontextEnabledtrue

Kubernetes 平台批处理应用开发说明

  • 在 Kubernetes 平台上部署分区应用时,必须使用以下命令 Spring Cloud Kubernetes Deployer 的依赖关系:Spring中文文档

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-deployer-kubernetes</artifactId>
    </dependency>
  • 任务应用程序及其分区的应用程序名称需要遵循 以下正则表达式模式:. 否则,将引发异常。[a-z0-9]([-a-z0-9]*[a-z0-9])Spring中文文档

将环境变量传递给分区时,每个分区可以 在具有不同环境设置的不同计算机上。 因此,您应仅传递所需的环境变量。
您可以在 samples 模块中找到示例远程分区应用程序 Spring Cloud Task 项目,请点击此处
我们需要关闭上下文,因为使用会使线程处于活动状态,因此应用程序不会终止。要适当地关闭应用程序,我们需要将 property 设置为 。ThreadPoolTaskExecutorspring.cloud.task.closecontextEnabledtrue

批处理信息性消息

Spring Cloud Task 为批处理作业提供了发出信息性消息的功能。这 “Spring Batch Events”一节详细介绍了此功能。Spring中文文档

批处理作业退出代码

如前所述,Spring Cloud 任务 应用程序支持记录任务执行的退出代码的功能。但是,在 在任务中运行 Spring Batch 作业的情况,无论批处理作业如何 执行完成,使用默认时任务结果始终为零 批处理/启动行为。请记住,任务是启动应用程序,退出代码 从任务返回的与引导应用程序相同。 覆盖此行为并允许任务在以下情况下返回除零以外的退出代码 批处理作业返回 的 BatchStatus,设置为 。然后是退出代码 可以是 1(默认值)或基于指定的 ExitCodeGeneratorFAILEDspring.cloud.task.batch.fail-on-job-failuretrue)Spring中文文档

此功能使用的新功能取代了 Spring 提供的功能 靴子。默认情况下,它配置了相同的顺序。但是,如果要自定义 运行的顺序,可以通过设置属性来设置其顺序。要让任务返回 退出代码基于批处理作业执行的结果,需要自己编写。ApplicationRunnerApplicationRunnerspring.cloud.task.batch.applicationRunnerOrderCommandLineRunnerSpring中文文档