javaspringspring-bootspring-data-cassandra

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.


Solution

  • 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).