tomcat 6 thread pool for asynchronous processing

2019-01-22 00:29发布

问题:

Short question: Within a Tomcat 6 app - how can I run a (separate) thread-pool?

What is the best solution for running a thread pool?

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.

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.

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?

回答1:

Answering your short question:

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.

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.

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

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

.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. 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. 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);;

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...



回答2:

For your use case, you can utilize Timer and TimerTask available in java platform to execute a background task periodically.

import java.util.Timer;
import java.util.TimerTask;

TimerTask dbTask = new TimerTask() {
    @Override
    public void run() {
        // Perform Db call and do some action
    }
};

final long INTERVAL = 1000 * 60 * 5; // five minute interval

// Run the task at fixed interval
new Timer(true).scheduleAtFixedRate(dbTask, 0, INTERVAL);

Note, if a task takes more than five minutes to finish, subsequent tasks will not be executed in parallel. Instead they will wait in the queue and will be executed rapidly one after another when the previous one finishes.

You can wrap this code in a singleton class and invoke from the startup servlet.

Generally, its a good practice to perform these kinds of periodic background jobs outside of Servlet containers as they should be efficiently used serve HTTP requests.



回答3:

For a simple background-task, you don't need any kind of thread pool at all. All you need to do is:

  1. Launch a thread
  2. Check to see if the background process should stop
  3. Have it poll the database
  4. Store freshly-polled data somewhere accessible
  5. Check to see if the background process should stop
  6. Go to sleep
  7. Repeat steps #2-#7

Write a ServletContextListener that launches a thread to perform the above steps that you define in a class that implements Runnable. In the contextDestroyed method, set a flag that triggers the checks indicated above in #2 and #5 and then calls Thread.interrupt so your thread terminates.

Your background task should definitely not try to synchronously send messages to clients. Instead, notify waiting pollers using some other mechanism like Object.notify on a monitor (which doesn't really make any sense since you don't want to block clients checking the current status), update a timestamp of some kind, or just have the polling-clients check the current data available in #4.