【发布时间】:2019-11-12 10:32:04
【问题描述】:
我已经在 Spring Cloud Data Flow 上部署了一个 Spring Batch 项目作为任务(使用 @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 数据流仍然显示成功启动任务“A”但日志出现我上面提到的错误。
最后,第一次运行完成时,Task 的状态为“ERROR”。 (我认为状态应该是 COMPLETE 因为第一次工作)
如何正确限制 Spring Cloud 任务实例?
【问题讨论】:
标签: spring spring-batch spring-cloud-dataflow spring-cloud-task