public class TestServlet 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) {

. . .

}

}

}

ReadListener

WriteListener

ServletInputStream.setReadListener

ServletOutputStream.setWriteListener

doGet

AsyncContext context = request.startAsync();

ServletInputStream input = request.getInputStream();

input.setReadListener(new MyReadListener(input, context));



setXXXListener

ReadListener

ServletIntputStream

WriteListener

ServletOutputStream

ServletInputStream.isReady

ServletInputStream.isFinished

ServletOutputStream.canWrite

MyReadListener

@Override

public void onDataAvailable() {

try {

StringBuilder sb = new StringBuilder();

int len = -1;

byte b[] = new byte[1024];

while (input.isReady()

&& (len = input.read(b)) != -1) {

String data = new String(b, 0, len);

System.out.println("--> " + data);

}

} catch (IOException ex) {

Logger.getLogger(MyReadListener.class.getName()).log(Level.SEVERE, null, ex);

}

}



@Override

public void onAllDataRead() {

System.out.println("onAllDataRead");

context.complete();

}



@Override

public void onError(Throwable t) {

t.printStackTrace();

context.complete();

}



onDataAvailable callback method is called

whenever data can be read without blocking





callback method is called whenever data can be read without blocking onAllDataRead callback method is invoked data for

the current request is completely read.

callback method is invoked data for the current request is completely read. onError callback is invoked if there is an error

processing the request.





context.complete()

onAllDataRead

onError

doGet

service

ReadListener

ReadListener

Servlet 3.0 allowed asynchronous request processing but onlytraditional I/O was permitted. This can restrict scalability of yourapplications. In a typical application, ServletInputStream is readin a while loop.If the incoming data is blocking or streamed slower than the servercan read then the server thread is waiting for that data. The samecan happen if the data is written to ServletOutputStream.This is resolved in Servet 3.1 ( href="http://jcp.org/en/jsr/detail?id=340">JSR 340, to bereleased as part Java EE 7) by adding href="http://docs.oracle.com/javase/7/docs/api/java/util/EventListener.html">eventlisteners -andinterfaces. These are then registered usingand. Thelisteners have callback methods that are invoked when the content isavailable to be read or can be written without blocking.The updatedin our case will look like:Invokingmethods indicate thatnon-blocking I/O is used instead of the traditional I/O. At most onecan be registered onand similarly at most onecan beregistered onandare new methods tocheck the status of non-blocking I/O read.is a new method to check if data can be written without blocking.implementation looks like:This implementation has three callbacks:Notice,is called inandto signal the completion of data read.For now, the first chunk of available data need to be read in theormethod of the Servlet. Rest of the data canbe read in a non-blocking way usingafterthat. This is going to get cleaned up where all data read can happeninonly.The sample explained above can be downloaded href="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/File/5eb844250bb11b6092f7cdd3c559ae50/totd188_nonblocking.zip">fromhere and works with href="http://dlc.sun.com.edgesuite.net/glassfish/4.0/promoted/glassfish-4.0-b64.zip">GlassFish4.0 build 64 and href="http://dlc.sun.com.edgesuite.net/glassfish/4.0/promoted/">onwards.The slides and a complete re-run of href="https://oracleus.activeevents.com/connect/sessionDetail.ww?SESSION_ID=6793">What'snew in Servlet 3.1: An Overview session at JavaOne isavailable href="https://oracleus.activeevents.com/connect/sessionDetail.ww?SESSION_ID=6793">here.Here are some more references for you: