Java Reference
In-Depth Information
public UserRegistration read() throws Exception,UnexpectedInputException,
ParseException {
Date today = new Date();
Collection<UserRegistration> registrations =
userRegistrationService.getOutstandingUserRegistrationBatchForDate(
1, today);
if (registrations!=null && registrations.size() >= 1)
return registrations.iterator().next();
return null;
}
}
As you can see, the interface is trivial. In this case, you defer most work to a remote service to
provide you with the input. The interface requires that you return one record. The interface is
parameterized to the type of object (the “item”) to be returned. All the read items will be aggregated
and then passed to the
ItemWriter
.
Writing a Custom ItemWriter
The
ItemWriter
example is also trivial. Imagine wanting to “write” by invoking a remote service using
any of the vast support for remoting that Spring provides. The
ItemWriter
interface is parameterized
by the type of item you're expecting to write. Here, you expect a
UserRegistration
object from the
ItemReader
. The interface consists of one method, which expects a
List
of the class's parameterized
type. These are the objects read from
ItemReader
and aggregated. If your
commit-interval
were ten, then
you might expect ten or fewer items in the
List
.
import java.util.List;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.log4j.Logger;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import com.apress.springenterpriserecipes.springbatch.User;
import com.apress.springenterpriserecipes.springbatch.UserRegistrationService;
import com.apress.springenterpriserecipes.springbatch.solution1.UserRegistration;
/**
*
*
* This class writes the user registration by calling an RPC service (whose
* client interface is wired in using Spring
*/
public class UserRegistrationServiceItemWriter implements
ItemWriter<UserRegistration> {
private static final Logger logger = Logger
.getLogger(UserRegistrationServiceItemWriter.class);
Search WWH ::
Custom Search