tomcat处理请求流程
从客户端发出一次请求至tomcat容器大致经过如下过程:
- 客户端发出http请求至tomcat的连接监听端口;
- tomcat connector接收线程接收请求,并根据http协议解析该次请求;
- tomcat 通过解析的http报文,初始化org.apache.coyote.Request,并实例化org.apache.coyote.Response;
- 经装饰模式转化为servlet api对应的HttpServletRequest与HttpServletReponse;
- 经tomcat的层层容器engine,host,context最终到过我们所写的业务servlet的service方法;
- 业务方法service,处理相关的业务逻辑,写入相应的响应的至response,并返回tomat的容器组件;
- tomcat该处理线程关闭响应流Response并将响应内容返回客户端;
- tomcat该处理线程被释放,然后用于下次请求的处理;
从中可以看出tomcat服务器在处理请求时在servlet中只有在相应的处理方法执行结束后才会将响应数据发送至客户端并关闭线程释放资源。所以如果在处理请求时如果处理的代码时间过程对当前线程也会一直占用,那么是否存在一种类似于ajax的技术,使当前servlet服务先继续向下执行,然后等请求执行玩动态返回结果,这就是servlet3的异步化,当然现在基本上还是使用ajax来实现请求的异步
从上面tomcat处理请求过程可知, tomcat处理线程一直被占用,直至业务方法处理完毕;那么servlet3是如何进行异步化的呢?使用servlet3的异步的一般姿势:
AsyncContext asyncContext = request.startAsync();//获取异步上下文对象asyncContext.start(new IndexThread(asyncContext));//创建线程执行异步线程asyncContext.complete();异步线程执行完通知主线程执行结束
当然在使用上面的代码之前,不要忘了开启servlet的异步化(使用注解也可以asyncSupported = true):
<servlet><servlet-name>test</servlet-name><servlet-class>***</servlet-class><async-supported>true</async-supported>
</servlet>
代码实例
TestServlet:执行异步化的servlet
import java.io.IOException;
import java.io.PrintWriter;
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;
@WebServlet(urlPatterns = "/test",asyncSupported = true)
public class TestServlet extends HttpServlet {private static final long serialVersionUID = 1L;protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType("text/html;charset=utf-8");try {PrintWriter out = response.getWriter();out.println("servlet started.<br/>");out.flush();AsyncContext asyncContext = request.startAsync();asyncContext.start(new IndexThread(asyncContext));out.println("servlet end.<br/>");out.flush();} catch (Exception e) {e.printStackTrace();}}
}
indexThread:异步执行的线程
import java.io.PrintWriter;
import javax.servlet.AsyncContext;
public class IndexThread implements Runnable{private AsyncContext asyncContext;public IndexThread(AsyncContext asyncContext) {this.asyncContext = asyncContext;}public void run() {try {Thread.sleep(5000);PrintWriter out = asyncContext.getResponse().getWriter();out.println("hello servlet3.<br/>");out.flush();asyncContext.complete();} catch (Exception e) {e.printStackTrace();}}
}