How Spring Boot run batch jobs
Solution 1
The jobs execution can be prevented by setting
spring.batch.job.enabled=false
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?
Solution 2
You can enable the execution of a Job using rest controller POST:
@RestController
@RequestMapping(value="/job/")
public class JobLauncherController {
private static final Log LOG = LogFactory.getLog(JobLauncherController.class);
@Autowired
private JobLauncher jobLauncher;
@Autowired
private Job job;
@Autowired
private JobRepository jobRepository;
@Autowired
private JobRegistry jobRegistry;
@RequestMapping("/launchjob/{jobName}")
public String handle(@PathVariable("jobName") String jobName, @RequestBody Map<String,Object> request) throws Exception {
try {
request.put("timeJobStarted", DateUtil.getDateFormatted(new Date(), DateUtil.DATE_UUUUMMDDHHMMSS));
Map<String,Object> mapMessage = this.enrichJobMessage(request);
Map<String, JobParameter> jobParameters = new HashMap<>();
mapMessage.forEach((k,v)->{
MapperUtil.castParameter(jobParameters, k, v);
});
jobParameters.put(Field.Batch.JOB_INSTANCE_NAME, new JobParameter(jobName));
jobLauncher.run(job, new JobParameters(jobParameters));
assertNotNull(jobRegistry.getJob(job.getName()));
}catch( NoSuchJobException ex){
jobRegistry.register(new ReferenceJobFactory(job));
} catch (Exception e) {
LOG.error(e.getMessage(),e);
}
return "Done";
}
public static void castParameter(Map<String, JobParameter> jobParameters, String k, Object v){
if(v instanceof String){
jobParameters.put(k, new JobParameter((String)v));
}else if(v instanceof Date){
jobParameters.put(k, new JobParameter((Date)v));
}else if(v instanceof Double){
jobParameters.put(k, new JobParameter((Double)v));
}else if(v instanceof Long){
jobParameters.put(k, new JobParameter((Long)v));
}else{
DslJson dslJson = new DslJson<>();
JsonWriter writer = dslJson.newWriter();
try {
dslJson.serialize(writer,v);
jobParameters.put(k, new JobParameter(writer.toString()));
} catch (IOException e) {
LOG.warn(e.getMessage(), e);
}
}
}
}
Related videos on Youtube
Evgeni Dimitrov
Updated on July 09, 2022Comments
-
Evgeni Dimitrov almost 2 years
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
@Bean public JobLauncher jobLauncher(JobRepository jobRepo){ SimpleJobLauncher simpleJobLauncher = new SimpleJobLauncher(); simpleJobLauncher.setJobRepository(jobRepo); 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 executed when the context is loaded and I got
JobInstanceAlreadyCompleteException
when I try to run it manually. Is there a way to prevent the automatic job execution? -
Anuj Acharya almost 9 yearsI tried spring.batch.job.enabled=false and also running the jar with java sys properties -Dspring.batch.job.enabled=false still it execute the job. How it debug it, I have no clue
-
Evgeni Dimitrov almost 9 years@Anuj Acharya You can ask a question about it where you can give more details about the problem(like the spring version) and share some code.
-
Anuj Acharya almost 9 yearsSorry Evgeni, I have created a seprate question stackoverflow.com/questions/31276011/…