Java Reference
In-Depth Information
<step id="step1">
<tasklet transaction-manager="transactionManager">
<chunk
reader="csvFileReader"
processor = "userRegistrationValidationProcessor"
writer="jdbcItemWriter"
commit-interval="5"
/>
</tasklet>
</step>
</job>
The goal is to do certain validations on the data before you authorize it to be written to the database.
If you determine the record is invalid, you can stop further processing by returning null from the
ItemProcessor. This is crucial and provides a necessary safeguard. One thing that you want to do is
ensure that the data is the right format (for example, the schema may require a valid two-letter state
name instead of the longer full state name). Telephone numbers are expected to follow a certain format,
and you can use this processor to strip the telephone number of any extraneous characters, leaving only
a valid (in the United States) ten-digit phone number. The same applies for U. S. zip codes, which
consist of five characters and optionally a hyphen followed by a four-digit code. Finally, while a
constraint guarding against duplicates is best implemented in the database, there may very well be some
other “eligibility” criteria for a record that can be met only by querying the system before insertion.
Here's the configuration for the ItemProcessor :
<beans:bean id="userRegistrationValidationProcessor"
class="com.apress.springenterpriserecipes.springbatch.solution1.
UserRegistrationValidationItemProcessor" />
In the interest of keeping this class short, I won't reprint it in its entirety, but the salient bits should
be obvious:
package com.apress.springenterpriserecipes.springbatch.solution2;
import java.util.Arrays;
import java.util.Collection;
import org.apache.commons.lang.StringUtils;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.annotation.BeforeStep;
import org.springframework.batch.item.ItemProcessor;
import com.apress.springenterpriserecipes.springbatch.solution1.UserRegistration;
public class UserRegistrationValidationItemProcessor
implements ItemProcessor<UserRegistration, UserRegistration> {
private String stripNonNumbers(String input) { /* … */ }
private boolean isTelephoneValid(String telephone) { /* … */ }
private boolean isZipCodeValid(String zip) { /* … */ }
private boolean isValidState(String state) { /* … */ }
Search WWH ::




Custom Search