I'm new to NIO, and I am trying to figure out how Jetty leverages NIO.
My understanding of how traditional servlet containers that use Blocking IO service a request is as follows:
- A request arrives
- A thread is allocated to process the request and the servlet method (
doGetetc) is invoked - Servlet method is handed an
InputStreamandOutputStream - The servlet method reads from the
InputStreamand writes to theOutputStream - The
InputStreamandOutputStreamare basically tied to the respective streams of the underlyingSocket
What is different when an NIO connector is used? My guess is along the following lines:
- A request arrives
- Jetty uses NIO connector and buffers the entire request asynchronously
- Once request has been read completely wrap the buffer in an
InputStream - Create an empty response buffer (wrapped in an
OutputStream) - Allocate a thread and invoke the servlet method (
doGetetc) handing the above wrapper streams - Servlet method writes to the wrapped (buffered) response stream and returns from the servlet method
- Jetty uses NIO to write the response buffer contents to the underlying
SocketChannel
From the Jetty documentation, I found the following:
SelectChannelConnector - This connector uses efficient NIO buffers with a non-blocking threading model. Jetty uses Direct NIO buffers, and allocates threads only to connections with requests. Synchronization simulates blocking for the servlet API, and any unflushed content at the end of request handling is written asynchronously.
I'm not sure I understand what Synchronization simulates blocking for the servlet API means?