Tomcat的背景线程 [英] tomcat background threads
问题描述
我正在运行一个tomcat 6.20实例,并且想通过后台线程发送电子邮件,以防止电子邮件发送功能阻止请求.
I have a tomcat 6.20 instance running, and would like to send an email via a background thread to prevent the email sending function from blocking the request.
有什么方法可以在后台执行线程,同时仍然允许正常的页面流发生.
Is there any way I can execute the thread in the background, while still allowing normal page flow to occur.
该应用程序是用ICEfaces编写的.
The application is written in ICEfaces.
谢谢.
推荐答案
- 在控制器/servlet的初始化方法中使用
java.util.concurrent.Executors.newCachedThreadPool
(或其他工厂方法之一)创建一个Executor
. - 收到请求后,将邮件发送逻辑包装在
java.lang.Runnable
中
- 将
Runnable
提交给Executor
- Create an
Executor
usingjava.util.concurrent.Executors.newCachedThreadPool
(or one of the other factory methods) in your controller/servlet's initialization method. - When a request comes in, wrap the mail-sending logic in a
java.lang.Runnable
- Submit the
Runnable
to theExecutor
这将在后台执行发送.记住在启动时创建一个执行器,并在所有请求中共享;不要每次都创建一个新的Executor(您可以,但这会有点慢且浪费).
This will perform the sending in the background. Remember to create a single Executor at startup, and share across all the requests; don't create a new Executor every time (you could, but it would be a bit slow and wasteful).
这篇关于Tomcat的背景线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!