<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 多線程服務(wù)器

    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 閱讀(3107) 評論(3)  編輯  收藏 所屬分類: NIO

    評論:
    # re: JAVA NIO 多線程服務(wù)器 1.2版 2011-08-30 13:59 | seo千里眼
    這個多線程程序挺實用哦。  回復(fù)  更多評論
      
    # re: JAVA NIO 多線程服務(wù)器 1.2版 2011-09-03 16:13 | 阿不都外力
    收藏一下!以后看。。。  回復(fù)  更多評論
      
    # re: JAVA NIO 多線程服務(wù)器 1.2版 2011-09-05 23:54 | 步步為營
    Tomcat中用NIO比較多,搭建高性能服務(wù)器時NIO挺好用的,呵呵  回復(fù)  更多評論
      

    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 黄网址在线永久免费观看| 四虎国产精品成人免费久久| 黄视频在线观看免费| 四虎影在线永久免费观看| 亚洲精品动漫免费二区| 热久久精品免费视频| 免费看男人j放进女人j免费看| 亚洲AV综合色区无码一区| 99久久婷婷免费国产综合精品| 亚洲av无码国产精品夜色午夜 | 亚洲午夜精品久久久久久浪潮| 亚洲av片在线观看| 亚洲日韩人妻第一页| 免费观看的a级毛片的网站| 国产91色综合久久免费| 亚洲日韩国产二区无码| 亚洲Av无码乱码在线观看性色| 国产美女视频免费观看的网站| 亚洲av无码无线在线观看| 亚洲人成小说网站色| yy6080久久亚洲精品| 一级毛片不卡片免费观看| 亚洲国产精品线观看不卡| 免费看国产曰批40分钟| 午夜老司机免费视频| 国产午夜精品理论片免费观看| 2020久久精品亚洲热综合一本| 亚洲国产婷婷香蕉久久久久久| 日本高清在线免费| 在线看无码的免费网站| 日韩色视频一区二区三区亚洲| 亚洲国产精品无码AAA片| 无码永久免费AV网站| 又粗又长又爽又长黄免费视频| 另类图片亚洲校园小说区| 亚洲综合一区二区精品导航| 歪歪漫画在线观看官网免费阅读 | 亚洲精品自产拍在线观看动漫| 在线看片无码永久免费aⅴ| 青青青国产手机频在线免费观看| 亚洲色大成网站www久久九|