首页 > 解决方案 > 限制 Spring Cloud Data Flow 上的并发任务执行

问题描述

我已经部署了一个 Spring Batch 项目作为 Spring Cloud Data Flow (Using @EnableTask) 上的一个任务。

我曾经spring.cloud.task.singleInstanceEnabled=true避免在任务状态仍在 Spring Cloud Data Flow 上运行时重新启动任务。该属性放在 Spring Batch 项目的 application.properties 中。(按照这个链接——https: //docs.spring.io/spring-cloud-task/docs/2.0.0.RELEASE/reference/htmlsingle/#_restricting_spring_cloud_task_instances

但是,测试时的结果并不像我预期的那样:

第一次运行:作业A启动成功(约15分钟)

第二次运行:作业 A 在第一次运行期间再次启动,因此出现错误:

ERROR o.s.c.t.l.TaskLifecycleListener - org.springframework.cloud.task.listener.TaskExecutionException: Failed to process @BeforeTask or @AfterTask annotation because: Task with name "A" is already running.
ERROR o.s.c.t.l.TaskLifecycleListener - An event to end a task has been received for a task that has not yet started.
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
...
Caused by: org.springframework.cloud.task.listener.TaskExecutionException: Failed to process @BeforeTask or @AfterTask annotation because: Task with name "A" is already running.
...
Caused by: java.lang.reflect.InvocationTargetException: null
...
Caused by: org.springframework.cloud.task.listener.TaskExecutionException: Task with name "A" is already running.

当我第二次启动作业时,Spring Cloud Data Flow 仍然显示成功启动任务“A”,但日志出现我上面提到的错误。

最后,第一次运行完成时,Task 的状态为“ERROR”。(我认为状态应该是 COMPLETE 因为第一次工作)

如何正确限制 Spring Cloud 任务实例?

标签: springspring-batchspring-cloud-dataflowspring-cloud-task

解决方案


任务实例限制是 Spring Cloud Task(您的应用程序)的功能,而不是 Spring Cloud Data Flow。因此,您看到的是预期行为。您可以打开 Spring Cloud Data Flow 项目的问题,让我们讨论在 Spring Cloud Data Flow 级别添加该功能。


推荐阅读