3

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

Peter Szanto
  • 7,568
  • 2
  • 51
  • 53

3 Answers3

9

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.

Rainer Montag
  • 493
  • 1
  • 6
  • 13
1

I see that nobody gave you a correct answer so here is it (even if it is one year late maybe it will help others):

You can directly call the main function of the CommandLineJobRunner class. For example:

String[] args = new String[]{"spring/batch/jobs/helloWorldJob.xml", "helloWorldJob", "name(string)=Spring", "-next"};
CommandLineJobRunner.main(args);

Basically this is what you are doing when you are running Spring Batch from the command line (or any other java application in fact).

Dan Nemes
  • 300
  • 3
  • 16
0

If you run with CommandLineJobRunner use the -next option else the best solution is to use a timestamp job parameter to make every job instance different from others.

Luca Basso Ricci
  • 17,829
  • 2
  • 47
  • 69
  • Thanks I saw this, but solution, but not sure how this applies in my scenario as I am running the batch jobs inside a war file of a web application. I would like to produce a generic war file, that can be deployed in any web container, so any such flag should be inside the war not coming from an external parameter. See the whole web app in the link in my post. – Peter Szanto Nov 07 '13 at 08:03