jsr340-experts@servlet-spec.java.net

[jsr340-experts] Async IO and Upgrade proposal updated

From: Shing Wai Chan <shing.wai.chan_at_oracle.com>
Date: Thu, 22 Mar 2012 14:27:40 -0700

Based on feedback from EG below are the upgrade and async IO proposal
changes to better align and simplify the proposals -

move the setReadListener from the ServletRequest to the
AsyncIOInputSource with the following signature -

     public void setReadListener(ReadListener readListener);

move the setWriteListener from ServletResponse to AsyncIOOutputSink with
the following signature -

     public void setWriteListener(WriteListener writeListener);

We have also updated the ReadListener callback methods with no
ServletRequest being passed into the methods
Similarly for WriteListener there is no need to pass the ServletResponse
in the callback methods.

In the WriteListener we have modified the onWritePossible method as
follows -

public void onWritePossible(int numOfBytes); -

Note that the onWritePossible(int numOfBytes) may require buffering at
the lower layers closer to where the
actual write happens. An alternate is to add the various write methods
to ServletOutputStream that returns an int with the number
of bytes written just like the WriteableByteChannel's write method.*
*
Below is an example of the usage of the API. I will update the wiki with
the proposal and the spec correspondingly once the
EG agrees to these changes -

package servlet.samples.nio;

import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class NIOServletSample extends HttpServlet {

     public void doGet(HttpServletRequest request, HttpServletResponse
response) throws IOException {
         final ServletInputStream sis = request.getInputStream();
         final ServletOutputStream sos = response.getOutputStream();

         sis.setReadListener(
         (new ReadListener() {

             public void onDataAvailable() {
                 try {
                     sis.read(new byte[sis.dataAvailable()]);
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             }

             public void onAllDataRead() {
                 try {
                     sis.close();
                 } catch (IOException e) {
                     e.printStackTrace();
                 }

             }

             public void onError(Throwable t) {

                 }
         }));
          sos.setWriteListener(new WriteListener() {
              public void onWritePossible(int size) {
              }

              public void onError(Throwable t) {

              }
          });

     }
}


With the changes above -

the upgrade proposal can be further simplified as follows -


we would like to remove the following two methods:
     ProtocolHandler.inputAvailable
     ProtocolHanlder.outputReady
as we can now use the asynchronous IO as defined above directly
in the ProtocolHandler.

Below is a sample of the ProtocolHandler based on the changes.

import javax.servlet.*;
import javax.servlet.http.*;

public class EchoProtocolHandler implements ProtocolHandler {
      public void init(WebConnection wc) {
          wc.getInputStream().setReadListener(new ReadListener() {
             public void onDataAvailable() {
                 try {
                     ServletInputStream sis = wc.getInputStream();
                     byte[] dataBytes = new byte[sis.dataAvailable()];
                     sis.read(dataBytes);
                     wc.getOutputStream().write(dataBytes);
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             }

             public void onAllDataRead() {
                 try {
                     sis.close();
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             }

             public void onError(Throwable t) {

             }
          });
      }
}


Thanks

- Shing Wai and Rajiv