This is not related to Spring Batch. Spring Batch does not prevent your JVM from shutting down. As soon as your job is finished, your JVM should terminate, otherwise there is something else preventing it from shutting down. In your case, it is your executor service that you've configured as non-daemon thread.
I took your example and removed everything related to Spring Batch, and the same thing happens:
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.annotation.PostConstruct;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
@SpringBootApplication
public class DemoBatchApplication {
public static void main(String[] args) {
new SpringApplicationBuilder().sources(com.example.demo.batch.DemoBatchApplication.class)
.web(WebApplicationType.NONE)
.run(args);
}
private final ScheduledExecutorService scheduledExecutorService = Executors
.newSingleThreadScheduledExecutor(new BasicThreadFactory.Builder()
.namingPattern("task-non-daemon-%d")
.daemon(false)
.build());
@PostConstruct
public void init() {
this.scheduledExecutorService.scheduleAtFixedRate(() -> {
System.out.println("Scheduled task non-daemon!!!!");
}, 1L, 1000L, TimeUnit.MILLISECONDS);
}
}
If you run this app, you should see the same behaviour: the scheduledExecutorService
will keep running since you set it as a non-daemon thread. If you change the daemon flag to true, the JVM will stop as soon as your job is finished. Please check What is a daemon thread in Java?.