Java Reference
In-Depth Information
Listing 4-13. Using a JobParametersIncrementer in a Job
<beans:bean id="idIncrementer"
<job id="baseJob" incrementer="idIncrementer">
Once you've configured JobParametersIncrementer (the framework provides in this case), there are two more
things you need to do to make this work. First you need to add the configuration for a JobExplorer
implementation. Chapter 5 goes into detail about what JobExplorer is and how to use it. For now, just
know that Spring Batch needs it to increment parameters. Listing 4-14 shows the configuration, but it's
already configured in the launch-context.xml that is included in the zip file distribution.
Listing 4-14. Configuration for JobExplorer
<bean id="jobExplorer"
<property name="dataSource" ref="dataSource"/>
The last piece of the puzzle to use a JobParametersIncrementer affects how you call your job. When
you want to increment a parameter, you need to add the parameter -next to the command when you
call your job. This tells Spring Batch to use the incrementer as required.
Now when you run your job with the command in Listing 4-15, you can run it as many times as you
want with the same parameters.
Listing 4-15. Command to Run a Job and Increment Parameters
java -jar sample-application-0.0.1-SNAPSHOT.jar jobs/sampleJob.xml
sampleJob name=Michael -next
In fact, go ahead and give it a try. When you've run the sampleJob three or four times, look in the
batch_job_params table and see how Spring Batch is executing your job with two parameters: one String
named name with the value Michael , and one long named . 's value changes each time,
increasing by one with each execution.
You saw earlier that you may want to have a parameter be a timestamp with each run of the job.
This is common in jobs that run once a day. To do so, you need to create your own implementation of
JobParametersIncrementer . The configuration and execution are the same as before. However, instead of
using RunIdIncrementer , you use DailyJobTimestamper , the code for which is in Listing 4-16.
Listing 4-16.
package com.apress.springbatch.chapter4;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersIncrementer;
import java.util.Date;
import org.apache.commons.lang.time.DateUtils;
Search WWH ::

Custom Search