User's Manual

Chapter 11. Services Tutorials 133
// Attach some content fetched from a URL
URL url = new URL("http://www.yahoo.com/");
MessagePart part = new MessagePart("Yahoo", "Yahoo index page");
part.setDataHandler(new DataHandler(url));
msg.attach(part);
// Attach a simple text document
msg.attach("Some additional text", "file.txt", "description);
// Save the message
msg.save();
// Send the message to the recipient
User to = getRecipient();
Notification n = new Notification(to, msg);
n.save();
11.3.3. Creating an Hourly Digest
Applications that send frequent notifications to users are often designed to group those messages
into a digest. A common example is a workflow application that sends users a summary of new and
completed tasks every hour.
The notification service supports using an explicit digest. After creating a digest, you can use it to
send future notifications by passing the digest to the notification constructor. All messages sent to a
user through the same digest will be combined into a single outbound message when the digest is next
processed.
// Party responsible for sending the digest
User from = getDigestSender();
String subject = "my-digest";
String header = "Recent messages";
String footer = "To unsubscribe, visit
http://mysite.net/unsubscribe";
Digest digest = new Digest(from, subject,
header, footer);
digest.setFrequency(Digest.HOURLY);
digest.save();
// Create a couple of notifications and send
// them using the hourly digest
Notification n0 = new Notification(digest, user,
from, "Subject0", "Body0");
n0.save();
Notification n1 = new Notification(digest, user,
from, "Subject1", "Body1");
n1.save();
These two separate notifications will be composed into a single email message and sent to the user
during the next run of the digest queue manager.
Applications typically create their digests when they are installed, and then use them as part of pro-
cessing alerts for users.