Tomcat 6 thread safe email queue (javax.mail.*)
Hi I have design/architecture question. I would like to send emails from one of my jsp pages. I have one particular issue that has been a little bit of a problem. there is an instance where one of the pages will need to send around 50 emails at near the same time. I would like the messages sent to a开发者_如何学Python queue where a background thread will actually do the email sending. What is the appropriate way to solve this problem? If you know of a tutorial, example code or tomcat configuration is needed please let me know.
Thanks,
Your solution is rather sound: append the messages to a internal queue and then let some background task handle them.
Here are a few pointers that may be useful:
- Unless you want to go distributed (in which case you should look at JMS), use a
BlockingQueue
implementation for your queue. In your background thread, just do an infinite loop whiletake()
-ing messages from the queue. Those classes take care of potential concurrency issues for you. - Use a
ServletContextListener
to set up your background thread when your Web application starts and when it is stopped.
One possible problem with using a raw BlockingQueue
is that when your Web application is stopped, all the messages in the queue are lost. If that's a serious problem, then it would probably be easiest just to use a database for the queue and to use notify()
to wake up your background thread, which then processes all requests from the database.
精彩评论