Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] jetty-9.1 -- ServletOutputStream.isReady() -- how much data can be written without blocking?


On Wed, Sep 11, 2013 at 10:48 PM, Richard Johnson
<johnson.richard78@xxxxxxxxx> wrote:
> Hi everyone,
>   I was looking into the Servlet-3.1 implementation for truly non-blocking
> IO.
>   Found this article:
> by Simone
> Bordet.

Credit where is due, Greg Wilkins wrote it.

>   The Servlet-3.1 ServletOutputStream provides simple boolean isReady()
> method to find out is the stream
>   is ready to receive some data to write to the client, but it is unclear
> how much data can be written without blocking.

You cannot know.

>   I.e. if we do large enough write in subsequent out.writ(buffer, 0, len),
> it would block? Or not?

It would not block (otherwise would not be async IO), but you cannot
know how much has been written.
If you do:

out.write(buffer, 0, len);
if (out.isReady())
    // then here you know the whole len has been written.
    // not completely written, must bail out, the container will call us again

You have to call out.isReady() again to know if it's been written, but
beware that it schedules another call to onWritePossible().

IMHO it's a very complex API, and veeery easy to get wrong, not for
the average developer.
I foresee frameworks will born to offer a simpler API to hide that of
Servlet 3.1.
It can take one half day to write a simple echo servlet right.

You can have a look at some example implementations:

And for a more complex one, real case one:

Simone Bordet
Developer advice, training, services and support
from the Jetty & CometD experts.
Intalio, the modern way to build business applications.

Back to the top