How to prevent Spring Boot daemon/server application from closing/shutting down immediately?

前端 未结 9 742
不知归路
不知归路 2020-12-03 01:10

My Spring Boot application is not a web server, but it\'s a server using custom protocol (using Camel in this case).

But Spring Boot immediately stops (gracefully) a

9条回答
  •  星月不相逢
    2020-12-03 01:55

    I found the solution, using org.springframework.boot.CommandLineRunner + Thread.currentThread().join(), e.g.: (note: code below is in Groovy, not 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()
        }
    }
    

提交回复
热议问题