连接池commons-pool源码学习之Hello World

缓存池的作用就是减少重复创建资源,不是到每次需要使用资源再创建,而是提前准备好资源,每次需要时,从池中获取,用完归还.

我们看看下面简单的 Hello World

从流中读取字符串,每次都需要创建StringBuffer

import java.io.Reader; 
import java.io.IOException; 
 
public class ReaderUtil { 
    public ReaderUtil() { 
    } 

    /** 
     * Dumps the contents of the {@link Reader} to a 
     * String, closing the {@link Reader} when done. 
     */ 
    public String readToString(Reader in) throws IOException { 
        StringBuffer buf = new StringBuffer(); 
        try { 
            for(int c = in.read(); c != -1; c = in.read()) { 
                buf.append((char)c); 
            } 
            return buf.toString(); 
        } catch(IOException e) { 
            throw e; 
        } finally { 
            try { 
                in.close(); 
            } catch(Exception e) { 
                // ignored 
            } 
        } 
    } 
}

有些麻烦吧,我们使用缓存池获取StringBuffer试试,直接上代码

import java.io.IOException;
import java.io.Reader;
import org.apache.commons.pool2.ObjectPool;

public class ReaderUtil {
    
    private ObjectPool pool;
    
    public ReaderUtil(ObjectPool pool) {
        this.pool = pool;
    }

    /**
     * Dumps the contents of the {@link Reader} to a String, closing the {@link Reader} when done.
     */
    public String readToString(Reader in)
        throws IOException {
        StringBuffer buf = null;
        try {
            //通过缓存池获取
            buf = pool.borrowObject();
            for (int c = in.read(); c != -1; c = in.read()) {
                buf.append((char) c);
            }
            return buf.toString();
        } catch (IOException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException("Unable to borrow buffer from pool" + e.toString());
        } finally {
            try {
                in.close();
            } catch (Exception e) {
                // ignored
            }
            try {
                if (null != buf) {
                    //使用完归还回缓存池
                    pool.returnObject(buf);
                }
            } catch (Exception e) {
                // ignored
            }
        }
    }
}

import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;

public class StringBufferFactory
    extends BasePooledObjectFactory {

    //创建对象的方法
    @Override
    public StringBuffer create() {
        return new StringBuffer();
    }

    /**
     * Use the default PooledObject implementation.
     * 使用默认的池化对象DefaultPooledObject封装StringBuffer,使之带上状态属性
     */
    @Override
    public PooledObject wrap(StringBuffer buffer) {
        return new DefaultPooledObject(buffer);
    }

    /**
     * When an object is returned to the pool, clear the buffer.
     * 钝化对象,下次之前可以再复用该对象
     */
    @Override
    public void passivateObject(PooledObject pooledObject) {
        pooledObject.getObject().setLength(0);
    }

    // for all other methods, the no-op implementation
    // in BasePooledObjectFactory will suffice
}
ReaderUtil readerUtil = new ReaderUtil(new GenericObjectPool(new StringBufferFactory()));

代码很简单,下回代码源码解读.

你可能感兴趣的:(连接池commons-pool源码学习之Hello World)