Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jetty-users] jetty request streaming

Hello, While experimented with async and non-blocking servlets, I noticed that jetty buffers entire request before processing it in the servlet itself. I wanted to see how to receive large request in the background on the slow connection, without blocking request processing threads, But servlet is invoked only when the entire request has been received and the same thread runs the servlet and read listener. I expected read listener to be called when new data is available on the request's input stream. Code looks like this:
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

  System.out.println(Thread.currentThread().getName());


  final AsyncContext acontext = request.startAsync();
  final ServletInputStream input = request.getInputStream();

  input.setReadListener(new ReadListener() {
    @Override
    public void onDataAvailable() throws IOException {
      byte buffer[] = new byte[4*1024];
      do {
        int length = input.read(buffer);
        System.out.println("received " + length + " at " + Thread.currentThread().getName());
      } while (input.isReady());

    }

    @Override
    public void onAllDataRead() throws IOException {
      acontext.getResponse().getWriter().write("done! " + LocalTime.now().format(DateTimeFormatter.ISO_TIME) + " at " + Thread.currentThread().getName());
      acontext.complete();
    }

    @Override
    public void onError(Throwable t) {
      t.printStackTrace();
    }
  });
}
I tested with the client which writes arbitrary data to HTTP connection stream, in a loop which sleeps on each iteration. Could anyone please explain how is this supposed to work? Is it possible to receive request without buffering it first? Thank you!

View this message in context: jetty request streaming
Sent from the Jetty User mailing list archive at Nabble.com.

Back to the top