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

前端 未结 9 719
不知归路
不知归路 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:01

    An example implementation using a CountDownLatch:

    @Bean
    public CountDownLatch closeLatch() {
        return new CountDownLatch(1);
    }
    
    public static void main(String... args) throws InterruptedException {
        ApplicationContext ctx = SpringApplication.run(MyApp.class, args);  
    
        final CountDownLatch closeLatch = ctx.getBean(CountDownLatch.class);
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                closeLatch.countDown();
            }
        });
        closeLatch.await();
    }
    

    Now to stop your application, you can look up the process ID and issue a kill command from the console:

    kill 
    

提交回复
热议问题