I am working on a Spring Batch application and I am finding some difficulties trying to correctly schedule a job.
I will try to explain my problem in details. I have this class where my job was scheduled:
/**
* This bean schedules and runs our Spring Batch job.
*/
@Component
@Profile("!test")
public class SpringBatchExampleJobLauncher {
private static final Logger LOGGER = LoggerFactory.getLogger(SpringBatchExampleJobLauncher.class);
@Autowired
@Qualifier("launcher")
private JobLauncher jobLauncher;
@Autowired
@Qualifier("updateNotaryDistrictsJob")
private Job updateNotaryDistrictsJob;
@Autowired
@Qualifier("updateNotaryListInfoJob")
private Job updateNotaryListInfoJob;
@Scheduled(cron = "0 */5 * * * *")
public void runUpdateNotaryDistrictsJob() {
LOGGER.info("SCHEDULED run of updateNotaryDistrictsJob STARTED");
Map<String, JobParameter> confMap = new HashMap<>();
confMap.put("time", new JobParameter(System.currentTimeMillis()));
JobParameters jobParameters = new JobParameters(confMap);
try {
jobLauncher.run(updateNotaryDistrictsJob, jobParameters);
}catch (Exception ex){
LOGGER.error(ex.getMessage());
}
}
}
As you can see on my runUpdateNotaryDistrictsJob() method I set this Spring CRON expression:
@Scheduled(cron = "0 */5 * * * *")
in order to start my updateNotaryDistrictsJob every 5 minuts.
The problem is that when I run my application in debug mode I can see that the job is immediatly performed (it stop on the first breackpoint). It seems that it is not waiting the 5 minutes set by the cron expression.
Why? What is wrong? How can I try to solve this issue?
Tnx
CodePudding user response:
The cron expression 0 */5 * * * *
does not read as you expect. "It seems that it is not waiting the 5 minutes set by the cron expression.", this is not what the cron expression defines. The cron expression will run at second 0, every 5 minutes starting at minute 0, of every hour, which means that it will never wait 5 minutes after the service has started. As an example, if you started it at 10:22, it will run at 10:25.
If you really need it to wait 5 minutes after service has started, you should consider using @Scheduled(fixedRate = 5000, initialDelay = 5000)
.