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

Hendy Irawan picture Hendy Irawan · Jan 19, 2015 · Viewed 30.2k times · Source

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) after started. How do I prevent this?

I'd like the app to stop if Ctrl+C or programmatically.

@CompileStatic
@Configuration
class CamelConfig {

    @Bean
    CamelContextFactoryBean camelContext() {
        final camelContextFactory = new CamelContextFactoryBean()
        camelContextFactory.id = 'camelContext'
        camelContextFactory
    }

}

Answer

Hendy Irawan picture Hendy Irawan · Jan 19, 2015

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()
    }
}