如何让Apache Camel Spring Boot的Supervising Route Controller持续运行?
我尝试启用Camel的Supervising Route Controller,实现路由启动失败时自动重试,按照官方文档的配置方式在application.properties中添加了以下配置:
camel.springboot.route-controller-supervise-enabled=true camel.springboot.route-controller-initial-delay=5000 camel.springboot.route-controller-back-off-delay=5000 camel.springboot.route-controller-back-off-max-attempts=10 camel.springboot.main-run-controller=true
但启动应用后,所有路由随即启动又立即停止,仿佛camel.springboot.main-run-controller=true配置未生效,相关日志如下:
... 2023-07-13T08:54:49.470+02:00 INFO 25984 --- [ restartedMain] c.b.c.l.LdapIntegrationApplication : Started LdapIntegrationApplication in 13.596 seconds (process running for 17.256) 2023-07-13T08:54:49.507+02:00 WARN 25984 --- [ionShutdownHook] o.a.c.s.boot.SpringBootCamelContext : CamelContext has only been running for less than a second. If you intend to run Camel for a longer time then you can set the property camel.springboot.main-run-controller=true in application.properties or add spring-boot-starter-web JAR to the classpath. 2023-07-13T08:54:49.509+02:00 INFO 25984 --- [ionShutdownHook] o.a.c.impl.engine.AbstractCamelContext : Apache Camel 4.0.0-M3 (LdapCamel) is shutting down (timeout:45s) 2023-07-13T08:54:49.524+02:00 INFO 25984 --- [ionShutdownHook] o.a.c.impl.engine.AbstractCamelContext : Routes stopped (stopped:4) ...
请问如何让Supervising Route Controller保持运行状态?
解决方案
确认配置生效优先级
检查camel.springboot.main-run-controller=true是否被其他配置(如环境变量、配置中心、命令行参数)覆盖。可以通过Spring Boot的--debug启动参数查看配置加载详情,确认该配置已被正确应用。排查路由启动失败原因
应用退出可能是因为所有路由启动失败,导致CamelContext没有活跃路由触发自动关闭。开启Camel的debug级日志,查看路由启动时的具体异常信息,修复路由的启动问题(比如依赖的外部服务不可用、配置错误等),确保至少有部分路由能正常运行,或者让Supervising Route Controller有重试的目标。升级Camel版本
你使用的是Camel 4.0.0-M3预览版本,可能存在配置逻辑的bug。建议切换到Camel 3.x稳定版或4.x正式发布版,预览版本的功能可能未完全成熟,容易出现兼容性问题。补充依赖或调整应用类型
如果是纯后台非Web应用,除了配置main-run-controller,还可以添加spring-boot-starter-web依赖让应用保持活跃;若不需要Web服务,也可以自定义实现CommandLineRunner或ApplicationRunner接口,在run方法中用CountDownLatch等方式阻塞主线程,防止应用退出。检查主类逻辑
确认Spring Boot主类中没有手动调用CamelContext.stop()或其他主动终止应用的代码,确保主类仅通过@SpringBootApplication注解启动,没有额外的退出逻辑。
内容的提问来源于stack exchange,提问作者Jirmed

