Announcement Announcement Module
No announcement yet.
Using JobParametersIncrementer Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using JobParametersIncrementer

    is JobOperator the only way to use job's incrementer attribute??

    Considering a job configuration like next:

    <batch:job id="bezzz_LecturaXml" incrementer="IASIncrementer">
    	<batch:step id="Paso1">
    		<batch:tasklet transaction-manager="transactionManager">
    			<batch:chunk reader="Paso1_Reader" processor="Paso1_Processor"
    					writer="Paso1_Writer" commit-interval="3" retry-limit="2">
    <bean id="IASIncrementer" class="es.bde.arq.ias.batch.core.IASIncrementer" />
    Where IASIncrementer class has the same code than InfiniteLoopIncrementer...

    During the tests I've done, I wasn't able to run the incrementer so I don't know how to use it! and so I can't restart any Job's beacuse parameters aren't different.

    Looking across the SpringBatch tests I've only found programtic test of JobOperator the only place where an incrementer is used, I think.

    Any help or examples will be very useful! Thanks!

  • #2
    Take a look at skipSampleJob.xml and in the spring-batch-samples project. This should give you an idea of how the incrementer can be used.

    It uses the InfiniteLoopIncrementer which creates a JobParameters object containing a "" parameter. It gets used in the following bean:

    <beans:bean id="fileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
        <beans:property name="resource" value="classpath:/data/skipJob/input/input#{jobParameters[]}.txt" />
    It is launched using the JobOperator:


    • #3
      Hello All,

      I have a doubt about this. I'm using the CommandLineJobRunner to launch the jobs. ┐How can i use the JobParametersIncrementer Feature in this form of running Jobs?

      I don't control directly the start of the job, and i don't know how to tell to the CommandLineJobRunner to use startNextInstance instead the normal start (this one ignores the incrementer attribute).

      Thanks a lot


      • #4
        You can't do it with the CommandLineJobRunner. You should instead create your own version of the CommandLineJobRunner that uses a JobOperator instead of a JobLauncher.


        • #5
          Ok, i create a copy of the CommandLineJobRunner injecting the jobOperator bean (to use the startNextInstance method) and jobExplorer Bean. I need it to get the jobExecution object from the id returned by startNextInstance method. The jobExecution is required for exitCodeMapper (need to call getExitStatus to build the parameter).

          Then, i added this changes:

          JobExecution jobExecution;
          if (job.getJobParametersIncrementer()==null) {
          	jobExecution =, jobParameters);
          } else {
          	long jobExecutionId = jobOperator.startNextInstance(jobName) ;
          	jobExecution = jobExplorer.getJobExecution(jobExecutionId);
          return exitCodeMapper.intValue(jobExecution.getExitStatus().getExitCode());
          This is, if the job has defined an incrementer, i use it, if not, i use the normal launcher code.

          But (there is always a but ) i want to mix normal parameters (from the command line) with the incrementer one. Why i want this?. Well, i want to control automaticaly (in the incrementer) when two jobs with same parameters can execute in a period of time.

          The problem resides in the JobOperator.startNextInstance. This method don't accept the actual param list loaded from the CommandLineJobRunner launcher.

          Thanks for your Help,



          • #6
            Since the incrementer is a bean in your context, you should be able to get a handle on it from your custom job launcher. You could then set anything you wanted on it to influence its behavior.


            • #7

              I have taken this approach:

              First, i create a new abstract class to define an atrribute to hold the originalJobParameters of the job
              public abstract class AbstractAddParameterIncrementer implements JobParametersIncrementer{
              	JobParameters originalJobParameters;
                    ......getter and setter
              Second, I have changed my custom ParameterIncrementer to extends from the abstract class.

              public class SequenceParameterIncrementer extends AbstractAddParameterIncrementer{
              Third, i have modified my custom CommandLineJobRunner (code in post above) to do this:

              if (job.getJobParametersIncrementer()==null) {
              	jobExecution =, jobParameters);
              } else {
              	JobParametersIncrementer jobParametersIncrementer = job.getJobParametersIncrementer();
              	if (jobParametersIncrementer instanceof AbstractAddParameterIncrementer) {
              	long jobExecutionId = jobOperator.startNextInstance(jobName) ;
              	jobExecution = jobExplorer.getJobExecution(jobExecutionId);
              Now, with this code my CommandLineJobRunner support normal jobs, jobs with standard incrementer and jobs with my customs incrementer (with the appended original parameters behavior).

              How it looks? It's a good practice?


              • #8
                The state (getter / setter) looks a bit unnecessary to me - doesn't JobParametersIncrementer get a reference to the old JobParameters in its getNext() method?


                • #9
                  Hi again,

                  Yes, getNext(..) has a reference to a JobParameters object. But this object aren't the original parameters. They are the last parameters used for launch the job (previous job instance).

                  The problem is that jobOperator.startNextInstance(jobName) ; hasn't recived the jobParameters (like in jobOperator.start(..) does). startNextInstance fetches the last instance and recover its jobsParameters. Then use the incrementer with those parameters (logical way due to flexible nature on incrementers).

                  This is the SimpleJobOperator class implementation