Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to shut down a Spring Boot command-line application

I am building a Command Line java application using Spring Boot to get it working quickly.

The application loads different types of files (for example CSV) and loads them into a Cassandra Database. It does NOT use any web components, it is not a web application.

The problem I am having is to stop the application when the work is done. I am using the Spring CommandLineRunner interface with a @Component to run the tasks, as shown below, but when the work is completed the application does not stop, it keeps running for some reason and I can't find a way to stop it.

@Component public class OneTimeRunner implements CommandLineRunner {      @Autowired     private CassandraOperations cassandra;      @Autowired     private ConfigurableApplicationContext context;      @Override     public void run(String... args) throws Exception {         // do some work here and then quit         context.close();     } } 

UPDATE: the problem seems to be spring-cassandra, since there is nothing else in the project. Does anyone know why it keeps threads running in the background that prevent the application from stopping?

UPDATE: the problem disappeared by updating to the latest spring boot version.

like image 778
ESala Avatar asked Oct 12 '14 19:10

ESala


People also ask

How do I stop a spring boot application in terminal?

Use the static exit() method in the SpringApplication class for closing your spring boot application gracefully.


2 Answers

I found a solution. You can use this:

public static void main(String[] args) {     SpringApplication.run(RsscollectorApplication.class, args).close();     System.out.println("done"); } 

Just use .close() on run.

like image 112
ACV Avatar answered Sep 23 '22 06:09

ACV


The answer depends on what it is that is still doing work. You can probably find out with a thread dump (eg using jstack). But if it is anything that was started by Spring you should be able to use ConfigurableApplicationContext.close() to stop the app in your main() method (or in the CommandLineRunner).

like image 28
Dave Syer Avatar answered Sep 22 '22 06:09

Dave Syer