如何防止 Spring Boot 守护进程/服务器应用程序立即关闭/关闭?

新手上路,请多包涵

我的 Spring Boot 应用程序不是 Web 服务器,而是使用自定义协议的服务器(在本例中使用 Camel)。

但是 Spring Boot 在启动后立即(优雅地)停止。我该如何防止这种情况?

我希望应用程序在 Ctrl+C 或以编程方式停止时停止。

 @CompileStatic
@Configuration
class CamelConfig {

    @Bean
    CamelContextFactoryBean camelContext() {
        final camelContextFactory = new CamelContextFactoryBean()
        camelContextFactory.id = 'camelContext'
        camelContextFactory
    }

}

原文由 Hendy Irawan 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 774
2 个回答

从 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 导入依赖管理信息以保持一致性。

原文由 jmkgreen 发布,翻译遵循 CC BY-SA 3.0 许可协议

我找到了解决方案,使用 org.springframework.boot.CommandLineRunner + Thread.currentThread().join() ,例如:(注意:下面的代码是 Groovy,不是 Java)

 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()
    }
}

原文由 Hendy Irawan 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题