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

前端 未结 9 732
不知归路
不知归路 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 02:03

    My project is NON WEB Spirng Boot. My elegant solution is create a daemon thread by CommandLineRunner. Then, Application do not shutdown immediately.

     @Bean
        public CommandLineRunner deQueue() {
            return args -> {
                Thread daemonThread;
                consumer.connect(3);
                daemonThread = new Thread(() -> {
                    try {
                        consumer.work();
                    } catch (InterruptedException e) {
                        logger.info("daemon thread is interrupted", e);
                    }
                });
                daemonThread.setDaemon(true);
                daemonThread.start();
            };
        }
    

提交回复
热议问题