Spring package: restarting a completed job with the same parameters

Is it possible to restart the job in spring package with the same job parameters that completed successfully?

Let's say I have a job with a step that is read from one file and written to another.

For testing purposes, I need to run the task again and again. However, I do not want the param parameter (which today is the date I read from the table) to change over and over again.

Is such a scenario possible?

+5
source share
4 answers
Long startNextInstance(String jobName) 
      throws NoSuchJobException, JobParametersNotFoundException, JobRestartException, 
             JobExecutionAlreadyRunningException, JobInstanceAlreadyCompleteException;

This method of the JobOperator class, together with JobParameterIncrementer, can be used to restart the job, either not completed or completed.

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

+4

Spring . , , "" , . .

org.springframework.batch.core.JobParametersIncrementer , JobParameter, run.id, .

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();
    } }

,

+3

Spring .

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

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.

0
source

All Articles