Spring Cloud Data Flow启动taskLifecycleListener失败问题求助
错误日志
. ____ _ __ _ _ /\ / ___'_ __ _ _(_)_ __ __ _ \ \ \\ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \\ \ \/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.7.0) 2025-02-10 09:47:28.658 INFO 302 --- [ main] o.example.testscdf.TestscdfApplication : Starting TestscdfApplication v0.0.1-SNAPSHOT using Java 11.0.19 on e6179495f5fd with PID 302 (/home/cnb/scdf/my-scdf-task-0.0.1-SNAPSHOT.jar started by root in /tmp/3667929642927/test-a641c6f4-604f-44f8-9517-a0b243e0712d) 2025-02-10 09:47:28.675 INFO 302 --- [ main] o.example.testscdf.TestscdfApplication : No active profile set, falling back to 1 default profile: "default" 2025-02-10 09:47:33.435 ERROR 302 --- [ main] o.s.c.t.listener.TaskLifecycleListener : An event to end a task has been received for a task that has not yet started. 2025-02-10 09:47:33.440 WARN 302 --- [ main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Failed to start bean 'taskLifecycleListener'; nested exception is java.lang.IllegalArgumentException: Invalid TaskExecution, ID 3 not found 2025-02-10 09:47:33.468 ERROR 302 --- [ main] o.s.c.t.listener.TaskLifecycleListener : An event to end a task has been received for a task that has not yet started. 2025-02-10 09:47:33.478 INFO 302 --- [ main] ConditionEvaluationReportLoggingListener : Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2025-02-10 09:47:33.608 ERROR 302 --- [ main] o.s.boot.SpringApplication : Application run failed org.springframework.context.ApplicationContextException: Failed to start bean 'taskLifecycleListener'; nested exception is java.lang.IllegalArgumentException: Invalid TaskExecution, ID 3 not found at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:181) ~[spring-context-5.3.20.jar!/:5.3.20] at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54) ~[spring-context-5.3.20.jar!/:5.3.20] at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356) ~[spring-context-5.3.20.jar!/:5.3.20] at java.base/java.lang.Iterable.forEach(Unknown Source) ~[na:na] at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:155) ~[spring-context-5.3.20.jar!/:5.3.20] at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:123) ~[spring-context-5.3.20.jar!/:5.3.20] at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:935) ~[spring-context-5.3.20.jar!/:5.3.20] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586) ~[spring-context-5.3.20.jar!/:5.3.20] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) ~[spring-boot-2.7.0.jar!/:2.7.0] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) ~[spring-boot-2.7.0.jar!/:2.7.0] at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) ~[spring-boot-2.7.0.jar!/:2.7.0] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-2.7.0.jar!/:2.7.0] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[spring-boot-2.7.0.jar!/:2.7.0] at org.example.testscdf.TestscdfApplication.main(TestscdfApplication.java:12) ~[classes!/:0.0.1-SNAPSHOT] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:na] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:na] at java.base/java.lang.reflect.Method.invoke(Unknown Source) ~[na:na] at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) ~[my-scdf-task-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT] at org.springframework.boot.loader.Launcher.launch(Launcher.java:108) ~[my-scdf-task-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT] at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) ~[my-scdf-task-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT] at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:65) ~[my-scdf-task-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT] Caused by: java.lang.IllegalArgumentException: Invalid TaskExecution, ID 3 not found at org.springframework.util.Assert.notNull(Assert.java:201) ~[spring-core-5.3.20.jar!/:5.3.20] at org.springframework.cloud.task.listener.TaskLifecycleListener.doTaskStart(TaskLifecycleListener.java:276) ~[spring-cloud-task-core-2.4.3.jar!/:2.4.3] at org.springframework.cloud.task.listener.TaskLifecycleListener.start(TaskLifecycleListener.java:419) ~[spring-cloud-task-core-2.4.3.jar!/:2.4.3] at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:178) ~[spring-context-5.3.20.jar!/:5.3.20] ... 21 common frames omitted
项目POM配置
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.0</version> <relativePath/> </parent> <groupId>com.example</groupId> <artifactId>my-scdf-task</artifactId> <version>0.0.1-SNAPSHOT</version> <name>my-scdf-task</name> <description>SCDF Task Example</description> <properties> <java.version>11</java.version> <spring-cloud.version>2021.0.3</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-task</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
任务应用代码
package org.example.testscdf; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.task.configuration.EnableTask; @EnableTask @SpringBootApplication public class TestscdfApplication { public static void main(String[] args) { SpringApplication.run(TestscdfApplication.class, args); } public void run(String... args) { System.out.println("Task executed!"); } }
环境说明
Spring Cloud Data Flow通过Docker Compose运行,任务应用是基于Spring Boot的Spring Batch项目。
解决方案
统一数据库配置:
确保SCDF服务和任务应用连接同一个PostgreSQL实例和数据库。检查Docker Compose中SCDF的数据库配置,以及任务应用配置文件中的数据库URL、用户名、密码完全一致。任务启动时SCDF会在数据库中创建TASK_EXECUTION记录,任务应用需要读取这条记录管理生命周期,数据库不一致会导致找不到对应ID的任务执行记录。修复任务执行逻辑:
当前代码中的run方法未实现Spring的CommandLineRunner或ApplicationRunner接口,无法被Spring容器识别为任务执行逻辑,导致生命周期监听异常。修改代码如下:package org.example.testscdf; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.CommandLineRunner; import org.springframework.cloud.task.configuration.EnableTask; @EnableTask @SpringBootApplication public class TestscdfApplication implements CommandLineRunner { public static void main(String[] args) { SpringApplication.run(TestscdfApplication.class, args); } @Override public void run(String... args) { System.out.println("Task executed!"); } }初始化数据库表结构:
确认数据库用户拥有建表权限,Spring Cloud Task会自动创建TASK_EXECUTION、TASK_EXECUTION_PARAMS等任务管理表。如果表未自动创建,可在任务应用配置中添加spring.cloud.task.initialize.enable=true(仅测试环境使用,生产环境建议手动初始化表结构)。校验版本兼容性:
Spring Boot 2.7.0对应Spring Cloud 2021.0.3(Jubilee),匹配的Spring Cloud Data Flow版本应为2.10.x。检查Docker Compose中使用的SCDF镜像版本,版本不兼容会导致任务生命周期事件传递异常。
内容的提问来源于stack exchange,提问作者Black Jinsoul

