I am creating a very generic Spring Batch Application where I read a lot of configurations from YAML files and I create jobs dynamically and register the Jobs like below,
jobRegistry.register(new ReferenceJobFactory(job));
All this is done in Configuration Class. After which I launch all the jobs like below,
jobs.forEach(j -> {
Job job;
try {
job = jobRegistry.getJob(j);
JobExecution jobExecution = jobLauncher.run(job, jobParameters);
jobExecutions.add(jobExecution);
} catch (Exception e) {
e.printStackTrace();
}
});
All is fine till here, all jobs gets executed and completed successfully and JobRepository is also updated with 'COMPLETED' status. After this, the Spring Batch application continues to run and does not exit. I don't know how to exit the spring batch application once all the jobs are completed.
I followed from some other thread to do below,
ConfigurableApplicationContext run = SpringApplication.run(ChpCgmsReplicationApplication.class, args);
System.exit(SpringApplication.exit(run));
This works but the problem is it doesn't wait for all the jobs, The very first job when it completes, the application exits. How do I make this wait till all the jobs are completed and then exit.
ThreadPoolTaskExecutorbean (probably used in your job launcher) preventing your app from exiting? - Mahmoud Ben Hassine