<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-23  評論-58  文章-0  trackbacks-0
    Reactor 模式的 JAVA NIO 多線程服務器

    public class MiniServer extends Thread
    {
        
    private static final Log log = LogFactory.getLog(MiniServer.class);
        
        
    private final Selector s;
        
    private final ServerSocketChannel ssc;
        
    private ExecutorService executor;
        
        
    public MiniServer(int portnumber,ExecutorService executor) throws IOException
        
    {
            
    this.executor=executor;
            s 
    = Selector.open();
            ssc 
    = ServerSocketChannel.open();
            ssc.socket().bind(
    new InetSocketAddress(portnumber));
            ssc.configureBlocking(
    false);
            ssc.register(s,SelectionKey.OP_ACCEPT);
        }

        
        
    public void run()
        
    {
            
    try
            
    {
                
    while(s.isOpen())
                
    {
                    
    int nKeys=s.select();
                    
    if(nKeys>0)
                    
    {
                        Iterator
    <SelectionKey> it = s.selectedKeys().iterator();
                        
    while (it.hasNext()) 
                        
    {
                            SelectionKey key 
    = it.next();
                            it.remove();
                            
    if (!key.isValid() || !key.channel().isOpen())
                                
    continue;
                            
    if(key.isAcceptable())
                            
    {
                                SocketChannel sc 
    = ssc.accept();
                                
    if (sc != null)
                                
    {
                                    sc.configureBlocking(
    false);
                                    sc.register(s, SelectionKey.OP_READ, 
    new Reader(executor));
                                }

                            }

                            
    else if(key.isReadable()||key.isWritable())
                            
    {
                                Reactor reactor 
    = (Reactor) key.attachment();
                                reactor.execute(key);
                            }

                        }

                    }

                }

            }

            
    catch(IOException e)
            
    {
                log.info(e);
            }

        }

    }



    public interface Reactor 
    {
        
    void execute(SelectionKey key);
    }



    public class Reader implements Reactor 
    {
        
    private static final Log log = LogFactory.getLog(Reader.class);
        
        
    private byte[] bytes=new byte[0];
        
    private ExecutorService executor;
        
        
    public Reader(ExecutorService executor)
        
    {
            
    this.executor=executor;
        }

        
        @Override
        
    public void execute(SelectionKey key)
        
    {
            SocketChannel sc 
    = (SocketChannel) key.channel();
            
    try
            
    {
                ByteBuffer buffer
    =ByteBuffer.allocate(1024);
                
    int len=-1;
                
    while(sc.isConnected() && (len=sc.read(buffer))>0)
                
    {
                    buffer.flip();
                      
    byte [] content = new byte[buffer.limit()];
                    buffer.get(content);
                    bytes
    =NutUtil.ArrayCoalition(bytes,content);
                    buffer.clear();
                }

                
    if(len==0)
                
    {
                    key.interestOps(SelectionKey.OP_READ);
                    key.selector().wakeup(); 
                }

                
    else if(len==-1)
                
    {
                    Callable
    <byte[]> call=new ProcessCallable(bytes);
                    Future
    <byte[]> task=executor.submit(call);
                    ByteBuffer output
    =ByteBuffer.wrap(task.get());
                    sc.register(key.selector(), SelectionKey.OP_WRITE, 
    new Writer(output));
                }

            }

            
    catch(Exception e)
            
    {
                log.info(e);
            }

        }

    }



    public class Writer implements Reactor 
    {
        
    private static final Log log = LogFactory.getLog(Writer.class);
        
        
    private ByteBuffer output;
        
        
    public Writer(ByteBuffer output)
        
    {
            
    this.output=output;
        }

        
        
    public void execute(SelectionKey key)
        
    {
            SocketChannel sc 
    = (SocketChannel) key.channel();
            
    try
            
    {
                
    while(sc.isConnected() && output.hasRemaining())
                
    {
                    
    int len=sc.write(output);
                    
    if(len<0)
                    

                        
    throw new EOFException(); 
                    }
     
                    
    if(len==0
                    

                        key.interestOps(SelectionKey.OP_WRITE); 
                        key.selector().wakeup(); 
                        
    break
                    }

                }

                
    if(!output.hasRemaining())
                
    {
                    output.clear();
                    key.cancel();
                    sc.close();
                }

            }

            
    catch(IOException e)
            
    {
                log.info(e);
            }

        }

    }

    posted on 2011-08-29 18:35 nianzai 閱讀(3102) 評論(3)  編輯  收藏 所屬分類: NIO

    評論:
    # re: JAVA NIO 多線程服務器 1.2版 2011-08-30 13:59 | seo千里眼
    這個多線程程序挺實用哦。  回復  更多評論
      
    # re: JAVA NIO 多線程服務器 1.2版 2011-09-03 16:13 | 阿不都外力
    收藏一下!以后看。。。  回復  更多評論
      
    # re: JAVA NIO 多線程服務器 1.2版 2011-09-05 23:54 | 步步為營
    Tomcat中用NIO比較多,搭建高性能服務器時NIO挺好用的,呵呵  回復  更多評論
      
    主站蜘蛛池模板: 亚洲成人一级电影| 美丽的姑娘免费观看在线播放| 一级做a爰片久久毛片免费陪 | 久久久久亚洲AV无码专区首JN| 亚洲av永久无码精品天堂久久 | 91麻豆最新在线人成免费观看| 四虎永久在线免费观看| 亚洲AV日韩AV高潮无码专区| 亚洲欧洲无码AV不卡在线| 中文字幕乱理片免费完整的| 久久这里只有精品国产免费10| 国产成人综合亚洲AV第一页| 亚洲色丰满少妇高潮18p| jizz免费一区二区三区| 歪歪漫画在线观看官网免费阅读 | 免费人成年激情视频在线观看| 亚洲国产精品久久| 美女被免费网站视频在线| 黄色永久免费网站| 久久99国产亚洲高清观看首页| 美国毛片亚洲社区在线观看| 在线人成精品免费视频| 中文字幕亚洲一区| 日韩色视频一区二区三区亚洲| 毛片免费全部播放无码| 国产AV无码专区亚洲AWWW| 国产精品亚洲lv粉色| 五月亭亭免费高清在线| 久久国产亚洲精品麻豆| 特级毛片在线大全免费播放| 黄瓜视频高清在线看免费下载 | 国产亚洲无线码一区二区| 美女视频黄频a免费| 成人午夜免费福利| 亚洲一级毛片免费看| 污污网站18禁在线永久免费观看| 亚洲伊人色欲综合网| 久久免费视频一区| 亚洲视频在线精品| 乱人伦中文视频在线观看免费| 国产男女猛烈无遮挡免费网站 |