并在Http11Processor的process方法的最后一部分,把文件内容以FileChannel的形式写回到前台,不需要先把文件内容先读到用户内存->压缩->写回socket内核内存。
/**
* Process pipelined HTTP requests using the specified input and output
* streams.
*
* @throws IOException error during an I/O operation
*/
public SocketState process(NioChannel socket)
throws IOException {
......
// Do sendfile as needed: add socket to sendfile and end
if (sendfileData != null && !error) {
KeyAttachment ka = (KeyAttachment)socket.getAttachment(false);
ka.setSendfileData(sendfileData);
sendfileData.keepAlive = keepAlive;
SelectionKey key = socket.getIOChannel().keyFor(socket.getPoller().getSelector());
//do the first write on this thread, might as well
openSocket = socket.getPoller().processSendfile(key,ka,true,true);
break;
}
......
}
这种NIO底层读写channel的形式避免了读取到用户内存的开销,也可以提升性能。
目前,尚不清楚使用NIO快,还是gzip较快,有待测试。
如果在使用NIO的同时还一定要用gzip,可以关闭NIO Connector的useSendFile选项。
<Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol"
connectionTimeout="20000"
redirectPort="8443"
useSendfile="false"
compression="on"
compressionMinSize="2048"
noCompressionUserAgents="gozilla, traviata"
compressableMimeType="text/html,text/xml,text/Javascript" />
参考:http://tomcat.apache.org/tomcat-6.0-doc/config/http.html
