荣耀彩票代理

IT技术互动交流平台

Tomcat如何实现Comet

来源:IT165收集  发布日期:2016-12-16 20:35:29

CometMOSHISHIYIZHONGFUWUQIDUANTUIJISHU,TADEHEXINSIXIANGTIGONGYIZHONGNENGRANGDANGFUWUQIDUANWANGKEHUDUANFASONGSHUJUDEFANGSHI。CometMOSHIWEISHENMEHUICHUXIAN?GANGKAISHIRENMENZAIKEHUDUANTONGGUOBUDUANZIDONGSHUAXINZHENGGEYEMIANLAIGENGXINSHUJU,HOULAIJUEDETIYANBUHAOYOUSHIYONGLEAJAXBUDUANCONGKEHUDUANLUNXUNFUWUQIGENGXINSHUJU,RANHOUSHISHIYONGCometMOSHIYOUFUWUQIDUANTONGGUOZHANGLIANJIETUISHUJU。CometMOSHINENGDADAJIANSHAOFASONGDAOFUWUQIDUANDEQINGQIUCONGERBIMIANLEHENDUOKAIXIAO,ERQIETAHAIJUBEIGENGHAODESHISHIXING。

荣耀彩票代理RUTUSUOSHI,KEHUDUANFASONGYIGEQINGQIUDAOFUWUQI,FUWUQIJIESHOULELIANJIEHOUYIZHIBAOCHIZHULIANJIEBUGUANBI;JIEZHEKEHUDUANFASONGYIGECAOZUOBAOWENGAOSUFUWUQIXUYAOZUOSHENMECAOZUO,FUWUQICHULIWANSHIJIAN1HOUHUIJIKEHUDUANXIANGYING,RANHOUCHULIWANSHIJIAN2HOUYOUHUIJIKEHUDUANXIANGYING;RANHOUKEHUDUANJIXUFASONGCAOZUOBAOWENJIFUWUQI,FUWUQIZAIJINXINGXIANGYING。

Comet模式

YIBANCometMOSHIXUYAONIOPEIHE,ERZAIBIOZHONGWUFASHIYONGCometMOSHI。ZAITomcatNEIBUJICHENGCometMOSHIDESILUYEBIJIAOQINGXI,YINRULEYIGECometProcessorJIEKOU,CIJIEKOUZHIYOUYIGEeventFANGFA,JUTIJIEKOUDAIMARUXIA:

public interface CometProcessor extends Servlet{
    public void event(CometEvent event)
        throws IOException, ServletException;
}

而CometEvent则表示Comet相关的事件,它包含四BEGIN, READ, END, ERROR四个事件,分别表示:
① BEGIN,表示请求开始,此时客户端连接已被接收。
② READ,表示可以读取客户端连接,你可以开始读取数据了,读取的过程不会阻塞。
③ END,表示请求结束,此时客户端连接将被断开。
④ ERROR,表示发生了IO异常,一般将会结束此次请求并且连接会被断开。

XIAMIANKANYIGEJIANDANDELIZI:

public class CometServlet extends HttpServlet implements CometProcessor {

    protected ArrayList connections = new ArrayList();

    public void event(CometEvent event) throws IOException, ServletException {
        HttpServletRequest request = event.getHttpServletRequest();
        HttpServletResponse response = event.getHttpServletResponse();
        if (event.getEventType() == CometEvent.EventType.BEGIN) {
            synchronized (connections) {
                connections.add(response);
            }
        } else if (event.getEventType() == CometEvent.EventType.ERROR) {
            synchronized (connections) {
                connections.remove(response);
            }
        }else if (event.getEventType() == CometEvent.EventType.END) {
            synchronized (connections) {
                connections.remove(response);
            }
        } else if (event.getEventType() == CometEvent.EventType.READ) {
            InputStream is = request.getInputStream();
            byte[] buf = new byte[512];
            do {
                int n = is.read(buf);
                if (n > 0) {
                    System.out.println(new String(buf, 0, n));
                } else if (n < 0) {
                    return;
                }
            } while (is.available() > 0);
        }
    }
}

ZHEIGELIZIZHONGZHISHIJIANDANDEKEHUDUANLIANJIEDOUJIESHOUQILAIERBUZUORENHECHULI,BINGJIANGKEHUDUANFASONGGUOLAIDESHUJUSHUCHU。HENRONGYILIJIE,ZAIBEGINSHIJIANZHONGJIESHOULIANJIEBINGBAXIANGYINGDUIXIANGJIARUDAOLIEBIAOZHONG,FASONGERRORHUOENDSHIJIANSHIZEJIANGXIANGYINGDUIXIANGYICHU,DANGREADSHIJIANSHIZEDUQUSHUJUBINGSHUCHU。

有了CometProcessor接口后,Tomcat内部就可以识别Comet模式的Servlet了,我们知道Tomcat对请求的处理是管道模式的,所以在Wrapper容器的管道中判断加载的Servlet是否继承了CometProcessor,继承则说明是Comet模式,则使用Comet方式处理。它的处理过程如图,当一个客户端连接到来,被接收器接收后注册到NioChannel队列中,Poller组件不断轮询是否有NioChannel需要处理,如果有则调用前面实例化的Comet模式Servlet,这里主要用到CometProcessor接口的event方法,Poller会将对应的请求对象、响应对象和事件封装成都CometEvent对象并传入event方法。此时即执行event方法的逻辑,完成对不同事件的处理,从而实现了Comet模式。
集成Comet

延伸阅读:

Tag标签:   
  • 专题推荐

About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规