Tomcat 6 线程安全电子邮件队列 (javax.mail.*)
您好,我有设计/架构问题。我想从我的 jsp 页面之一发送电子邮件。我有一个特殊的问题,这有点问题。有一种情况,其中一个页面需要几乎同时发送大约 50 封电子邮件。我希望将消息发送到一个队列,其中后台线程将实际执行电子邮件发送。解决这个问题的适当方法是什么?如果您知道需要教程、示例代码或 tomcat 配置,请告诉我。
谢谢,
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 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,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的解决方案相当合理:将消息附加到内部队列,然后让某些后台任务处理它们。
以下是一些可能有用的提示:
BlockingQueue
队列的实现。在后台线程中,只需在take()
从队列中获取消息时执行无限循环即可。这些类会为您解决潜在的并发问题。ServletContextListener
在 Web 应用程序启动和停止时设置后台线程。使用原始
BlockingQueue
可能出现的一个问题是,当您的 Web 应用程序停止时,队列中的所有消息都会丢失。如果这是一个严重的问题,那么最简单的方法可能是使用数据库作为队列,并使用notify()
唤醒后台线程,然后后台线程处理来自数据库的所有请求。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:
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.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 usenotify()
to wake up your background thread, which then processes all requests from the database.