Java Reference
In-Depth Information
// this is the client interface to an HTTP Invoker service.
@Autowired
private UserRegistrationService userRegistrationService;
/**
* takes aggregated input from the reader and 'writes' them using a custom
* implementation.
*/
public void write(List<? extends UserRegistration> items)
throws Exception {
for (final UserRegistration userRegistration : items) {
User registeredUser = userRegistrationService
.registerUser(userRegistration);
logger.debug("Registered:"
+ ToStringBuilder.reflectionToString(registeredUser));
}
}
}
Here, you've wired in the service's client interface. You simply loop through the items and invoke
the service. If you remove the gratuitous spacing, curly brackets and logging output, it becomes two lines
of code to satisfy the requirement.
9-6. Processing Input Before Writing
Problem
While transferring data directly from a spreadsheet or CSV dump might be useful, one can imagine
having to do some sort of processing on the data before it's written. Data in a CSV file, and more
generally from any source, is not usually exactly the way you expect it to be or immediately suitable for
writing. Just because Spring Batch can coerce it into a POJO on your behalf, that doesn't mean the state
of the data is correct. There may be additional data that you need to infer or fill in from other services
before the data is suitable for writing.
Solution
Spring Batch will let you do processing on reader output. This processing can do virtually anything to
the output before it gets passed to the writer , including changing the type of the data.
How It Works
Spring Batch gives the implementor a chance to perform any custom logic on the data read
from reader . The processor attribute on the chunk element expects a reference to a bean of type
org.springframework.batch.item.ItemProcessor<I,O> . Thus, the revised definition for the job from
the previous recipe looks like this:
<job
job-repository="jobRepository"
id="insertIntoDbFromCsvJob">
 
Search WWH ::




Custom Search