I have a questions regarding the Non Blocking API introduced in Tomcat 8. I'm looking at the TestNonBlockingAPI test as an example, specifically the NBReadServlet class.
@WebServlet(asyncSupported = true)
public class NBReadServlet extends TesterServlet {
private static final long serialVersionUID = 1L;
public volatile TestReadListener listener;
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// step 1 - start async
AsyncContext actx = req.startAsync();
actx.setTimeout(Long.MAX_VALUE);
actx.addListener(new AsyncListener() {
// removed for brevity
});
// step 2 - notify on read
ServletInputStream in = req.getInputStream();
listener = new TestReadListener(actx);
in.setReadListener(listener);
listener.onDataAvailable();
}
}
My question is why does the test call "listener.onDataAvailable()" at the end?
In regards to the "onDataAvailable()" method, Section 3.7 of the spec says…
"The onDataAvailable method is invoked on the ReadListener when data is available to read from the incoming request stream. The container will invoke the method the first time when data is available to read. The container will subsequently invoke the onDataAvailable method if and only if isReady method on ServletInputStream, described below, returns false."
...which leads me to believe that the container should be calling onDataAvailable and not the servlet.
As a side note, my test works if I call "onDataAvailable()" from my test servlet. Otherwise it fails and times out.