我的 Spring Boot 应用程序不是 Web 服务器,而是使用自定义协议的服务器(在本例中使用 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()
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)