我有一种情况需要开始运行4个小时的任务。我正在使用servlet开始这个过程。但是我遇到了内存泄漏异常。
Aug 10, 2016 2:08:05 PM org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/] has started
Aug 10, 2016 2:08:05 PM org.apache.catalina.core.StandardWrapper unload
INFO: Waiting for 1 instance(s) to be deallocated for Servlet [Servlet]
Aug 10, 2016 2:08:06 PM org.apache.catalina.core.StandardWrapper unload
INFO: Waiting for 1 instance(s) to be deallocated for Servlet [Servlet]
Aug 10, 2016 2:08:07 PM org.apache.catalina.core.StandardWrapper unload
INFO: Waiting for 1 instance(s) to be deallocated for Servlet [DayZeroServlet]
Aug 10, 2016 2:08:07 PM org.apache.catalina.loader.WebappClassLoaderBase clearReferencesThreads
WARNING: The web application [xxx#Day0MS] is still processing a request that has yet to finish. This is very likely to create a memory leak. You can control the time allowed for requests to finish by using the unloadDelay attribute of the standard Context implementation. Stack trace of request processing thread:
java.net.SocketOutputStream.socketWrite0(Native Method)
java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
下面是代码,我正在使用
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
if (!GehDAO.getProcessFlag()) {
GehDAO.updateProcessFlag(true);
out.println("Transformation started ...");
closeWriter(out);
execute(); // This step start the long running process
}
else {
out.println("Transformation Already Running ...");
closeWriter(out);
}
}
有没有办法可以在单独的进程中运行execute()方法?
我想在一个月内运行一次或两次这个servlet,以启动长时间运行的进程。
答案 0 :(得分:1)
你不能将execute()
电话打包成一个帖子吗?
new Thread(() -> execute()).start();
或者对于Java8之前的旧版本:
new Thread(new Runnable() {
public void run() { execute(); }
}).start();
一方面注意:确保管理关机。您通常可以使用ServletContextListener执行此操作。基本上,如果上下文被破坏,请在长时间运行的线程上设置一个布尔标志,让它知道它需要停止,因为应用服务器正在等待它完成才能关闭。