|
8.1. Sending Email MessagesIn Chapter 6, you learned how to attach behaviors to a bean. You built an example to write a message to a database whenever an exception was thrown. But it would be much more useful to have an email message sent to you when something breaks. Let's use Spring's email abstraction to do that. 8.1.1. How do I do that?Spring provides a set of services to help you quickly send email messages. You'll need to use three classes to get the support:
First, you'll need to configure a simple message. Do that in the context, like in Example 8-1. Example 8-1. RentABike-servlet.xml<bean > <property name="to"><value>itsupport@rentabike.com</value></property> <property name="from"> <value>problems@rentabike.com</value> </property> <property name="subject"><value>An error occurred</value></property> <property name="text"> <value>There was a problem in the application.</value> </property> </bean> Next, you'll want to configure a sender. You'll use the sender that's packaged with the Spring framework. All you need to do is configure it in the context (Example 8-2). Example 8-2. RentABike-servlet.xml<bean > <property name="host"><value>mail.rentabike.com</value></property> </bean> Finally, you'll need to write a business method. Change the advisor that you made in Chapter 6 (in the Section 6.4 example) to use the new mail client, as in Example 8-3. Example 8-3. ExceptionInterceptor.javaprivate MailSender mailSender; private SimpleMailMessage mailMessage; NOTE TO PROD: NoteWarning was here. public MailSender getMailSender( ) { return mailSender; } public void setMailSender(MailSender mailSender) { this.mailSender = mailSender; } public SimpleMailMessage getMailMessage( ) { return mailMessage; } public void setMailMessage(SimpleMailMessage mailMessage) { this.mailMessage = mailMessage; } public class ExceptionInterceptor implements ThrowsAdvice { public void afterThrowing(Method m, Object[] args, Object target, Exception ex) { try { mailMessage.setText(ex.getMessage( )); mailSender.send(mailMessage); } catch (Exception mex) { // handle mail error } } } Next, you'll have to configure the advisor, as in Example 8-4. Example 8-4. RentABike-servlet.xml<bean > <property name="mailSender"><ref local="mailSender"/></property> <property name="mailMessage"><ref local="mailMessage"/></property> </bean> <bean > <property name="proxyInterfaces"> <value>com.springbook.RentABike</value> </property> <property name="interceptorNames"> <list> <value>exceptionInterceptor</value> <value>transactionInterceptor</value> <value>rentaBikeTarget</value> </list> </property> </bean> Now, do something that will cause an exception. For example, log on with a bad userID. The security interceptor should fire an exception, which in turn should fire an email message. 8.1.2. What just happened?If you'll recall, in Chapter 6 you created an interceptor to log a message. You're using the same advisor. Here's what your code did:
When you ran the code and coerced an exception, the proxy intercepted the exception, and then notified all of the interceptors in the chain for that method. Your interceptor got fired, and sent the email message. |
|