简体   繁体   English

是从Tomcat 6 CometProcessor内部进行的非阻塞写入

[英]Are writes from within a Tomcat 6 CometProcessor non-blocking

I have a CometProcessor implementation that is effectively doing a Multicast to a potentially large number of clients. 我有一个CometProcessor实现,可以有效地对潜在的大量客户端进行多播。 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. 当需要传播到所有客户端的事件发生时,CometProcessor将需要遍历客户端列表以写出响应。 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: 来自CometProcessor的写入被阻止:

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

See the table at the bottom of the page. 请参阅页面底部的表格。

Tomcat6's implementation of HttpServlerResponse is the Response class. Tomcat6的HttpServlerResponse实现是Response类。 Internally it uses a CoyoteOutputStream wrapped around an OutputBuffer. 在内部,它使用包装在OutputBuffer周围的CoyoteOutputStream。 As the name suggests, this class is a buffer, default size 8k. 顾名思义,此类是一个缓冲区,默认大小为8k。 So I would say at the very least if you are writing less than 8k then you arent going to block. 因此,我至少要说的是,如果您编写的内容少于8k,那么您将无法进行阻塞。 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 http://tomcat.apache.org/tomcat-6.0-doc/config/http.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM