关于servlet3.0中的异步servlet
刚看了一下维基百科上的介绍,servlet3.0是2009年随着JavaEE6.0发布的:
到现在已经有六七年的时间了,在我第一次接触java的时候(2011年),servlet3.0就已经出现很久了,但是到现在,里边的一些东西还是没有能够好好地了解一下
最近在研究java的长连接,在了解jetty中的continuations机制的时候也重新了解了一下servlet3.0中的异步servlet机制,通过看几个博客,加上自己的一些测试,算是搞明白了一些,在这里记录一下:
在服务器的并发请求数量比较大的时候,会产生很多的servlet线程(这些servlet线程在servlet容器的线程池中维护),如果每个请求需要耗费的时间比较长(比如,执行了一些IO的处理等),在之前的非异步的servlet中,这些servlet线程将会阻塞,严重耗费服务器的资源.而在servlet3.0中首次出现的异步servlet,通过一个单独的新的线程来执行这些比较耗时的任务(也可以把这些任务放到一个自己维护的线程池里),servlet线程立即返回servlet容器的servlet池以便响应其他请求,这样,在降低了系统的资源消耗的同时,也会提升系统的吞吐量
下面是我自己做的一个模拟的操作(代码参考了importnew上的这篇文章:http://www.importnew.com/8864.html)
一个服务器端需要十秒才能返回的servlet,分别有同步的版本和异步的版本,通过JMeter做压力测试,配合jprofiler来分析服务器的资源消耗情况(主要是线程的创建和使用情况)来分析当服务器处理时间较长的时候,异步的servlet和同步的servlet对服务器性能的影响
同步的servlet:
package com.jiaoyiping.websample.asyncServlet; /* * Created with Intellij IDEA * USER: 焦一平 * Mail: jiaoyiping@gmail.com * Date: 2016/10/22 * Time: 22:16 * To change this template use File | Settings | Editor | File and Code Templates */import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;@WebServlet(urlPatterns = "/syncServlet")
public class SyncServlet extends HttpServlet {
//线程睡眠十秒才返回的servlet
private static final long serialVersionUID = 1L;</span><span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> doGet(HttpServletRequest request, HttpServletResponse response) </span><span style="color: #0000ff;">throws</span><span style="color: #000000;"> ServletException, IOException { </span><span style="color: #0000ff;">long</span> startTime =<span style="color: #000000;"> System.currentTimeMillis(); System.out.println(</span>"LongRunningServlet Start::Name=" + Thread.currentThread().getName() + "::ID=" +<span style="color: #000000;"> Thread.currentThread().getId()); </span><span style="color: #0000ff;">int</span> milliseconds = 10000<span style="color: #000000;">; longProcessing(milliseconds); PrintWriter out </span>=<span style="color: #000000;"> response.getWriter(); </span><span style="color: #0000ff;">long</span> endTime =<span style="color: #000000;"> System.currentTimeMillis(); out.write(</span>"Processing done for " + milliseconds + " milliseconds!!"<span style="color: #000000;">); System.out.println(</span>"LongRunningServlet Start::Name=" + Thread.currentThread().getName() + "::ID=" + Thread.currentThread().getId() + "::Time Taken=" + (endTime - startTime) + " ms."<span style="color: #000000;">); out.flush(); out.close(); } </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> longProcessing(<span style="color: #0000ff;">int</span><span style="color: #000000;"> secs) { </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> { Thread.sleep(secs); } </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (InterruptedException e) { e.printStackTrace(); } }
}
异步的servlet:
package com.jiaoyiping.websample.asyncServlet.async;import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.ThreadPoolExecutor;/*
Created with Intellij IDEA
USER: 焦一平
Mail: jiaoyiping@gmail.com
Date: 2016/10/22
Time: 22:38
To change this template use File | Settings | Editor | File and Code Templates
*/
@WebServlet(asyncSupported = true, urlPatterns = "/asyncServlet")
public class AsyncServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
long startTime = System.currentTimeMillis();
System.out.println("AsyncLongRunningServlet Start::Name="
+ Thread.currentThread().getName() + "::ID="
+ Thread.currentThread().getId());request.setAttribute(</span>"org.apache.catalina.ASYNC_SUPPORTED", <span style="color: #0000ff;">true</span><span style="color: #000000;">); </span><span style="color: #0000ff;">int</span> secs = 10000<span style="color: #000000;">; AsyncContext asyncCtx </span>=<span style="color: #000000;"> request.startAsync(); asyncCtx.addListener(</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> AppAsyncListener()); asyncCtx.setTimeout(</span>2000000<span style="color: #000000;">); ThreadPoolExecutor executor </span>= (ThreadPoolExecutor) request.getServletContext().getAttribute("executor"<span style="color: #000000;">); executor.execute(</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> AsyncRequestProcessor(asyncCtx, secs)); </span><span style="color: #0000ff;">long</span> endTime =<span style="color: #000000;"> System.currentTimeMillis(); System.out.println(</span>"AsyncLongRunningServlet End::Name=" + Thread.currentThread().getName() + "::ID=" + Thread.currentThread().getId() + "::Time Taken=" + (endTime - startTime) + " ms."<span style="color: #000000;">);
}
}
异步的servlet依赖的处理长时间任务的Thread:
package com.jiaoyiping.websample.asyncServlet.async; /* * Created with Intellij IDEA * USER: 焦一平 * Mail: jiaoyiping@gmail.com * Date: 2016/10/22 * Time: 22:43 * To change this template use File | Settings | Editor | File and Code Templates */import javax.servlet.AsyncContext;
import java.io.IOException;
import java.io.PrintWriter;public class AsyncRequestProcessor implements Runnable {
private AsyncContext asyncContext;
private int milliseconds;</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> AsyncRequestProcessor() { } </span><span style="color: #0000ff;">public</span> AsyncRequestProcessor(AsyncContext asyncContext, <span style="color: #0000ff;">int</span><span style="color: #000000;"> milliseconds) { </span><span style="color: #0000ff;">this</span>.asyncContext =<span style="color: #000000;"> asyncContext; </span><span style="color: #0000ff;">this</span>.milliseconds =<span style="color: #000000;"> milliseconds; } @Override </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> run() { System.out.println(</span>"Async Supported? " +<span style="color: #000000;"> asyncContext.getRequest().isAsyncSupported()); longProcessing(milliseconds); </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> { PrintWriter out </span>=<span style="color: #000000;"> asyncContext.getResponse().getWriter(); out.write(</span>"Processing done for " + milliseconds + " milliseconds!!"<span style="color: #000000;">); out.flush(); out.close(); } </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (IOException e) { e.printStackTrace(); } asyncContext.complete(); } </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> longProcessing(<span style="color: #0000ff;">int</span><span style="color: #000000;"> secs) { </span><span style="color: #008000;">//</span><span style="color: #008000;"> wait for given time before finishing</span> <span style="color: #0000ff;">try</span><span style="color: #000000;"> { Thread.sleep(secs); } </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (InterruptedException e) { e.printStackTrace(); } }
}
异步的servlet依赖的初始化ThreadPoll的Listener(可选的,本例子中使用线程池,如不使用线程池时不需要):
package com.jiaoyiping.websample.asyncServlet.async; /* * Created with Intellij IDEA * USER: 焦一平 * Mail: jiaoyiping@gmail.com * Date: 2016/10/22 * Time: 22:41 * To change this template use File | Settings | Editor | File and Code Templates */import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@WebListener
public class ApplicationListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
ThreadPoolExecutor executor = new ThreadPoolExecutor(50, 100, 50000L,
TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(5000));
sce.getServletContext().setAttribute("executor",
executor);
}@Override </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> contextDestroyed(ServletContextEvent sce) { ThreadPoolExecutor executor </span>=<span style="color: #000000;"> (ThreadPoolExecutor) sce .getServletContext().getAttribute(</span>"executor"<span style="color: #000000;">); executor.shutdown(); }
}
异步servlet中添加的异步监听器(可选的)
package com.jiaoyiping.websample.asyncServlet.async; /* * Created with Intellij IDEA * USER: 焦一平 * Mail: jiaoyiping@gmail.com * Date: 2016/10/22 * Time: 22:48 * To change this template use File | Settings | Editor | File and Code Templates */import javax.servlet.AsyncEvent;
import javax.servlet.AsyncListener;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebListener;
import java.io.IOException;
import java.io.PrintWriter;@WebListener
public class AppAsyncListener implements AsyncListener {
@Override
public void onComplete(AsyncEvent event) throws IOException {
System.out.println("AppAsyncListener complete");
}@Override </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> onTimeout(AsyncEvent event) <span style="color: #0000ff;">throws</span><span style="color: #000000;"> IOException { ServletResponse response </span>=<span style="color: #000000;"> event.getAsyncContext().getResponse(); PrintWriter out </span>=<span style="color: #000000;"> response.getWriter(); out.write(</span>"TimeOut Error in Processing"<span style="color: #000000;">); out.flush(); out.close(); } @Override </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> onError(AsyncEvent event) <span style="color: #0000ff;">throws</span><span style="color: #000000;"> IOException { System.out.println(</span>"AppAsyncListener error"<span style="color: #000000;">); ServletResponse response </span>=<span style="color: #000000;"> event.getAsyncContext().getResponse(); PrintWriter out </span>=<span style="color: #000000;"> response.getWriter(); out.write(</span>"error on processing"<span style="color: #000000;">); out.flush(); out.close(); } @Override </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> onStartAsync(AsyncEvent event) <span style="color: #0000ff;">throws</span><span style="color: #000000;"> IOException { System.out.println(</span>"AppAsyncListener start"<span style="color: #000000;">); }
}
启动tomcat之后,我们jprofile连接到该tomcat
容器启动的时候,线程图是这样的,一共有大概十几个的线程:
线程快照:
配置JMeter的线程组,在十秒钟内启动500个线程:
测试同步的servlet:
在请求同步的servlet时,线程图是这样子的(jvm中的线程数飙升到了203个,绿色的线表明我在那个时刻做了一次线程快照):
线程快照是这样子的(容器中维护的线程数达到了203个):
现在请求异步的servlet:
请求时的线程图:
线程快照:
在对异步的servlet做压力测试时,jvm中的线程数量并没有大量地上升,我们在处理异步的servlet的时候,自己维护了一个线程池,基本上增加的线程都是来自这个线程池,因为使用了异步的servlet,servlet请求会立即返回servlet池,所以,需要servlet容器分配的sevlet线程的数量基本上没有增加多少,系统消耗的线程的数量下降了,对资源的消耗也会下降
值得一提的是,异步的servlet并不会使客户端的访问速度加快,只是提升了服务器端的处理性能,减轻了服务器端的资源消耗,使得服务器端使用比较少的线程就能处理大量的连接,所以这个特性要在合适的场景下使用才可以
另外,异步的servlet的编程模型要比之前的servlet复杂许多,这也是在开发的时候需要注意的,jetty的continuations也提供了类似的功能,并且提供了简化的编程模型,在任何支持servlet3.0的容器里都可以运行(不是只能在jetty中运行),我们项目组开发的针对安卓客户端的消息推送服务器就使用到了jetty的这个机制,在之后的文章里总结一下