Evgeni Dimitrov Evgeni Dimitrov - 4 months ago 88
Java Question

How Spring Boot run batch jobs

I followed this sample for Spring Batch with Boot.

When you run the main method the job is executed.
This way I can't figure out how one can control the job execution. For example how you schedule a job, or get access to the job execution, or set job parameters.

I tried to register my own JobLauncher

public JobLauncher jobLauncher(JobRepository jobRepo){
SimpleJobLauncher simpleJobLauncher = new SimpleJobLauncher();
return simpleJobLauncher;

but when i try to use it in the main method:

public static void main(String[] args) {
ConfigurableApplicationContext ctx = SpringApplication.run(Application.class, args);
JobLauncher jobLauncher = ctx.getBean(JobLauncher.class);
//try catch removed for readability
jobLauncher.run(ctx.getBean(Job.class), new JobParameters());

The job is again xecuted when the context is loaded and i got JobInstanceAlreadyCompleteException when i try to run it manually.
Is there a way to prevet the automatic job execution?


The jobs execution can be prevented by setting


in application.properties. Or you can use spring.batch.job.names it takes a comma-delimited list of job names that will be run.

Taken from here: how to stop spring batch scheduled jobs from running at first time when executing the code?