tomcat何时写回响应数据报的详析

2019-10-18 20:08:56丽君

疑问的产生

这个疑问是我在写文件下载的时候产生的,我是用HttpServletResponse获取到Outputstream,然后利用OutputStream直接写数据的。当时我就想这个OutputStream是不是就是对应的Socket连接的OutputStream。即是不是的程序在用stream写的时候,数据也同时在发?

Response的OutputStream把数据写到哪去?

于是我看了下HttpServletResponse的getOutputStream方法,看看它注释是怎么说的。

/**
  * Returns a {@link ServletOutputStream} suitable for writing binary 
  * data in the response. The servlet container does not encode the
  * binary data. 
  *
  * <p> Calling flush() on the ServletOutputStream commits the response.
  *
  * Either this method or {@link #getWriter} may 
  * be called to write the body, not both, except when {@link #reset}
  * has been called.
  *
  * @return a {@link ServletOutputStream} for writing binary data 
  *
  * @exception IllegalStateException if the <code>getWriter</code> method
  * has been called on this response
  *
  * @exception IOException if an input or output exception occurred
  *
  * @see #getWriter
  * @see #reset
  */
 public ServletOutputStream getOutputStream() throws IOException;

以上,注释有说明是OutputStream是用来写响应body内容的,也有提到flush()方法,说明肯定是有缓冲的,所以应该不是直接操作socket写数据。我猜测应该是有一个字节数组用来暂时存储,然后统一flush。但是还是不太确定,于是简单翻阅了下tomcat源码。

找到ServletOutputStream的实现类CoyoteOuputStream。它实现了OutputStream的抽象方法write,把数据写入到OutputBuffer类型的字段中存着。而这个OutputBuffer对象来自于coyote/Response。其实这个OutputBuffer也只是一个接口,具体实现一直向下翻是StreamOutputBuffer。数据大小没有限制,是用链表存储的,每个链表节点存储8196字节。

什么时候把响应数据报返回给客户端?

其实就是查看,它是何时调用OutputBuffer的flush方法的。我逐层查看,最终定位到了connector/Response的finishResponse()方法。这个方法,会先发送响应行和响应头。然后再发送响应body。Tomcat的源码我看的不多,这里找到一张不错的时序图,描述的是一个HTTP请求的处理过程。如下,我们把重点放在servlet的service方法调用,和Response的fininshResponse方法调用上。可以得到,在service方法返回后,执行的就是finishResponse操作。也就是说,当servlet程序处理完这个请求后,tomcat就会把响应结果发回客户端