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

标签 spring spring-boot groovy server daemon

我的 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/

相关文章:

java - 在名称为 'HelloWeb' Spring MVC 的 DispatcherServlet 中找不到具有 URI [/HelloWeb/] 的 HTTP 请求的映射

java - 获取 MockMvc perform() 方法的错误

git - 如何 checkin 文件以使用 git 构建?

grails - 为什么不能在Grails中导入此Groovy库?

java - PLAY框架路由多参数问题

java - URL中的编码逗号在Spring中被读取为List

Spring Security - 我可以同时使用命名空间和过滤器链吗?

java - 在 Java 11 中使用 Spring boot 2.0 时创建名称为 'entityManagerFactory' 的 bean 时出错

java - 使用Spring的SockJsClient编写一个java websocket客户端

java - Spring @PropertySource 值不覆盖