`
收藏列表
标题 标签 来源
NIOselector 从Jetty、Tomcat和Mina中提炼NIO构架Server的经典模式
    public void doSelect(){
        int channelSize = channelQueue.size();
        SocketChannel channel;
        try {
            while(channelSize-->0 && (channel = channelQueue.poll()) != null){
                SelectionKey key = channel.register(selector,SelectionKey.OP_READ,null);
                NioEndPoint endpoint = createEndPoint(channel,key);
                key.attach(endpoint);
                endpoint.schedule();
            }
            int selected=selector.select(SELECT_TIMEOUT);
            if(selected > 0){
                try {

                    for (SelectionKey key: selector.selectedKeys()){
                        if(!key.isValid()){
                            key.cancel();
                            NioEndPoint endpoint = (NioEndPoint)key.attachment();
                            if (endpoint != null){
                                endpoint.updateKey();
                            }
                            continue;
                        }
                        Object att = key.attachment();
                        if (att instanceof NioEndPoint){
                            ((NioEndPoint)att).schedule();
                        }
                        else{
                            SocketChannel channelTmp = (SocketChannel)key.channel();
                            NioEndPoint endpoint = createEndPoint(channelTmp,key);
                            key.attach(endpoint);
                            if (key.isReadable()){
                                endpoint.schedule();     
                            }
                        }
                        key = null;
                    }
                } catch (Exception e) {
                    log.error("schedule error", e);
                }
            }
            selector.selectedKeys().clear();
        } catch (Exception e) {
            log.error("processor doSelect error", e);
        }
        
    }
Global site tag (gtag.js) - Google Analytics