code4kix code4kix - 2 months ago 31
Java Question

Java - Async in Servlet 3.0 vs NIO in Servlet 3.1

Until now, as it applies to serving http requests, I thought the terms -

non-blocking i/o
meant the same thing. But apparently, they have been implemented separately in servlet 3.0 and 3.1 respectively. I am struggling to understand the difference here...

Can someone shed more light on this topic, please? Specifically, I am looking for an example of how a servlet 3.0 implementation of a server can be async, yet block on a thread? I think may be if I understand this, it may be easier to understand the exact problem that the non-blocking i/o in servlet 3.1 is trying to solve.


I will try to summarize what I learned. To understand the problem that Servlet 3.0 and Servlet 3.1 solve, let's look at it this way:

Prior to Servlet 3.0:
Long running threads lead to thread starvation. Prior to Servlet 3.0, there were container specific solution for these long running threads where we can spawn a separate worker thread to do the heavy task and then return the response to client. The servlet thread returns to the servlet pool after starting the worker thread. Tomcat’s Comet, WebLogic’s FutureResponseServlet and WebSphere’s Asynchronous Request Dispatcher are some of the example of implementation of asynchronous processing.
(See link 1 for more info.)

Servlet 3.0 Async:
The actual work can be delegated to a thread pool implementation (independent of the container specific solutions). The Runnable implementation will perform the actual processing and will use the AsyncContext to either dispatch the request to another resource or write the response. We can also add AsyncListener implementation to the AsyncContext object to implement callback methods.
(See link 1 for more info.)

Servlet 3.1 NIO:
Servlet 3.0 allowed asynchronous request processing but only traditional I/O was permitted. How does traditional I/O affect processing? Well, if the data coming into the server (i/o) is blocking or streamed slower than the server can read, then the server thread is waiting for that data. On the other hand, if the data is written to ServletOutputStream is slow, the client has to wait. This problem is solved by ReadListener and WriteListener interfaces. These are registered in ServletInputStream and ServletOutputStream. The listeners have callback methods that are invoked when the content is available to be read or can be written without blocking.
(See link 2 for more info.)