My Spring-Batch job is set like that:
@Bean
Job myJob(JobBuilderFactory jobBuilderFactory,
@Qualifier("stepA") Step stepA,
@Qualifier("s"tepB) Step stepB) {
return jobBuilderFactory.get("myJob")
.incrementer(new RunIdIncrementer())
.start(stepA)
.next(stepB)
.build();
}
And here is my launcher:
@Autowired
JobLauncher(@Qualifier("myJob") Job job, JobLauncher jobLauncher) {
this.job = job;
this.jobLauncher = jobLauncher;
}
@Scheduled(fixedDelay=5000)
void launcher() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
jobLauncher.run(job, newExecution());
}
private JobParameters newExecution() {
Map<String, JobParameter> parameters = new HashMap<>();
this.dateTime = new DateTime(DateTimeZone.UTC);
this.dateTimeString = this.dateTime.toString(ISODateTimeFormat.dateTime());
JobParameter parameter = new JobParameter(this.dateTimeString);
parameters.put("currentTime", parameter);
return new JobParameters(parameters);
}
As you can see, my job is scheduled to launch every 5 seconds. But, after first launch, it does not end; it goes on the next execution. The job is like in a loop. I would like it to stop and restart after 5 seconds.
I missed that readers need to return null when they finish. Problem solved.
You can also use System.exit(0); at the end of main class which will terminate JVM resulting in terminatation of batch.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.