View Javadoc

1   /*
2    * Copyright 2006-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.springframework.batch.item.mail.javamail;
17  
18  import java.util.List;
19  import java.util.Map;
20  import java.util.Map.Entry;
21  
22  import javax.mail.internet.MimeMessage;
23  
24  import org.springframework.batch.item.ItemWriter;
25  import org.springframework.batch.item.mail.DefaultMailErrorHandler;
26  import org.springframework.batch.item.mail.MailErrorHandler;
27  import org.springframework.beans.factory.InitializingBean;
28  import org.springframework.mail.MailException;
29  import org.springframework.mail.MailSendException;
30  import org.springframework.mail.javamail.JavaMailSender;
31  import org.springframework.mail.javamail.MimeMailMessage;
32  import org.springframework.util.Assert;
33  
34  /**
35   * <p>
36   * A simple {@link ItemWriter} that can send mail messages. If it fails there is
37   * no guarantee about which of the messages were sent, but the ones that failed
38   * can be picked up in the error handler. Because the mail protocol is not
39   * transactional, failures should be dealt with here if possible rather than
40   * allowing them to be rethrown (which is the default).
41   * </p>
42   * 
43   * <p>
44   * Delegates the actual sending of messages to a {@link JavaMailSender}, using the
45   * batch method {@link JavaMailSender#send(MimeMessage[])}, which normally uses
46   * a single server connection for the whole batch (depending on the
47   * implementation). The efficiency of for large volumes of messages (repeated
48   * calls to the item writer) might be improved by the use of a special
49   * {@link JavaMailSender} that caches connections to the server in between
50   * calls.
51   * </p>
52   * 
53   * <p>
54   * Stateless, so automatically restartable.
55   * </p>
56   * 
57   * @author Dave Syer
58   * 
59   * @since 2.1
60   * 
61   */
62  public class MimeMessageItemWriter implements ItemWriter<MimeMessage> {
63  
64  	private JavaMailSender mailSender;
65  
66  	private MailErrorHandler mailErrorHandler = new DefaultMailErrorHandler();
67  
68  	/**
69  	 * A {@link JavaMailSender} to be used to send messages in {@link #write(List)}.
70  	 * 
71  	 * @param mailSender
72  	 */
73  	public void setJavaMailSender(JavaMailSender mailSender) {
74  		this.mailSender = mailSender;
75  	}
76  
77  	/**
78  	 * The handler for failed messages. Defaults to a
79  	 * {@link DefaultMailErrorHandler}.
80  	 * 
81  	 * @param mailErrorHandler the mail error handler to set
82  	 */
83  	public void setMailErrorHandler(MailErrorHandler mailErrorHandler) {
84  		this.mailErrorHandler = mailErrorHandler;
85  	}
86  
87  	/**
88  	 * Check mandatory properties (mailSender).
89  	 * 
90  	 * @throws IllegalStateException if the mandatory properties are not set
91  	 * 
92  	 * @see InitializingBean#afterPropertiesSet()
93  	 */
94  	public void afterPropertiesSet() throws IllegalStateException {
95  		Assert.state(mailSender != null, "A MailSender must be provided.");
96  	}
97  
98  	/**
99  	 * @param items the items to send
100 	 * @see ItemWriter#write(List)
101 	 */
102     @Override
103 	public void write(List<? extends MimeMessage> items) throws MailException {
104 		try {
105 			mailSender.send(items.toArray(new MimeMessage[items.size()]));
106 		}
107 		catch (MailSendException e) {
108 			@SuppressWarnings("unchecked")
109 			Map<Object, Exception> failedMessages = e.getFailedMessages();
110 			for (Entry<Object, Exception> entry : failedMessages.entrySet()) {
111 				mailErrorHandler.handle(new MimeMailMessage((MimeMessage)entry.getKey()), entry.getValue());
112 			}
113 		}
114 	}
115 
116 }