我的Spring Boot应用程序不是Web服务器,而是使用自定义协议的服务器(在这种情况下使用Camel)。
但是Spring Boot在启动后立即(正常)停止。我该如何预防?
我希望该应用程序以Ctrl + C或编程方式停止。
@CompileStatic @Configuration class CamelConfig { @Bean CamelContextFactoryBean camelContext() { final camelContextFactory = new CamelContextFactoryBean() camelContextFactory.id = 'camelContext' camelContextFactory } }
从Apache Camel 2.17开始,有一个更干净的答案。引用http://camel.apache.org/spring-boot.html:
要保持主线程处于阻塞状态,以使Camel保持正常运行,请添加spring-boot-starter-web依赖项,或者将camel.springboot.main-run-controller = true添加到application.properties或application.yml文件中。
你还将需要以下依赖项:
<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-spring-boot-starter</artifactId> <version>2.17.0</version> </dependency>
清楚地替换<version>2.17.0</version>或使用骆驼BOM导入依赖关系管理信息以保持一致性。
<version>2.17.0</version>
我找到了使用org.springframework.boot.CommandLineRunner+ 的解决方案,Thread.currentThread().join()例如:(注意:下面的代码在Groovy中,而不是Java中)
org.springframework.boot.CommandLineRunner
Thread.currentThread().join()
package id.ac.itb.lumen.social import org.slf4j.LoggerFactory import org.springframework.boot.CommandLineRunner import org.springframework.boot.SpringApplication import org.springframework.boot.autoconfigure.SpringBootApplication @SpringBootApplication class LumenSocialApplication implements CommandLineRunner { private static final log = LoggerFactory.getLogger(LumenSocialApplication.class) static void main(String[] args) { SpringApplication.run LumenSocialApplication, args } @Override void run(String... args) throws Exception { log.info('Joining thread, you can press Ctrl+C to shutdown application') Thread.currentThread().join() } }