views:

1132

answers:

1

I have a CometProcessor implementation that is effectively doing a Multicast to a potentially large number of clients. When an event occurs that needs propagated to to all the clients, the CometProcessor will need to loop through the list of clients writing out the response. If writing responses block then there is the possibility that potentially slow clients could have an adverse effect on the distribution of the event. Example:

public class MyCometProcessor implements CometProcessor {
    private List<Event> connections = new ArrayList<Event>();
    public void onEvent(byte[] someInfo) {
        synchronized (connections) {
            for (Event e : connections) {
                HttpServletResponse r = e.getHttpResponse();

                // -- Does this line block while waiting for I/O --
                r.getOutputStream().write(someInfo);
            }
        }
    }

    public void event(CometEvent event) {
        switch (event.getEventType()) {
        case READ:
            synchronzied (connections) {
                connections.add(event);
            }
            break;
        // ...
        }

    }
}

Update: Answering my own question. Writes from a CometProcessor are blocking:

http://tomcat.apache.org/tomcat-6.0-doc/config/http.html

See the table at the bottom of the page.

+1  A: 

Tomcat6's implementation of HttpServlerResponse is the Response class. Internally it uses a CoyoteOutputStream wrapped around an OutputBuffer. As the name suggests, this class is a buffer, default size 8k. So I would say at the very least if you are writing less than 8k then you arent going to block. You may need to flush though for your clients to see the data which means that ultimately it depends on which connector variant you are using. In your Connector config if you want non-blocking writes then specify

protocol=org.apache.coyote.http11.Http11NioProtocol

This Connector/Protocol is massively configurable:

http://tomcat.apache.org/tomcat-6.0-doc/config/http.html

Craig