Encapsulates mail transport agent with FIFO queue and non-blocking send capabilities. More...
Classes | |
interface | Context |
Provides message presentation and call-back context for the instance of MailTransport class. More... | |
Public Member Functions | |
MailTransport (Context context, String smtpServer, int smtpPort) | |
Creates instance of the mail transport with the (new) session to SMTP server. | |
int | getQueueSize () |
Gets number of messages in the queue. | |
void | startSending () |
Starts the worker thread (if not already started) | |
void | noMoreMessages () |
Sends QUIT message to the worker thread. | |
void | enqueueMessage (String from, String[] to, String subject, String textMessage) throws MessagingException |
Creates and puts new MIME text message into outgoing queue. | |
void | run () |
Waits for messages to become available in the queue, then takes messages (first-in first-out) from the queue and relays them through the session to remote SMTP server. | |
Private Attributes | |
Thread | workerThread = null |
Instance of the main thread. | |
Context | context = null |
Presentation contexts. | |
Session | session = null |
Session to SMTP server. | |
LinkedBlockingQueue< Message > | queue = null |
FIFO message queue served by worker thread. | |
Message | quitSignal = null |
The special message used to signal the worker thread to stop processing of messages and terminate. |
Encapsulates mail transport agent with FIFO queue and non-blocking send capabilities.
Instances of this class may be used in two different scenarios (note the position of MailTransport.startSending() method and the usage of MailTransport.noMoreMessages() bellow):
Usage:
Usage:
Definition at line 44 of file MailTransport.java.
MailTransport.MailTransport | ( | Context | context, |
String | smtpServer, | ||
int | smtpPort | ||
) |
Creates instance of the mail transport with the (new) session to SMTP server.
Definition at line 97 of file MailTransport.java.
References context, queue, quitSignal, and session.
{ this.context = context; this.queue = new LinkedBlockingQueue<Message> (); this.quitSignal = new MimeMessage( this.session ); Properties relayHost = new Properties(); relayHost.put( "mail.smtp.host", smtpServer ); relayHost.put( "mail.smtp.port", smtpPort ); /* Create instance of the session. * DO NOT USE: getDefaultInstance() as it will create cached session. */ this.session = Session.getInstance( relayHost, null ); }
void MailTransport.enqueueMessage | ( | String | from, |
String[] | to, | ||
String | subject, | ||
String | textMessage | ||
) | throws MessagingException |
Creates and puts new MIME text message into outgoing queue.
Definition at line 157 of file MailTransport.java.
References queue, and session.
Referenced by SendMailApp.enqueueMessage().
{ InternetAddress[] toAddr = new InternetAddress[ to.length ]; for( int i = 0; i < to.length; i++ ) { toAddr[i] = new InternetAddress( to[i] ); } MimeMessage mimeMsg = new MimeMessage( session ); mimeMsg.setFrom( new InternetAddress( from ) ); mimeMsg.setRecipients( Message.RecipientType.TO, toAddr ); mimeMsg.setHeader( "X-Mailer", "IP1-MailSender" ); mimeMsg.setSubject( subject ); mimeMsg.setContent( textMessage, "text/plain" ); /* Places the message into outgoing queue... */ try { this.queue.put( mimeMsg ); } catch( InterruptedException e ) { /* ignore */ } }
int MailTransport.getQueueSize | ( | ) |
Gets number of messages in the queue.
Definition at line 116 of file MailTransport.java.
References queue.
Referenced by SendMailApp.enqueueMessage(), and SendMailApp.onSendCompleted().
{ return this.queue.size (); }
void MailTransport.noMoreMessages | ( | ) |
Sends QUIT message to the worker thread.
Definition at line 140 of file MailTransport.java.
References queue, and quitSignal.
Referenced by SendMailApp.enqueueMessage(), and SendMailApp.formWindowClosing().
{ synchronized( this ) { /* put the 'QUIT' message into queue */ try { this.queue.put( this.quitSignal ); } catch( InterruptedException e ) { /* ignore */ } } }
void MailTransport.run | ( | ) |
Waits for messages to become available in the queue, then takes messages (first-in first-out) from the queue and relays them through the session to remote SMTP server.
After receiving special 'QUIT' message, the worker thread terminates. In case of error, thread also quits and reports the error using Context.onSendCompleted() from the MailTransport.Context call-back interface.
Definition at line 191 of file MailTransport.java.
References context, MailTransport.Context.onSendCompleted(), MailTransport.Context.println(), queue, quitSignal, and workerThread.
{ context.println( "MailTransport(" + workerThread.getId () + "): Worker thread started..." ); context.println( "MailTransport(" + workerThread.getId () + "): Found " + queue.size() + " messages in queue." ); String errorMessage = null; // error to be reported to the owner int curMessageNo = 0; // keeps track of the current message id while( true ) { Message msgToSend = null; /* Retrieves and removes the head message from the queue, * waiting if necessary until an element becomes available). * Terminates the thread in case of interrupt. */ try { context.println( "MailTransport(" + workerThread.getId () + "): Waiting for messsages..." ); msgToSend = this.queue.take (); } catch( InterruptedException e ) { break; } /* Terminates the thread if the last retrieved message is * the 'QUIT thread' message. */ if ( msgToSend == quitSignal || msgToSend == null ) { context.println( "MailTransport(" + workerThread.getId () + "): Got signal to 'QUIT'..." ); break; } /* Sends message via transport protocol */ try { ++curMessageNo; context.println( "MailTransport(" + workerThread.getId () + "): Sending message #" + curMessageNo + "..." ); for( javax.mail.Address a : msgToSend.getFrom() ) { context.println( " From: " + a.toString () ); } for( javax.mail.Address a : msgToSend.getRecipients( RecipientType.TO ) ) { context.println( " To: " + a.toString () ); } context.println( " Subject: " + msgToSend.getSubject () ); Transport.send( msgToSend ); context.println( "MailTransport(" + workerThread.getId () + "): MESSAGE SENT!" ); } catch( MessagingException e ) { context.println( "MailTransport(" + workerThread.getId () + "): Error\n\n" + e.toString () + "\n" ); errorMessage = e.toString (); break; // there is no point to continue in case of error... } } context.println( "MailTransport(" + workerThread.getId () + "): Thread done." ); context.onSendCompleted( errorMessage ); }
void MailTransport.startSending | ( | ) |
Starts the worker thread (if not already started)
Definition at line 124 of file MailTransport.java.
References workerThread.
Referenced by SendMailApp.enqueueMessage().
{ synchronized( this ) { if ( workerThread != null ) { return; // already started (there can be only one worker thread) } this.workerThread = new Thread( this ); this.workerThread.start (); } }
Context MailTransport.context = null [private] |
Presentation contexts.
Definition at line 75 of file MailTransport.java.
Referenced by MailTransport(), and run().
LinkedBlockingQueue<Message> MailTransport.queue = null [private] |
FIFO message queue served by worker thread.
Definition at line 85 of file MailTransport.java.
Referenced by enqueueMessage(), getQueueSize(), MailTransport(), noMoreMessages(), and run().
Message MailTransport.quitSignal = null [private] |
The special message used to signal the worker thread to stop processing of messages and terminate.
Definition at line 91 of file MailTransport.java.
Referenced by MailTransport(), noMoreMessages(), and run().
Session MailTransport.session = null [private] |
Session to SMTP server.
Definition at line 80 of file MailTransport.java.
Referenced by enqueueMessage(), and MailTransport().
Thread MailTransport.workerThread = null [private] |
Instance of the main thread.
Definition at line 70 of file MailTransport.java.
Referenced by run(), and startSending().