0

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); 
1
  • 1
    Schedule a task to shutdown the application after a the specified time. What's the issue you're having? Commented May 6, 2018 at 18:33

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.