tomcat 6线程池用于异步处理 [英] tomcat 6 thread pool for asynchronous processing

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

问题描述

简短问题:在Tomcat 6应用程序中 - 如何运行(单独的)线程池?

什么是运行线程池的最佳解决方案吗?

长问题:

我这里有一个简单的需求;

轮询数据库中的某些数据,同时允许Web客户端等待答复(长轮询连接)。

当数据在数据库中可用时,我会发送回复给相关客户。

Long question:
I have a simple need here;
Poll a database for some data, while allowing web clients wait for an answer (long poll connections).
When this data is available at the database I'll send a reply to the relevant client.

这样说,我更喜欢目前不要潜入任何框架( quartz scheduler 也许?)。

Saying so, I prefer not to dive into any framework at the moment (quartz scheduler maybe?).

因此,正如我总结的那样,我需要一个线程池来在后台完成这项工作。

Therefore, as I conclude, I'll need a thread pool to do the job in the background.

所以如果 Thread 就是我要使用的(实际上是 Runnable ),哪个类可以组织它?是否有一种 ThreadPool 解决方案?任何建议?

So if Thread is what I'm about to use (actually Runnable), which class can organizes it all? Is there sort of a ThreadPool solution? Any recommendation?

推荐答案

回答你的简短问题:

在JVM中线程池在 java.util.concurrent.ExecutorService 接口后面抽象。这个接口有不同的实现,但在大多数情况下这个接口的方法就足够了。

In JVM thread pools are abstracted behind java.util.concurrent.ExecutorService interface. There are different implementations of this interface but in most cases methods of this interface would suffice.

要创建特定的线程池,请看一下 java .util.concurrent.Executors class:
http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/Executors.html
包含静态工厂方法创建 ExecutorService 接口的不同实现。可能感兴趣: newFixedThreadPool(int threadsNumber) newCachedThreadPool methods。

To create specific thread pool, take a look at java.util.concurrent.Executors class: http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/Executors.html which contains static factory methods for creating different implementations of ExecutorService interface. You may be interested in newFixedThreadPool(int threadsNumber) and newCachedThreadPool methods.

有关JVM中 Executors 的更多常规信息,您可能需要阅读以下Oracle教程: http://docs.oracle.com/javase/tutorial/essential/concurrency/executors.html

For more general information on Executors in JVM you may want to read following Oracle's tutorial: http://docs.oracle.com/javase/tutorial/essential/concurrency/executors.html

因此,要在Tomcat下使用线程池( ExecutorService ),您应该执行以下操作:

So, to use thread pool (ExecutorService) under Tomcat you should do the following:

.1。创建并注册 web.xml javax.servlet.ServletContextListener 接口的实例(这将作为入口点你的网络应用程序)如果还没有完成。

.1. Create and register in web.xml instance of javax.servlet.ServletContextListener interface (which would act like an entry point to your webapplication) if it's not done yet.

.2。在 contextInitialized(ServletContextEvent)方法中,您创建 ExecutorService 的实例(线程池)并将其存储在<$ c $中c> ServletContext 属性映射,以便可以从webapp中的任何位置访问它,例如:

.2. In contextInitialized(ServletContextEvent) method, you create instance of ExecutorService (thread pool) and store it in ServletContext attribute map, so that it can be accessed from any point in you webapp e.g.:

// following method is invoked one time, when you web application starts (is deployed)
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
    // ...
    final int numberOfThreads = ...;
    final ExecutorService threadPool = Executors.newFixedThreadPool(numberOfThreads); // starts thread pool
    final ServletContext servletContext = servletContextEvent.getServletContext();
    servletContext.setAttribute("threadPoolAlias", threadPool);
    // ...
}

// following method is invoked one time when your web application stops (is undeployed)
public void contextDestroyed(ServletContextEvent servletContextEvent) {
    // following code is just to free resources occupied by thread pool when web application is undeployed
    final ExecutorService threadPool = (ExecutorService) servletContextEvent.getServletContext().getAttribute("threadPoolAlias");
    threadPool.shutdown();
}

.3。在 Servlet.service 方法的某个地方或你的webapp中的任何地方(你应该能够从几乎任何地方获得对 ServletContext 的引用webapp):

.3. Somewhere in Servlet.service method or anywhere in your webapp (you should be able to obtain reference to ServletContext almost anywhere from webapp):

Callable<ResultOfMyTask> callable = new Callable<ResultOfMyTask>() {
    public ResultOfMyTask call() {
        // here goes your task code which is to be invoked by thread pool 
    }
};

final ServletContext servletContext = ...;
final ExecutorService threadPool = (ExecutorService) servletContext.getAttribute("threadPoolAlias");
final Future<ResultOfMyTask> myTask = threadPool.submit(callable);;

您应该存储对myTask的引用,并可以从其他线程查询它以查明它是否已完成以及是结果。

You should store reference to myTask and can query it from other threads to find out whether it's finished and what is the result.

希望这会有所帮助......

Hope this helps...

这篇关于tomcat 6线程池用于异步处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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