<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

    JAVA NIO 多線程服務器是 Nut (lucene + hadoop 分布式搜索運行框架)  Nut Search層封裝代碼


    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((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);
                
    else
                
    {
                    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(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);
            }

        }

    }


    public class MiniServer
    {
        
    private static final Log log = LogFactory.getLog(MiniServer.class);
        
        
    private final Selector s;
        
    private final ServerSocketChannel ssc;
        
    private ExecutorService executor;
        
        
    private static Map<String,Long> map=new TreeMap<String,Long>();//保存不能正確完成的SelectionKey
        private ScheduledExecutorService scheduled = Executors.newScheduledThreadPool(1);
        
        
    public MiniServer(int portnumber,ExecutorService executor) throws IOException
        
    {
            scheduled.scheduleAtFixedRate(task,
    10,10,TimeUnit.MINUTES);//每10分鐘清空一次map
            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 execute()
        
    {
            
    try
            
    {
                
    while(s.isOpen())
                
    {
                    
    int nKeys=s.select();
                    
    if(nKeys==0)
                    
    {
                        
    for (SelectionKey key : s.keys())
                        
    {
                            log.info(
    "channel " + key.channel() + " waiting for " + key.interestOps());
                            
    //如果超過2分鐘就廢除
                            if(map.containsKey(key.toString()))
                            
    {
                                Long t
    = map.get(key.toString());
                                
    if((NutUtil.now()-t)>200);
                                
    {
                                    map.remove(key.toString());
                                    s.keys().remove(key);
                                    key.cancel();
                                }

                            }

                            
    else
                            
    {
                                map.put(key.toString(), NutUtil.now());
                            }

                        }

                        
    continue;
                    }

                    
                    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);
            }

        }

        
        Runnable task 
    = new Runnable()
        
    {
            
    public void run()
            
    {
                map.clear();
            }

        }
    ;
    }
    posted on 2010-07-26 11:31 nianzai 閱讀(2706) 評論(2)  編輯  收藏 所屬分類: NIO

    評論:
    # re: JAVA NIO 多線程服務器 1.1版 2010-07-26 20:34 | intex充氣床
    謝謝!  回復  更多評論
      
    # re: JAVA NIO 多線程服務器 1.1版 2013-02-02 16:11 | jnan77
    ProcessCallable 這是什么包的呢  回復  更多評論
      

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


    網站導航:
     
    主站蜘蛛池模板: 永久黄网站色视频免费直播| 久久久久久曰本AV免费免费| 国产乱弄免费视频| 亚洲中文字幕无码一去台湾| 69堂人成无码免费视频果冻传媒| 久久亚洲美女精品国产精品| 在线成人爽a毛片免费软件| 久久青青草原亚洲av无码app | 好先生在线观看免费播放| 亚洲日本在线播放| 男人的好看免费观看在线视频| 亚洲人色大成年网站在线观看| 中文字幕无码成人免费视频| 亚洲中文无码卡通动漫野外| 国产免费午夜a无码v视频| 一级中文字幕乱码免费| 亚洲愉拍99热成人精品热久久| baoyu777永久免费视频| 亚洲自偷自拍另类12p| 成在人线AV无码免费| 国产亚洲精品2021自在线| 国产自偷亚洲精品页65页| 国产午夜精品免费一区二区三区| 97久久精品亚洲中文字幕无码 | 亚洲av无码一区二区三区乱子伦 | 亚洲天堂电影在线观看| 免费看香港一级毛片| 免费无码国产在线观国内自拍中文字幕 | 亚洲一区二区三区免费视频| 午夜爱爱免费视频| 精品久久久久久国产免费了| 亚洲精品视频免费在线观看| 成人免费在线观看网站| fc2免费人成为视频| 亚洲综合在线成人一区| 日本免费的一级v一片| 成人妇女免费播放久久久| 91亚洲国产成人久久精品| 国产精品色午夜视频免费看| a级成人毛片免费视频高清| 亚洲人成小说网站色|