关于servlet3.0中的异步servlet

Posted 梦中彩虹

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于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;

    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response) throws ServletException, IOException {
        long startTime = System.currentTimeMillis();
        System.out.println("LongRunningServlet Start::Name="
                + Thread.currentThread().getName() + "::ID="
                + Thread.currentThread().getId());
        int milliseconds = 10000;
        longProcessing(milliseconds);

        PrintWriter out = response.getWriter();
        long endTime = System.currentTimeMillis();
        out.write("Processing done for " + milliseconds + " milliseconds!!");
        System.out.println("LongRunningServlet Start::Name="
                + Thread.currentThread().getName() + "::ID="
                + Thread.currentThread().getId() + "::Time Taken="
                + (endTime - startTime) + " ms.");
        out.flush();
        out.close();
    }

    private void longProcessing(int secs) {
        try {
            Thread.sleep(secs);
        } catch (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("org.apache.catalina.ASYNC_SUPPORTED", true);
        int secs = 10000;
        AsyncContext asyncCtx = request.startAsync();
        asyncCtx.addListener(new AppAsyncListener());
        asyncCtx.setTimeout(2000000);
        ThreadPoolExecutor executor = (ThreadPoolExecutor) request.getServletContext().getAttribute("executor");
        executor.execute(new AsyncRequestProcessor(asyncCtx, secs));
        long endTime = System.currentTimeMillis();
        System.out.println("AsyncLongRunningServlet End::Name="
                + Thread.currentThread().getName() + "::ID="
                + Thread.currentThread().getId() + "::Time Taken="
                + (endTime - startTime) + " ms.");
    }
}

 

异步的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;

    public AsyncRequestProcessor() {

    }

    public AsyncRequestProcessor(AsyncContext asyncContext, int milliseconds) {
        this.asyncContext = asyncContext;
        this.milliseconds = milliseconds;
    }

    @Override
    public void run() {
        System.out.println("Async Supported? "
                + asyncContext.getRequest().isAsyncSupported());
        longProcessing(milliseconds);
        try {
            PrintWriter out = asyncContext.getResponse().getWriter();
            out.write("Processing done for " + milliseconds + " milliseconds!!");
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        asyncContext.complete();
    }

    private void longProcessing(int secs) {
        // wait for given time before finishing
        try {
            Thread.sleep(secs);
        } catch (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
    public void contextDestroyed(ServletContextEvent sce) {
        ThreadPoolExecutor executor = (ThreadPoolExecutor) sce
                .getServletContext().getAttribute("executor");
        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
    public void onTimeout(AsyncEvent event) throws IOException {
        ServletResponse response = event.getAsyncContext().getResponse();
        PrintWriter out = response.getWriter();
        out.write("TimeOut Error in Processing");
        out.flush();
        out.close();

    }

    @Override
    public void onError(AsyncEvent event) throws IOException {
        System.out.println("AppAsyncListener error");
        ServletResponse response = event.getAsyncContext().getResponse();
        PrintWriter out = response.getWriter();
        out.write("error on processing");
        out.flush();
        out.close();
    }

    @Override
    public void onStartAsync(AsyncEvent event) throws IOException {
        System.out.println("AppAsyncListener start");
    }
}

 

启动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的这个机制,在之后的文章里总结一下

 

以上是关于关于servlet3.0中的异步servlet的主要内容,如果未能解决你的问题,请参考以下文章

servlet3.0异步,前台页面怎么支持,怎么获取异步输出内容

深入理解Servlet3.0异步请求

23spring注解学习——servlet3.0异步和spring的异步

Servlet3.0对异步处理的支持

Servlet3.0的异步

servlet3.0 新特性——异步处理