How to prevent Spring Boot daemon/server application from closing/shutting down immediately?
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.socialimport org.slf4j.LoggerFactoryimport org.springframework.boot.CommandLineRunnerimport org.springframework.boot.SpringApplicationimport org.springframework.boot.autoconfigure.SpringBootApplication@SpringBootApplicationclass 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() }}
As of Apache Camel 2.17 there is a cleaner answer. To quote http://camel.apache.org/spring-boot.html:
To keep the main thread blocked so that Camel stays up, either include the spring-boot-starter-web dependency, or add camel.springboot.main-run-controller=true to your application.properties or application.yml file.
You will want the following dependency too:
<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-spring-boot-starter</artifactId> <version>2.17.0</version></dependency>
Clearly replace <version>2.17.0</version>
or use the camel BOM to import dependency-management information for consistency.
An example implementation using a CountDownLatch:
@Beanpublic 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>