User's Manual

134 Chapter 11. Services Tutorials
11.3.4. Email Alerts
The examples above assume that applications will simply need to notify users of some event via email.
However, one of the powerful features of the notification service is that it is built on top of the WAF
messaging service. One of the goals of this design is to make it efficient for messaging applications to
send Message objects directly to users via email, without the need to store any additional information
in the database.
A simple example of this is a bulletin-board (bboard) application that stores each post as a Message,
and then sends each post via email to Users who have subscribed to bboard alerts. In addition to the
basic text of the message, such an application might also need to wrap additional text around it to
include introductory remarks and possibly action links that users can manipulate.
For example, a simple message such as this is my post would typically be sent out with addi-
tional information:
Message-ID:
574077721.1185532184.CCMMail@localhost
Date: Fri, 5 Oct 2001 00:43:21 -0700 (PDT)
From: bboard-robot@somedomain.net
To: user@somedomain.net
Subject: a simple post
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Posted by: Bboard User
Category: Announcement
this is my post
----------
This is a posting from the company news bboard.
To reply you can go to:
http://mysite.com/bboard/news/
To unsubscribe from this or other bboard posts go to:
http://mysite.com/bboard/subscriptions/
The notification API includes methods to define the additional text that is wrapped around the con-
tained Message object so that these emails can be generated without creating and storing a duplicate
Message object in the database. In the example above, the header and signature of the notification
would be specified as follows:
Notification notice = new Notification(to,msg);
notice.setHeader
("Posted by: " + poster.getName() + "\n" +
"Category: " + forum.getCategory());
notice.setSignature
("---------\n" +
"This is a posting from the company news bboard.\n" +
"To reply you can go to:\n" +
"http://mysite.com/bboard/news/\n" +
"To unsubscribe from this or other bboard posts go to:\n" +
"http://mysite.com/bboard/subscriptions/");
notice.save();
Applications that use extensive email alerts will probably want to extend Notification so that the
header and signature are set in a special constructor for each notification to be sent out.
For efficiency, applications should avoid sending email alerts to individual users. It is much more
efficient to store the list of recipients in a Group and send a single notification to the group, letting the
notification service expand the group into the list of individual recipients. If you follow this approach,