Java Reference
In-Depth Information
public void setName(String newName) {
name = newName;
}
public String getName() {
return name;
}
}
With the ability to pass parameters into your jobs as well as put them to use, two parameter-specific
pieces of functionality are built into the Spring Batch framework that the chapter discusses next:
parameter validation and the ability to increment a given parameter with each run. Let's start with
parameter validation because it's been alluded to in previous examples.
Validating Job Parameters
Whenever a piece of software obtains outside input, it's a good idea to be sure the input is valid for what
you're expecting. The web world uses client-side JavaScript as well as various server-side frameworks to
validate user input, and validation of batch parameters is no different. Fortunately, Spring has made it
very easy to validate job parameters. To do so, you just need to implement the
org.springframework.batch.core.JobParametersValidator interface and configure your implementation
in your job. Listing 4-11 shows an example of a job parameter validator in Spring Batch.
Listing 4-11. A Parameter Validator that Validates the Parameter Name Is a String
package com.apress.springbatch.chapter4;
import java.util.Map;
import org.springframework.batch.core.*;
import org.apache.commons.lang.StringUtils;
public class ParameterValidator implements JobParametersValidator{
public void validate(JobParameters params) throws
JobParametersInvalidException {
String name = params.getString("name");
if(!StringUtils.isAlpha(name)) {
throw new
JobParametersInvalidException("Name is not alphabetic");
}
}
}
As you can see, the method of consequence is the validate method. Because this method is void,
the validation is considered passing as long as a JobParametersInvalidException isn't thrown. In this
example, if you pass the name 4566, the exception is thrown and the job completes with a status of
COMPLETED . This is important to note. Just because the parameters you passed in weren't valid doesn't
mean the job didn't complete correctly. In the case where invalid parameters are passed, the job is
marked as COMPLETED because it did all valid processing for the input it received. And when you think
about this, it makes sense. A JobInstance is identified by the job name and the parameters passed into
 
Search WWH ::




Custom Search