How can I terminate a running Spring Boot application after a certain time?
In other words, I will run my spring boot application today and I want to terminate two days from now:
new SpringApplicationBuilder().sources(Main.class).run(args); How can I terminate a running Spring Boot application after a certain time?
In other words, I will run my spring boot application today and I want to terminate two days from now:
new SpringApplicationBuilder().sources(Main.class).run(args); One of the ways to gracefully shutdown spring boot application described in documentation: https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-application-exit
so basically to gracefully shutdown spring boot application you should just call
System.exit(SpringApplication .exit(SpringApplication.run(ExitCodeApplication.class, args))); In order to schedule this, you can use spring boot scherduled task: https://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/scheduling.html
All in all you will have some service which will have this method:
@Scheduled(fixedRate = 2 * MILLIS_IN_DAY, initialDelay = 2 * MILLIS_IN_DAY) public void shutdownApp() { System.exit(SpringApplication .exit(SpringApplication.run(ExitCodeApplication.class, args))); } And you enable this scheduled method you need to annotate your application class with @EnableScheduling