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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With