tomcat后台线程
我有一个正在运行的 tomcat 6.20 实例,并且想通过后台线程发送电子邮件以防止电子邮件发送功能阻止请求。
有什么方法可以在后台执行线程,同时仍然允许正常的页面流发生。
该应用程序是用 ICEfaces 编写的。
谢谢。
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.
The application is written in ICEfaces.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
java.lang.Runnable
中Runnable
提交给Executor
这将执行在后台发送。 请记住在启动时创建一个 Executor,并在所有请求之间共享; 不要每次都创建一个新的执行器(你可以,但是会有点慢而且浪费)。
Executor
usingjava.util.concurrent.Executors.newCachedThreadPool
(or one of the other factory methods) in your controller/servlet's initialization method.java.lang.Runnable
Runnable
to theExecutor
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).
将您的电子邮件发送代替
Thread.sleep()
。 将您的输出替换为sendRedirect()
。Put your email sending in place of
Thread.sleep()
. Put your output in place ofsendRedirect()
.我已经找到出路了。 这些标记
和
在您的 servlet 中创建 2 个方法,它们返回 void 且不接受任何参数。
将第一个标签放在第一个方法的正上方,将第二个标签放在第二个标签的上方。
标签的本质
@PostConstruct 方法在实现类开始响应 Web 服务客户端之前由容器调用。
@PreDestroy 方法在端点从操作中删除之前由容器调用。
在 PostConstruction() 方法内部,使用 runnable 接口创建线程,并让它在无限循环中运行,除非某个布尔变量的值为 false。
使用 PreDestroy() 方法将布尔变量设置为 false。
I have found a way out. These tags
and
Create 2 methods in your servlet that return void and accept no parameters.
place the 1st tag immediately above the first method and the 2nd tag above second tag.
Essense of the Tags
The @PostConstruct method is called by the container before the implementing class begins responding to web service clients.
The @PreDestroy method is called by the container before the endpoint is removed from operation.
inside the PostConstruction() method create your thread using the runnable interface and have it run in an infinite loop unless the value of a certain boolean variable is false.
use the PreDestroy() method to set the boolean variable to false.