Section 8.1. Sending Email Messages

team bbl


8.1. Sending Email Messages

In 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:

  • The SimpleMailMessage class is a value object that has the mail properties that you'll need, such as subject, body, to, from, and cc properties.

  • The MailException is thrown when something breaks.

  • The MailSender interface and JavaMailSender provide Spring's implementation to send messages. You will, of course, need to configure a mail client.

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.java
private 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:

  • You changed your advisor to use the Spring email client. You injected the value of the email server and set up a simple message in the context.

  • You configured a proxy to intercept calls to the rentABike target. You specified three interceptors, one of which is the new exception handler.

  • The proxy calls the interceptor chain whenever a specified method throws any exception. You then configured the interceptor to point to all methods on the rentABike target, and added it to the context.

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.

    team bbl



    Spring. A developer's Notebook
    Spring: A Developers Notebook
    ISBN: 0596009100
    EAN: 2147483647
    Year: 2005
    Pages: 90

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net