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?

Hi,

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:
> http://webtide.intalio.com/2013/07/servlet-3-1-async-io-and-jetty/ 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.
else
    // 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:
https://github.com/jetty-project/jetty-bench/blob/master/bench-9-server/src/main/java/org/eclipse/jetty/benchmark/Servlet31AsyncIOEchoBenchmark.java

And for a more complex one, real case one:
https://github.com/cometd/cometd/blob/master/cometd-java/cometd-java-server/src/main/java/org/cometd/server/transport/AsyncJSONTransport.java

-- 
Simone Bordet
----
http://cometd.org
http://webtide.com
http://intalio.com
Developer advice, training, services and support
from the Jetty & CometD experts.
Intalio, the modern way to build business applications.


Back to the top