Java Reference
In-Depth Information
Customer object. Next is the output configuration, which consists of defining the file and its writer. With
the input and output defined, the bean customerValidatingProcessor will serve as your ItemProcessor.
By default, the ValidatingItemProcessor just passes the item through from the ItemReader to the
ItemWriter, which will work for this example. The only dependency you inject for your ItemProcessor is
the reference to the BeanValidator you write in Listing 8-5. With all of the beans defined, you can build
your step, which is the next piece of the file. All you need for your step is to define the reader, processor,
and writer. With your step defined, you finish the file by configuring the job itself.
To run the job, use the command in Listing 8-8 from the target directory of your project.
Listing 8-8. Running the copyJob
java -jar itemProcessors-0.0.1-SNAPSHOT.jar jobs/copyJob.xml copyJob
customerFile=/tmp/customer.csv outputFile=/tmp/output.csv
As mentioned, you have some bad input that will not pass validation. When you run the job, it fails
due to the ValidationException that is thrown. To get the job to complete successfully, you have to fix
your input to pass validation. Listing 8-9 shows the results of your job when the input fails validation.
Listing 8-9. copyJob Output
2011-02-13 16:31:11,030 DEBUG main [org.springframework.batch.core.step.tasklet.TaskletStep]
- <Applying contribution: [StepContribution: read=10, written=0, filtered=0, readSkips=0,
writeSkips=0, processSkips=0, exitStatus=EXECUTING]>
2011-02-13 16:31:11,031 DEBUG main [org.springframework.batch.core.step.tasklet.TaskletStep]
- <Rollback for RuntimeException:
org.springframework.batch.item.validator.ValidationException: size must be between 1 and 1
>
2011-02-13 16:31:11,031 DEBUG main [org.springframework.batch.repeat.support.RepeatTemplate]
- <Handling exception: org.springframework.batch.item.validator.ValidationException, caused
by: org.springframework.batch.item.validator.ValidationException: size must be between 1 and
1
>
2011-02-13 16:31:11,031 DEBUG main [org.springframework.batch.repeat.support.RepeatTemplate]
- <Handling fatal exception explicitly (rethrowing first of 1):
org.springframework.batch.item.validator.ValidationException: size must be between 1 and 1
>
2011-02-13 16:31:11,032 ERROR main [org.springframework.batch.core.step.AbstractStep] -
<Encountered an error executing the step>
org.springframework.batch.item.validator.ValidationException: size must be between 1 and 1
at
com.apress.springbatch.chapter8.BeanValidator.buildValidationException(BeanValidator.java:40
)
at com.apress.springbatch.chapter8.BeanValidator.validate(BeanValidator.java:28)
at
org.springframework.batch.item.validator.ValidatingItemProcessor.process(ValidatingItemProce
ssor.java:77)
That is all that is required to add item validation to your jobs in Spring Batch. JSR 303 provides the
ability to add custom checks as well as a number of additional annotations out of the box to be able to
 
 
Search WWH ::




Custom Search