spring batch: restarting a completed job with same parameters

Long startNextInstance(String jobName) 
      throws NoSuchJobException, JobParametersNotFoundException, JobRestartException, 
             JobExecutionAlreadyRunningException, JobInstanceAlreadyCompleteException;

This method of the JobOperator class along with a JobParameterIncrementer can be used to restart a job, either failed or completed.

http://static.springsource.org/spring-batch/apidocs/org/springframework/batch/core/launch/support/SimpleJobOperator.html#startNextInstance(java.lang.String)


Spring Batch requires unique job parameters for its execution.so you can add the current time as a job parameter

Map<String, JobParameter> confMap = new HashMap<String, JobParameter>();
confMap.put("time", new JobParameter(System.currentTimeMillis()));
JobParameters jobParameters = new JobParameters(confMap);
jobLauncher.run(springCoreJob, jobParameters);

Spring Batch requires unique job parameters for its execution. In your case, if you want to run the same job with the same date parameter, than you should "add" another job parameter to make it unique. You may think of it unique job parameter set.

org.springframework.batch.core.JobParametersIncrementer interface can be used in this scenario, just give it your JobParameter and it will add a run.id that will make it unique.

public class SampleIncrementer implements JobParametersIncrementer {  

    public JobParameters getNext(JobParameters parameters) { 
        if (parameters==null || parameters.isEmpty()) {
            return new JobParametersBuilder().addLong("run.id", 1L).toJobParameters();
        }
        long id = parameters.getLong("run.id",1L) + 1;
        return new JobParametersBuilder().addLong("run.id", id).toJobParameters();
    } }

You may check a simple sample using it

Tags:

Spring Batch