Section 8.3. Upgrading the MDB to Send an Email Message


8.3. Upgrading the MDB to Send an Email Message

If you'll recall from the JMS and MDB chapter, the CreditCheckProcessor MDB's onMessage( ) method consumes the message and invokes a simulated external credit verification service. Example 8-1 shows the changes necessary to send an email message with JavaMail.

Example 8-1. CreditCheckProcessorBean.java
 public class CreditCheckProcessorBean implements MessageDrivenBean, MessageListener {     private static final String JAVAMAIL_SESSION =                                            "java:comp/env/mail/JawJavaMailSession";     private static final String JAW_MOTORS_EMAIL_ADDRESS =                                            "credit.check@jbossatwork.com";     private static final String CREDIT_VERIFICATION_RESULT = "Credit Check Result";     ...     public void onMessage(Message message) {         System.out.println(             "CreditCheckProcessorBean.onMessage(  ): Received message.");         try {             if (message instanceof ObjectMessage) {                 ObjectMessage objMessage = (ObjectMessage) message;                 Object obj = objMessage.getObject(  );                 if (obj instanceof CreditCheckRequestDTO) {                     String result = null;                         CreditCheckRequestDTO creditCheckReq =                                                    (CreditCheckRequestDTO) obj;                     System.out.println("Verifying Credit ...");                     result = CreditVerificationService.verifyCredit(                                                          creditCheckReq);                     System.out.println("Credit Check Result = [" + result + "]");                     sendNotificationEmail(creditCheckReq, result);                 } else {                     System.err.println(                                     "Expecting CreditCheckRequestDTO in Message");                 }             } else {                 System.err.println("Expecting Object Message");             }         } catch (Throwable t) {             t.printStackTrace(  );         }     }     private void sendNotificationEmail(CreditCheckRequestDTO creditCheckReq,                                        String result) {         javax.mail.Session javaMailSession = null;         try {             javaMailSession =                           ServiceLocator.getJavaMailSession(JAVAMAIL_SESSION);             TextEmail email = new TextEmail(javaMailSession);             email.setBody(result);             email.setSender(JAW_MOTORS_EMAIL_ADDRESS);             email.setSubject(CREDIT_VERIFICATION_RESULT);             email.addRecipient(creditCheckReq.getEmail(  ));             System.out.println("Sending Email to [" + creditCheckReq.getEmail(  ) +                                "] ...");             email.send(  );         } catch (ServiceLocatorException sle) {             System.err.println("Error Looking up JavaMail Session: " + sle);             sle.printStackTrace(  );         }     } } 

The onMessage( ) method consumes a JMS message that contains the user's credit information. After pulling the CreditCheckRequestDTO out of the JMS message, we pass it to our emulated external credit verification service. The CreditVerificationService.verifyCredit( ) method simulates a long-running process and returns a String result value that indicates whether the credit check passed or failed. The sendNotificationEmail( ) method takes the CreditCheckRequestDTO and credit check result as parameters and sends a notification email to the customer. The sendNotificationEmail( ) method uses the ServiceLocator to look up a JavaMail Session by using JNDI, and then invokes the TextEmail utility class to send the email message. Some readers may not be familiar with using JNDI to look up a JavaMail Sessionwe'll cover this in the "JavaMail Sessions" section. The "Factoring out JNDI Calls" section shows the new ServiceLocator method used to find the JavaMail Session.

Let's take a closer look at the TextEmail utility to see how it uses the JavaMail API calls to send an email message.



JBoss at Work. A Practical Guide
JBoss at Work: A Practical Guide
ISBN: 0596007345
EAN: 2147483647
Year: 2004
Pages: 197

Similar book on Amazon

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