spring-boot: Application does not terminate upon completion of CommandLineRunner anymore

I run some java batch processes using this code structure:

@SpringBootApplication
public class Application {
    
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public CommandLineRunner commandLineRunner(MyServiceInstance service) {
        return args -> {
            service.doJob();
        };
    }

}

Prior spring 2.4.2 my service/process terminates after doing it’s Job, so the code was convenient to run batch jobs. Starting from spring 2.4.2 the service instances do not terminate. So image the result all my instances are running in my AWS Batch account causing cost increases and blocking other jobs.

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 16 (12 by maintainers)

Most upvoted comments

We’re in a bit of a catch 22. The shutdown() method does get called, but only when the ApplicationContext is closed. That won’t happen if non-daemon threads are still active.

@eduardobarrena-tc you can change your main method to this to force the context to close:

public static void main(String[] args) {
	SpringApplication.run(DemoApplication.class, args).close();
}

Thanks for the sample project. It looks like this might be a Lettuce issue. Adding <lettuce.version>6.0.1.RELEASE</lettuce.version> to the POM also fixes the issue.

I’m going to dig a bit more to see if I can find out what’s changed.

@eduardobarrena-tc You can already use Spring Boot without auto-configuration, if you look at @SpringBootApplication you’ll see it’s actually:

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
		@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {

If you don’t want auto-configuration you can annotate your main class with:

@SpringBootConfiguration
@ComponentScan

You can also exclude the spring-boot-autoconfigure jar if you want to be totally sure that no auto-configuration functionality is used.