gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 09:34:36 24 4
gpt4 key购买 nike

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

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

我希望应用程序能够通过 Ctrl+C 或以编程方式停止。

@CompileStatic
@Configuration
class CamelConfig {

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

}

最佳答案

我找到了解决方案,使用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()
}
}

关于spring - 如何防止 Spring Boot 守护进程/服务器应用程序立即关闭/关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56659996/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com