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

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

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

    隨筆-57  評(píng)論-202  文章-17  trackbacks-0
          使用Jakarta Commons Pool可以根據(jù)需要快速的實(shí)現(xiàn)自己的對(duì)象池,只需要實(shí)現(xiàn)PoolableObjectFactory或者KeyedPoolableObjectFactory接口。KeyedPoolableObjectFactory和PoolableObjectFactory的不同之處在于KeyedPoolableObjectFactory的每個(gè)方法都比PoolableObjectFactory多了一個(gè)Object key的參數(shù),使用這個(gè)參數(shù)可以使得對(duì)象池中的每個(gè)對(duì)象都有所不同。
          PoolableObjectFactory定義了五個(gè)方法(摘至Jakarta Commons Pool API文檔):
    1. makeObject is called whenever a new instance is needed.
    2. activateObject is invoked on every instance before it is returned from the pool.
    3. passivateObject is invoked on every instance when it is returned to the pool.
    4. destroyObject is invoked on every instance when it is being "dropped" from the pool (whether due to the response from validateObject, or for reasons specific to the pool implementation.)
    5. validateObject is invoked in an implementation-specific fashion to determine if an instance is still valid to be returned by the pool. It will only be invoked on an "activated" instance. 


          下面是我的一個(gè)SocketChannel對(duì)象池的實(shí)現(xiàn),實(shí)現(xiàn)了KeyedPoolableObjectFactory接口。

    package sample.pool;

    import java.net.SocketAddress;
    import java.nio.channels.SocketChannel;

    import org.apache.commons.pool.KeyedPoolableObjectFactory;

    /**
     * <p>Title: </p>
     *
     * <p>Description: </p>
     *
     * <p>Copyright: Copyright (c) 2005</p>
     *
     * <p>Company: </p>
     *
     * @author George Hill
     * @version 1.0
     
    */


    public class SocketPoolableObjectFactory implements KeyedPoolableObjectFactory {

        
    /**
         * 創(chuàng)建新的對(duì)象
         * @param key Object 創(chuàng)建對(duì)象需要用到的參數(shù)
         * @return Object SocketChannel實(shí)例
         * @throws Exception
         
    */

        
    public Object makeObject(Object key) throws Exception {
        SocketAddress address 
    = (SocketAddress) key;

        
    // 創(chuàng)建SocketChannel
        SocketChannel channel = SocketChannel.open(address);

        
    return channel;
      }


      
    /**
       * 銷(xiāo)毀對(duì)象
       * @param key Object 創(chuàng)建對(duì)象時(shí)的參數(shù)
       * @param obj Object 需要銷(xiāo)毀的對(duì)象
       * @throws Exception
       
    */

      
    public void destroyObject(Object key, Object obj) throws Exception {
        SocketChannel channel 
    = (SocketChannel) obj;

        
    if (channel != null)
          channel.close();
        channel 
    = null;
      }


      
    /**
       * 檢驗(yàn)對(duì)象是否有效
       * @param key Object 創(chuàng)建對(duì)象時(shí)的參數(shù)
       * @param obj Object 需要進(jìn)行檢驗(yàn)的對(duì)象
       * @return boolean 有效返回true,無(wú)效返回false
       
    */

      
    public boolean validateObject(Object key, Object obj) {
        SocketChannel channel 
    = (SocketChannel) obj;

        
    if (channel != null && channel.isOpen() && channel.isConnected())
          
    return true;

        
    return false;
      }


      
    /**
       * 將對(duì)象激活,這里不需要做任何工作
       * @param key Object
       * @param obj Object
       * @throws Exception
       
    */

      
    public void activateObject(Object key, Object obj) throws Exception {
      }


      
    /**
       * 將對(duì)象掛起,這里不需要做任何工作
       * @param key Object
       * @param obj Object
       * @throws Exception
       
    */

      
    public void passivateObject(Object key, Object obj) throws Exception {
      }


    }

          我的測(cè)試程序:

    package sample.pool;

    import java.io.IOException;
    import java.net.
    *;
    import java.nio.ByteBuffer;
    import java.nio.channels.
    *;
    import java.util.
    *;

    import org.apache.commons.pool.
    *;
    import org.apache.commons.pool.impl.
    *;

    /**
     * <p>Title: 測(cè)試的線(xiàn)程類(lèi)</p>
     *
     * <p>Description: </p>
     *
     * <p>Copyright: Copyright (c) 2005</p>
     *
     * <p>Company: </p>
     *
     * @author George Hill
     * @version 1.0
     
    */


    public class TestThread implements Runnable {

      
    // 線(xiàn)程名稱(chēng)
      private String name;

      
    // 對(duì)象池
      private KeyedObjectPool pool;

      
    // 連接的網(wǎng)絡(luò)地址
      private InetSocketAddress address;

      
    public TestThread(String name, KeyedObjectPool pool,
                        InetSocketAddress address) 
    {
        
    this.name = name;
        
    this.pool = pool;
        
    this.address = address;
      }


      
    public void run() {
        System.
    out.println(name + ": Client Start");
        SocketChannel channel 
    = null;

        
    try {
          channel 
    = (SocketChannel) pool.borrowObject(address);
        }

        
    catch (Exception ex) {
          ex.printStackTrace();
        }


        
    // 從對(duì)象池中借出對(duì)象成功
        if (channel != null{
          System.
    out.println(name + ": Borrow Channel successfully!");

          
    try {
            channel.configureBlocking(
    false);

            
    // 創(chuàng)建Selector
            Selector selector = Selector.open();
            
    // 向Selector注冊(cè)我們需要的READ事件
            SelectionKey skey = channel.register(selector, SelectionKey.OP_READ);

            boolean stop 
    = false;
            
    int n = 0;
            
    int read = 0;
            ByteBuffer buffer 
    = ByteBuffer.allocate(1024);

            System.
    out.println("Client Start");

            
    // 輪詢(xún)
            while (!stop) {
              
    // 獲取Selector返回的時(shí)間值
              n = selector.select();

              
    // 當(dāng)傳回的值大于0事,讀事件發(fā)生了
              if (n > 0{
                Set 
    set = selector.selectedKeys();
                Iterator it 
    = set.iterator();

                
    while (it.hasNext()) {
                  skey 
    = (SelectionKey) it.next();
                  it.remove();

                  
    if (skey.isReadable()) {
                    SocketChannel sc 
    = (SocketChannel) skey.channel();

                    
    while ( (read = sc.read(buffer)) != -1{
                      
    if (read == 0{
                        
    break;
                      }


                      buffer.flip();
                      
    byte[] array = new byte[read];
                      buffer.
    get(array);
                      String s 
    = new String(array);
                      System.
    out.print(s);
                      buffer.clear();

                      
    if (s.indexOf("new"!= -1{
                        stop 
    = true;
                        System.
    out.println();
                      }

                    }

                  }

                }

              }

            }

          }

          
    catch (IOException ioe) {
            ioe.printStackTrace();
          }


          
    try {
            pool.returnObject(address, channel);
          }

          
    catch (Exception ex) {
            ex.printStackTrace();
          }

        }


        System.
    out.println(name + ": Client Stop");
      }


      
    /**
       * 測(cè)試方法
       * @param args String[] 控制臺(tái)參數(shù)
       * @throws Exception
       
    */

      
    public static void main(String[] args) throws Exception {
        SocketPoolableObjectFactory factory 
    = new SocketPoolableObjectFactory();
        StackKeyedObjectPoolFactory poolFactory 
    = new StackKeyedObjectPoolFactory(factory);
        KeyedObjectPool pool 
    = poolFactory.createPool();

        
    // 創(chuàng)建連接清華BBS的線(xiàn)程
        Thread t1 = new Thread(new TestThread("清華", pool, new InetSocketAddress("bbs.tsinghua.edu.cn"23)));
        t1.start();
        
    // 創(chuàng)建連接華南理工BBS的線(xiàn)程
        Thread t2 = new Thread(new TestThread("華南理工", pool, new InetSocketAddress("bbs.gznet.edu.cn"23)));
        t2.start();
      }


    }



          參考資料:
          1. Jakarta Commons Pool網(wǎng)站:http://jakarta.apache.org/commons/pool/
          2. IBM開(kāi)發(fā)者的一篇很好的文章《使用Jakarta Commons Pool處理對(duì)象池化》
    posted on 2005-05-20 14:08 小米 閱讀(4094) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): Java
    主站蜘蛛池模板: 最新国产AV无码专区亚洲| 日木av无码专区亚洲av毛片| 亚洲国产精品国自产拍电影| 亚洲va久久久久| 三年片免费高清版 | 国产日产亚洲系列| 亚洲国语在线视频手机在线| 免费在线观看一区| 全免费毛片在线播放| 奇米影视亚洲春色| 亚洲一区AV无码少妇电影| 99在线热播精品免费99热| 毛片a级毛片免费观看品善网| 亚洲精品夜夜夜妓女网| 亚洲日韩国产AV无码无码精品| 怡红院免费全部视频在线视频| 免费观看一级毛片| 亚洲黄色三级视频| 九九视频高清视频免费观看| 国产福利在线免费| 亚洲国产精品一区二区第一页| 国产亚洲精彩视频| 免费中文熟妇在线影片 | 久久久无码精品亚洲日韩蜜臀浪潮| 免费看一级高潮毛片| 97人妻无码一区二区精品免费| 亚洲男同帅GAY片在线观看| 亚洲成熟丰满熟妇高潮XXXXX| 鲁大师在线影院免费观看| 亚洲欧洲日本在线| 亚洲av无码专区青青草原| 足恋玩丝袜脚视频免费网站| 久久久亚洲精品蜜桃臀| 亚洲国产美女精品久久久| 91精品免费观看| 亚洲老妈激情一区二区三区| 国产亚洲综合精品一区二区三区| 久久WWW色情成人免费观看| 久久久久久亚洲精品成人| 99麻豆久久久国产精品免费| 日批日出水久久亚洲精品tv|