jsr340-experts@servlet-spec.java.net

[jsr340-experts] Re: Non-blocking changes

From: Mark Thomas <markt_at_apache.org>
Date: Wed, 24 Oct 2012 14:11:26 +0100

On 23/10/2012 00:41, Shing Wai Chan wrote:
> On 10/22/12 3:11 PM, Mark Thomas wrote:
>> On 22/10/2012 22:17, Shing Wai Chan wrote:
>>> Based on the feedback from JavaOne, we would like to propose the
>>> following changes:
>>>
>>> In section 5.3 of Servlet 3.1, we have
>>>
>>> WriteListener
>>> void onWritePossible(). When a WriteListener is registered with the
>>> ServletOutputStream, this method will be invoked by the container if and
>>> only if canWrite method on ServletOutputStream, described below, has
>>> been called and has returned false.
>>>
>>> In other words, one need to write as much data as possible until it is
>>> blocking before the WriteListener#onWritePossible is invoked.
>>> In this case, the developer need to do the following pseudo code:
>>> servletOutputStream.setWriteListener(wlistener);
>>> writeUntilCannotWrite(servletOutputStream); // (a) first write
>>> ...
>>> // (b) more write to WriteListener.onWritePossible later
>>>
>>> It is hard to achieve this in the application code as they need to keep
>>> track of how much data has been written
>>> and make it available to the WriteListener.
>> Why? You write some bytes and check that "number of bytes written" ==
>> "number of bytes passed to write()". I assume I am missing something.
>> What is it?
> With the proposal in EDR, we have the following:
>
> public void service(HttpServletRequest req, HttpServletResponse res)
> throws IOException, ServletException {
>
> ....
> output.setWriteListener(writeListener);
>
> output.write("START\n".getBytes());
> boolean prevCanWrite = true;
>
> while ((prevCanWrite = output.canWrite())) {
> writeData(output); // (a)
> }
> ...
> // then the WriteListener will be invoked later
> }
>
> static class WriteListenerImpl implements WriteListener {
> ....
>
> public void onWritePossible() {
> try {
> writeData(...); // (b)
> } catch(Exception ex) {
> throw new IllegalStateException(ex);
> } finally {
> latch.countDown();
> }
> }
> }
>
> In this case, the writeData(...) in (a) need to keep track of how much
> data has been written and then (b) will continue the writing.

OK. That isn't how I would use the API but if folks want to (mis-)use it
that way then yes, they'll need to keep track of what they have written.

>>> I propose to change the behavior that there is no need to do (a) above.
>>> The WriteListener#onWritePossible will be invoked once for every write
>>> operation permitted.
>> I understood (or at least thought I understood) the previous proposal.
>> This change is not clear to me. Please can you spell out precisely how
>> you envisage that the API will work after this change.

You have not answered this question. We can guess but you really need to
spell out explicitly how this change will operate.

I am concerned that a significant change is being proposed contrary to
the previous consensus reached after long discussion within the expert
group. However, until you are clear about your proposal no-one on the EG
can be clear about any objections they may have.

Mark