Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring batch incrementer is ignored

I hava a spring batch (2.2.2) application and for some reason cannot make the job parameter incremeneter work. The step is declared as this :

<job id="job1" xmlns="http://www.springframework.org/schema/batch" incrementer="incrementer">
    <step id="step1" parent="step" />
</job>

<bean id="incrementer" class="org.springframework.batch.core.launch.support.RunIdIncrementer" />

When I put a breakpoint into the incrementer it is not even called.

Calling the job two times with the same parameter gives the following exception :

A job instance already exists and is complete for parameters={fail=false}.  If you want to run this job again, change the parameters.

I checked the official samples here

https://github.com/spring-projects/spring-batch-admin/tree/master/spring-batch-admin-sample

and it has the same problem

like image 479
Peter Szanto Avatar asked Apr 16 '26 00:04

Peter Szanto


1 Answers

Old question, but still applies to current version (3.0.5):

If you are starting the job execution via

JobExecution jobExecution = launcher.run(job, jobParameters);

using e.g. the SimpleJobLauncher class, then the incrementer is never called. If you check the "callers" of the method Incrementer.getNext(JobParameters), the number of callers is limited:

  • org.springframework.batch.core.launch.support.CommandLineJobRunner

CommandLineJobRunner does the call to getNext() conditionally on "-next" before calling the launcher:

    if (opts.contains("-next")) {
        JobParameters nextParameters = getNextJobParameters(job);
        Map<String, JobParameter> map = new HashMap<String, JobParameter>(nextParameters.getParameters());
        map.putAll(jobParameters.getParameters());
        jobParameters = new JobParameters(map);
    }

    JobExecution jobExecution = launcher.run(job, jobParameters);
  • org.springframework.batch.core.launch.support.SimpleJobOperator

This is used by the Spring Admin web application and it is basically the same implementation as in CommandLineJobRunner:

if (lastInstances.isEmpty()) {
    parameters = incrementer.getNext(new JobParameters());
    if (parameters == null) {
        throw new JobParametersNotFoundException("No bootstrap parameters found for job=" + jobName);
    }
}
else {
    List<JobExecution> lastExecutions = jobExplorer.getJobExecutions(lastInstances.get(0));
    parameters = incrementer.getNext(lastExecutions.get(0).getJobParameters());
}

logger.info(String.format("Attempting to launch job with name=%s and parameters=%s", jobName, parameters));
try {
    return jobLauncher.run(job, parameters).getId();
}
catch (JobExecutionAlreadyRunningException e) {
    throw new UnexpectedJobExecutionException(String.format(ILLEGAL_STATE_MSG, "job already running", jobName,
            parameters), e);
}

So if you are using using the JobLauncher class for starting Jobs, must must take care of yourself for calling the incrementer before calling the jobLauncher to enhance the job parameter with your desired values.

like image 185
Rainer Montag Avatar answered Apr 24 '26 07:04

Rainer Montag