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

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

    for springboot app to run continously it has to be run in a container, otherwise it is just like any java app all threads are done it finishes, you can add

    <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    

    and it will turn it into webapp, if not you are responsible keeping it alive in your implementation

    0 讨论(0)
  • 2020-12-03 01:47

    To keep the java process alive when not deploying a web application set the webEnvironment property to false like so:

     SpringApplication sa = new SpringApplication();
     sa.setWebEnvironment(false); //important
     ApplicationContext ctx = sa.run(ApplicationMain.class, args);
    
    0 讨论(0)
  • 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()
        }
    }
    
    0 讨论(0)
  • 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 <PID>
    
    0 讨论(0)
  • 2020-12-03 02:03

    This is now made even simpler.

    Just add camel.springboot.main-run-controller=true to your application.properties

    0 讨论(0)
  • 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();
            };
        }
    
    0 讨论(0)
提交回复
热议问题