Use an Email Management Framework
Single applications should have as few points of contact with the external email layer as possible. This allows the centralization of logic to deal with concurrency issues—only one thread ever accesses the mailbox. To help you out with this, I present a basic framework for handling incoming messages. The framework is based on the Strategy design pattern, which allows you to plug in different kinds of processing behavior.[41] Having this framework will allow you to easily extend your applications to handle different kinds of messages by creating new instances of a monitoring thread pointed at different mailboxes and passing in handler objects that know how to deal with particular message types.
Start by declaring an interface that can be implemented by classes
that will be able to handle incoming messages. I will keep it simple
for this example, so you will define just one kind of interface, a
BlockingMailHandler
,
which handles a particular message and returns when processing for
that message has completed. (See Example 10-4.)
public interface BlockingMailHandler
{
public boolean handleMessage(javax.mail.Message message);
}A more complete implementation would result in a
BlockingMailHandler from a
MailHandler
,
and probably define an
AsynchronousMailHandler
as well.
To handle incoming messages, you create a class that implements the
BlockingMailHandler interface and does something
interesting in the handleMessage( ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access