<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 多線程服務(wù)器是 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 閱讀(2712) 評論(2)  編輯  收藏 所屬分類: NIO

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

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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 国产JIZZ中国JIZZ免费看| 毛片在线免费视频| 国产成人高清亚洲一区91| 亚洲六月丁香六月婷婷蜜芽| 亚洲无人区午夜福利码高清完整版| 全免费a级毛片免费看无码| 国产2021精品视频免费播放| 久热免费在线视频| 中国一级特黄高清免费的大片中国一级黄色片 | a国产成人免费视频| 国产免费无码一区二区| 无码av免费一区二区三区试看| 午夜精品一区二区三区免费视频| 成人啪精品视频免费网站| 国产男女性潮高清免费网站| 在线观看国产情趣免费视频| 又爽又高潮的BB视频免费看| 亚洲精品无码成人片在线观看 | 亚洲一区日韩高清中文字幕亚洲| 亚洲精品国产精品乱码不卡| 亚洲国产午夜精品理论片 | 91福利免费体验区观看区| 国产成人免费在线| 亚洲高清中文字幕免费| 妞干网免费观看视频| 免费一级做a爰片性色毛片| 亚洲精品视频在线播放| 亚洲国产美女精品久久| 亚洲日韩亚洲另类激情文学| 国产亚洲中文日本不卡二区| 亚洲а∨精品天堂在线| 久久av免费天堂小草播放| 69av免费观看| 国产一级高清视频免费看| 亚洲av午夜福利精品一区| 亚洲福利一区二区三区| 中文字幕无码免费久久9一区9 | 九九综合VA免费看| 一级做a爰全过程免费视频| jlzzjlzz亚洲乱熟在线播放| 亚洲国产精品成人AV在线|