使用ExecutorService。
雖然有幾件事你應該做,將線程標記爲守護程序線程,這樣它至少不會在錯誤場景中綁定tomcat,並且當你的servlet上下文被銷燬時(例如,當你重新部署時或停止您的應用程序要做到這一點,使用的ServletContextListener:
public class ExecutorContextListener implements ServletContextListener {
private ExecutorService executor;
public void contextInitialized(ServletContextEvent arg0) {
ServletContext context = arg0.getServletContext();
int nr_executors = 1;
ThreadFactory daemonFactory = new DaemonThreadFactory();
try {
nr_executors = Integer.parseInt(context.getInitParameter("nr-executors"));
} catch (NumberFormatException ignore) {}
if(nr_executors <= 1) {
executor = Executors.newSingleThreadExecutor(daemonFactory);
} else {
executor = Executors.newFixedThreadPool(nr_executors,daemonFactory);
}
context.setAttribute("MY_EXECUTOR", executor);
}
public void contextDestroyed(ServletContextEvent arg0) {
ServletContext context = arg0.getServletContext();
executor.shutdownNow(); // or process/wait until all pending jobs are done
}
}
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
/**
* Hands out threads from the wrapped threadfactory with setDeamon(true), so the
* threads won't keep the JVM alive when it should otherwise exit.
*/
public class DaemonThreadFactory implements ThreadFactory {
private final ThreadFactory factory;
/**
* Construct a ThreadFactory with setDeamon(true) using
* Executors.defaultThreadFactory()
*/
public DaemonThreadFactory() {
this(Executors.defaultThreadFactory());
}
/**
* Construct a ThreadFactory with setDeamon(true) wrapping the given factory
*
* @param thread
* factory to wrap
*/
public DaemonThreadFactory(ThreadFactory factory) {
if (factory == null)
throw new NullPointerException("factory cannot be null");
this.factory = factory;
}
public Thread newThread(Runnable r) {
final Thread t = factory.newThread(r);
t.setDaemon(true);
return t;
}
}
你必須上下文監聽器添加到你的web.xml,在這裏你還可以指定你想運行的線程數量後臺作業:
<listener>
<listener-class>com.example.ExecutorContextListener</listener-class>
</listener>
您可以從你的servlet訪問執行程序和作業提交到它:
ExecutorService executor = (ExecutorService)getServletContext().getAttribute("MY_EXECUTOR");
...
executor.submit(myJob);
如果你使用Spring,這一切大概可以還送simpler
來源
2011-02-05 16:24:58
nos
你期待給回一個響應?此外,Tomcat使用線程池,可以在必要時擴展以服務多個同時請求。 – 2011-02-05 14:59:37
您是否需要將「數據挖掘請求」的結果顯示回給用戶(或者至少通知他們已完成的作業)?你如何做到這一點? – 2011-02-05 15:02:30
我在想,當一份工作完成後,用戶可以通過電子郵件發送結果。 – 2011-02-05 15:40:28