<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 閱讀(3100) 評論(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挺好用的,呵呵  回復  更多評論
      
    主站蜘蛛池模板: 亚洲精品tv久久久久久久久久| 扒开双腿猛进入爽爽免费视频| 亚洲狠狠爱综合影院婷婷| 中文字幕亚洲码在线| 国产免费看JIZZ视频| 国产91在线|亚洲| 性盈盈影院免费视频观看在线一区| 亚洲自国产拍揄拍| 色吊丝永久在线观看最新免费| 亚洲国产精品99久久久久久| 国产精品视_精品国产免费| 精品国产_亚洲人成在线| 免费一级成人毛片| 拍拍拍无挡视频免费观看1000| 亚洲情综合五月天| 香港a毛片免费观看| 亚洲精品中文字幕乱码| 大学生a级毛片免费观看| 久久亚洲色WWW成人欧美| 亚洲av午夜精品一区二区三区| 久久WWW免费人成—看片| 亚洲精品高清国产一久久| 又大又硬又爽又粗又快的视频免费| 亚洲三级中文字幕| 成人人免费夜夜视频观看| 黄色一级视频免费观看| 亚洲区小说区激情区图片区| 全免费a级毛片免费看| 亚洲一区二区三区免费观看| 日本a级片免费看| 久久成人18免费网站| 亚洲日韩乱码中文无码蜜桃臀| 一二三四在线播放免费观看中文版视频| 最新亚洲卡一卡二卡三新区| 国产精品亚洲高清一区二区| 日韩精品人妻系列无码专区免费| 国产亚洲精品成人AA片| 国产亚洲一区二区三区在线不卡| 国产高清不卡免费在线| 一边摸一边桶一边脱免费视频| 久久久久亚洲AV无码麻豆|