Advanced IO and Tomcat
Table of Contents
Introduction
IMPORTANT NOTE: Usage of these features requires using the HTTP connectors. The AJP connectors do not support them.
Asynchronous writes
When using HTTP connectors Tomcat supports using sendfile to send large
static files. These writes, as soon as the system load increases, will be performed
asynchronously in the most efficient way. Instead of sending a large response using
blocking writes, it is possible to write content to a static file, and write it
using a sendfile code. A caching valve could take advantage of this to cache the
response data in a file rather than store it in memory. Sendfile support is
available if the request attribute org.apache.tomcat.sendfile.support
is set to Boolean.TRUE
.
Any servlet can instruct Tomcat to perform a sendfile call by setting the appropriate request attributes. It is also necessary to correctly set the content length for the response. When using sendfile, it is best to ensure that neither the request or response have been wrapped, since as the response body will be sent later by the connector itself, it cannot be filtered. Other than setting the 3 needed request attributes, the servlet should not send any response data, but it may use any method which will result in modifying the response header (like setting cookies).
org.apache.tomcat.sendfile.filename
: Canonical filename of the file which will be sent as a Stringorg.apache.tomcat.sendfile.start
: Start offset as a Longorg.apache.tomcat.sendfile.end
: End offset as a Long
In addition to setting these parameters it is necessary to set the content-length header. Tomcat will not do that for you, since you may have already written data to the output stream.
Note that the use of sendfile will disable any compression that Tomcat may otherwise have performed on the response.