Java Reference
In-Depth Information
the job. If you pass in invalid parameters, you don't want to repeat that, so it's ok to declare the job
In addition to implementing your own custom parameter validator as you did earlier, Spring Batch
offers a validator to confirm that all the required parameters have been passed:
org.springframework.batch.core.job.DefaultJobParametersValidator . To use it, you configure it the
same way you would your custom validator. DefaultJobParametersValidator has two optional
dependencies: requiredKeys and optionalKeys . Both are String arrays that take in a list of parameter
names that are either required or are the only optional parameters allowed. Listing 4-12 shows two
configurations for DefaultJobParametersValidator as well as how to add it to your job.
Listing 4-12. DefaultJobParametersValidator Configuration in parameterValidatorJob.xml
<beans:bean id="requiredParamValidator"
<beans:property name="requiredKeys" value=",batch.runDate"/>
<beans:bean id="optionalParamValidator"
<beans:property name="requiredKeys" value=",batch.runDate"/>
<beans:property name="optionalKeys" value="batch.address"/>
<job id="parameterValidatorJob">
<validator ref="requiredParamValidator"/>
If you use requiredParamValidator , your job throws an exception if you don't pass the parameters and batch.runDate . You're allowed to pass more parameters in if required, but those two
can't be null. On the other hand, if you use optionalParamValidator , the job once again throws an
exception if and batch.runDate aren't passed to the job, but it also throws an exception if any
parameters in addition to batch.address are passed. The difference between the two validators is that
the first one can accept any parameters in addition to the required ones. The second one can only accept
the three specified. In either case, if the invalid scenario occurs, a JobParametersInvalidException is
thrown and the job is marked as completed as previously discussed.
Incrementing Job Parameters
Up to now, you've been running under the limitation that a job can only be run once with a given set of
parameters. If you've been following along with the examples, you've probably hit what happens if you
attempt to run the same job twice with the same parameters as shown in Listing 4-5. However, there is a
small loophole: using JobParametersIncrementer .
org.springframework.batch.core.JobParametersIncrementer is an interface that Spring Batch
provides to allow you to uniquely generate parameters for a given job. You can add a timestamp to each
run. You may have some other business logic that requires a parameter to be incremented with each
run. The framework provides a single implementation of the interface, which increments a single long
parameter with the default name .
Listing 4-13 shows how to configure a JobParametersIncrementer for your job by adding the
reference to the job.
Search WWH ::

Custom Search