Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WriteListener (servlet 3.1) semantics

Let's say I'm writing chunks of data to an HttpServletResponse. So my method receives an array of bytes, I write it to response.getOutputStream(), and then I release the thread. When I receive another chunk of data, my method will be awaken, and it will write to getOutputStream() again. In the end, I call to AsyncContext.complete().

Now, the WriteListener.onWritePossible() spec says:

this method will be invoked by the container the first time when it is possible to write data. The container will subsequently invoke the onWritePossible method if and only if isReady method on ServletOutputStream, described below, returns false.

It seems that when the container calls this method, I already need to have my response buffered somewhere, because onWritePossible() may never be called again. So, for the previous example, I need to write all chunks of data I receive in a buffer (a byte array, or maybe a temporary file if the buffer is big enough), and if onWritePossible() is called before my response is complete, what should I do? Block the thread until I have all my response data?

like image 675
Luciano Avatar asked Jun 13 '13 12:06

Luciano


1 Answers

Check code in following link. May not be the exact solution but it's idea can be a solution for your case: Servlet 3.1 Async IO Echo

like image 144
hyda Avatar answered Nov 05 '22 11:11

hyda