Asynchronous writes

When APR or NIO is enabled, 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 toBoolean.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).

  • apache.tomcat.sendfile.filename: Canonical filename of the file which will be sent as a String
  • apache.tomcat.sendfile.start: Start offset as a Long
  • 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.

Share this post
[social_warfare]
Comet support
Additional Components

Get industry recognized certification – Contact us

keyboard_arrow_up