使用特殊线程发送邮件
我有一个在 tomcat 6.0 上运行的 jsf 应用程序,在应用程序的某个位置我向一些用户发送电子邮件。但是发送邮件比我想象的要慢,它导致这些相关页面之间缺少。
所以我的问题是;这是一种很好的(或可行的)方法来将此过程交给我创建的另一个线程,该线程获取邮件发送请求并将这些请求放入队列中,并在主应用程序之外处理这些请求。因此,邮件发送过程将结束主要流程,不会影响应用程序的速度。
i have a jsf application running on tomcat 6.0 and somewhere in the app i send e mails to some users.But sending mail slower than i thought, it causes lacks beetwen these related pages.
So my question is; is that a good(or doable) a way to give this proccess to another thread which i create, a thread that gets mail sending requests and put these in a queue and proccess these apart from main application.Hence the mail sending proccess would be out of the main flow and doesnt affect the app's speed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这绝对是个好主意。您应该非常小心地进行此操作。这里有一些值得思考的地方:
由于您使用的是 Tomcat,它不支持开箱即用的 EJB(因此
@Asynchronus
@Singleton
是不可能的),我将创建一个应用程序范围的 bean,其中包含ExecutorService
处理邮件任务。这是一个启动示例:这将创建一个线程并将任务放入队列中。您可以在普通 bean 中使用它,如下所示:
要了解有关 java.util.concurrent API 的更多信息,请参阅 官方教程。
Yes, that's definitely a good idea. You should only do it with an extreme care. Here's some food for thought:
As you're using Tomcat, which does not support EJB out the box (and thus
@Asynchronus
@Singleton
is out of question), I'd create an application scoped bean which holds anExecutorService
to process the mail tasks. Here's a kickoff example:This creates a single thread and puts the tasks in a queue. You can use it in normal beans as follows:
To learn more about
java.util.concurrent
API, refer the official tutorial.