Non-Blocking I/O

Java logo 

Agenda

  • Non-Blocking I/O
  • Servlet 3.1 Non-Blocking I/O support
  • Hands-on example

What is Non-Blocking I/O?

Threads not waiting for I/O operations to finish

Non-Blocking I/O in Java

  • Introduced in 1.4
  • Enhanced in java 1.7
  • Netty - a non-blocking I/O client-server framework

Servlet 3.0 vs 3.1

With traditional IO if streamed data is slower than the server can read then the server thread is waiting for that data


                public class BlockingIoServlet extends HttpServlet {
                    protected void doGet(HttpServletRequest request, HttpServletResponse response)
                         throws IOException, ServletException {     
                 ServletInputStream input = request.getInputStream();
                       byte[] b = new byte[1024];
                       int len = -1;
                       while ((len = input.read(b)) != -1) {
                          . . . 
                       }
                   }
                }
                

Servlet 3.1 Non-Blocking I/O support

Servlet 3.1 enhancements

  • Ability to register EventListeners
  • Ability to check Input/Output Stream readiness
  • Ability to check Input/Output Stream compleetnes

Non-Blocking reading

ServletInputStream has the ability to regiser a ReadListener


                AsyncContext context = request.startAsync();
                ServletInputStream input = request.getInputStream();
                input.setReadListener(new MyReadListener(input, context));
              

Non-Blocking writing

ServletOutputStream has the ability to regiser a WriteListener


                AsyncContext context = request.startAsync();
                ServletOutputStream responeOutput = response.getOutputStream();

                responeOutput.setWriteListener(new WriteListener() { 
                    @Override
                    public void onWritePossible() throws IOException {
                      while (responeOutput.isReady()) { 
                        // write
                      }
                    }
                    @Override
                    public void onError(Throwable t) { 
                      // Error handling
                      context.complete();
                    }
                  });
              
              

Lab

Install: visualvm, curl