Tomcat 6 线程安全电子邮件队列 (javax.mail.*)

发布于 2024-08-26 22:51:05 字数 179 浏览 9 评论 0原文

您好,我有设计/架构问题。我想从我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执手闯天涯 2024-09-02 22:51:05

您的解决方案相当合理:将消息附加到内部队列,然后让某些后台任务处理它们。

以下是一些可能有用的提示:

使用原始 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:

  • 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 while take()-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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文