Java Reference
In-Depth Information
Alternative Output Destination ItemWriters
Files and databases aren't the only ways you can communicate the end result of an item being
processed. Enterprises use a number of other means to store an item after it has been processed. In
Chapter 7, you looked at Spring Batch's ability to call an existing Spring service to obtain data. It should
come as no surprise then that the framework offers similar functionality on the writing end. Spring Batch
also exposes Spring's powerful JMS interactions with a JmsItemWriter . Finally, if you have a requirement
to send e-mails from a batch process, Spring Batch can handle that too. This section looks at how to call
existing Spring services, write to a JMS destination, and send e-mail using provided Spring Batch
In most enterprises that use Spring, there are a number of existing services already written and battle-
tested in production. There is no reason they can't be reused in your batch processes. In Chapter 7, you
looked at how to use them as sources of input for the jobs. This section looks at how the
ItemWriterAdapter allows you to use existing Spring services as ItemWriters as well.
org.springframework.batch.item.adapter.ItemWriterAdapter is nothing more than a thin wrapper
around the service you configure. As with any other ItemWriter, the write method receives a list of items
to be written. ItemWriterAdapter loops through the list calling the service method configured for each
item in the list. It's important to note that the method being called by ItemWriterAdapter can only accept
the item type being processed. For example, if the step is processing Car objects, the method being
called must take a single argument of type Car .
To configure an ItemWriterAdapter , two dependencies are required:
targetObject : The Spring bean that contains the method to be called
targetMethod : The method to be called with each item
Note The method being called by ItemWriterAdapter must take a single argument of the type that is being
processed by the current step.
Let's look at an example of ItemWriterAdapter in action. Listing 9-41 shows the code for a service
that logs Customer items to System.out .
Listing 9-41.
package com.apress.springbatch.chapter9;
public class CustomerServiceImpl {
public void logCustomer(Customer cust) {
System.out.println("I just saved " + cust);
Search WWH ::

Custom Search