public void run()
{
LOG.info("Processing batch...");
try
{
Job job = createNewJob();
JobParameters jobParameters = new JobParameters();
Optional<JobInstance> existingInstance = jobExplorer.getJobInstances(job.getName(), 0, 1).stream().findFirst();
if (existingInstance.isPresent())
{
jobParameters = job.getJobParametersIncrementer().getNext(jobParameters);
LOG.warn("Trying to restart task \"{}\" with the parameters [{}]", job, jobParameters);
}
jobLauncher.run(job, jobParameters);
}
catch (JobExecutionAlreadyRunningException ex)
{
LOG.warn("The task \"{}\" is already running", BillingBatchConfig.QUALIFIER);
}
catch (JobRestartException ex)
{
LOG.warn("The task \"{}\" cannot be restarted", BillingBatchConfig.QUALIFIER);
}
catch (JobInstanceAlreadyCompleteException ex)
{
LOG.warn("The task \"{}\" cannot be restarted cause its finished", BillingBatchConfig.QUALIFIER);
}
catch (JobParametersInvalidException ex)
{
LOG.warn("The task \"{}\" cannot be excecuted cause the parameters are invalid", BillingBatchConfig.QUALIFIER);
}
catch (Exception ex)
{
LOG.warn("Unexpected error running the task \"{}\"", BillingBatchConfig.QUALIFIER);
}
}
The key is to first check if there is an existing instance org.springframework.batch.core.explore.JobExplorerin the database of the desired job. If so, you need to get started using the parameters obtained with job.getJobParametersIncrementer().getNext(jobParameters). That's all.
Eliux source
share