ItemWriter, which means it's called once for each item processed in the step where it's used. If you want
to run your own spam operation, this is the ItemWriter for you! This section looks at how to use Spring
Batch's SimpleMailMessageItemWriter to send e-mails from jobs.
Although you probably won't be using this ItemWriter to write a spam-processing program, you can
use it for other things as well. Let's say the customer file you've been processing up to this point is really
a customer import file; after you import all the new customers, you want to send a welcome e-mail to
each one. Using the org.springframework.batch.item.mail.SimpleMailMessageItemWriter is a perfect
way to do that.
For this example, you have a two-step process as you did in the JMS example. The first step imports
the customer.csv file into the customer database table. The second step reads all the customers that
have been imported and sends them the welcome e-mail. Figure 9-6 shows the flow for this job.
Figure 9-6. Flow for the customerImport job
Before you begin coding, let's look at SimpleMailMessageItemWriter . Like all other ItemWriters, it
implements the ItemWriter interface by executing a single write method that takes a list of objects.
However, unlike the ItemWriters you've looked at up to this point, SimpleMailMessageItemWriter doesn't
take just any item. Sending an e-mail requires more information than the text of the e-mail. It needs a
subject, a to address, and a from address. Because of this, SimpleMailMessageItemWriter requires that
the list of objects it takes contain objects that extend Spring's SimpleMailMessage . By doing this,
SimpleMailMessageItemWriter has all the information it needs to build the e-mail message.
But does that mean any item you read in must extend SimpleMailMessage ? That seems like a poor
job of decoupling e-mail functionality from business logic—which is why you don't have to do that. If
you remember, Chapter 8 talked about how ItemProcessors don't need to return an object of the same
type they receive. For example, you can receive a Car object but return an object of type House . In this
case, you create an ItemProcessor that takes in the Customer object and returns the required
To make this work, you reuse the same input file format with a single field appended to the end: the
customer's e-mail address. Listing 9-52 shows an example of the input file you're processing.
Listing 9-52. customerWithEmail.csv
Ann,A,Smith,2501 Mt. Lee Drive,Miami,NE,62935, ASmith@yahoo.com
Laura,B,Jobs,9542 Isabella Ave,Aurora,FL,62344, LJobs@yahoo.com
Harry,J,Williams,1909 4th Street,Seatle,TX,48548, HWilliams@hotmail.com
Larry,Y,Minella,7839 S. Greenwood Ave,Miami,IL,65371, LMinella@hotmail.com
Richard,Q,Jobs,9732 4th Street,Chicago,NV,31320, RJobs@gmail.com
Ann,P,Darrow,4195 Jeopardy Lane,Aurora,CA,24482, ADarrow@hotmail.com
Larry,V,Williams,3075 Wall Street,St. Louis,NY,34205, LWilliams@hotmail.com
Michael,H,Gates,3219 S. Greenwood Ave,Boston,FL,24692, MGates@gmail.com
Harry,H,Johnson,7520 Infinite Loop Drive,Hollywood,MA,83983, HJohnson@hotmail.com
Harry,N,Ellison,6959 4th Street,Hollywood,MO,70398, HEllison@gmail.com
To handle the need for an e-mail address per customer, you need to add an e-mail field to the
Customer object as well. Listing 9-53 shows the updated Customer class.