| CONTENTS | PREV | NEXT | INDEX | J2EE BluePrints |
The sample application uses stateless session beans for shared service objects. For example, in an e-commerce application, you might want to send order confirmation mail to customers on successful completion of an order. Such a service can be shared by all clients of the application. The sample application Mailer service objects are stateless session beans.
When a client places an order, an order event is passed to ShoppingClientController. Although the handleEvent method is defined by ShoppingClientController, ShoppingClientController delegates its implementation to a helper class named StateMachine. StateMachine interacts with Inventory, Order, and Mailer enterprise beans to debit the quantity of the purchased item, insert the order details, and finally send the confirmation email to the client.
The last thing that StateMachine does in the createOrder
method is to send an order confirmation message. It does this by first creating
Mailer stateless session bean and then invoking the Mailer.sendOrderConfirmationMail
method (shown in Code Example 10.21).
This method uses the order ID to obtain the information needed
for the confirmation message from the Order and Account
entity beans. The Mailer then invokes the createAndSendMail method
of its helper class MailHelper.
public void sendOrderConfirmationMail(int orderId)
throws RemoteException {
OrderDetails orderDetails = null;
try {
OrderHome orderHome = EJBUtil.getOrderHome();
Order order = orderHome.findByPrimaryKey(orderId);
orderDetails = order.getOrderDetails();
} catch (FinderException fe) {
...
return;
}
String userId = orderDetails.getUserId();
AccountDetails acctDetails = null;
try {
AccountHome acctHome = EJBUtil.getAccountHome();
Account acct = acctHome.findByPrimaryKey(userId);
acctDetails = acct.getAccountDetails();
} catch (FinderException fe) {
...
}
String subject = "Your order# "+orderId;
String HTML Contents =
"This message is a confirmation of your order# "
+ orderId + ". Please save it for your records.";
getMailHelper().createAndSendMail(acctDetails.
getEmail(), subject, HTML Contents);
}
Code Example 10.21 Mailer.sendOrderConfirmationMail
|
Code Example 10.22 illustrates the
createAndSendMail method of MailHelper. This method
looks up a mail session in the JNDI namespace, creates a MIME message, sets
the mail headers, collects the contents of the message into a string, and then
sends the message.
public void createAndSendMail(String to,
String subject, String HTML Contents) {
try {
InitialContext ic = new InitialContext();
Session session = (Session) ic.
lookup(JNDI Names.MAIL_SESSION);
// construct the message
Message msg = new MimeMessage(session);
msg.setFrom();
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to, false));
msg.setSubject(subject);
collect(subject, htmlContents, msg);
msg.setHeader("X-Mailer", "JavaMailer");
msg.setSentDate(new Date());
// send the message
Transport.send(msg);
} catch (Exception e) {
...
}
}
Code Example 10.22 MailHelper.createAndSendMail
|