Tomcat的背景线程 [英] tomcat background threads

查看:101
本文介绍了Tomcat的背景线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个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.

谢谢.

推荐答案

  1. 在控制器/servlet的初始化方法中使用java.util.concurrent.Executors.newCachedThreadPool(或其他工厂方法之一)创建一个Executor.
  2. 收到请求后,将邮件发送逻辑包装在java.lang.Runnable
  3. Runnable提交给Executor
  1. Create an Executor using java.util.concurrent.Executors.newCachedThreadPool (or one of the other factory methods) in your controller/servlet's initialization method.
  2. When a request comes in, wrap the mail-sending logic in a java.lang.Runnable
  3. Submit the Runnable to the Executor

这将在后台执行发送.记住在启动时创建一个执行器,并在所有请求中共享;不要每次都创建一个新的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆