Apache Commons-pool2简记

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/caiwenfeng_for_23/article/details/69808342

Apache旗下的对象池框架common-pool2
官方网址:https://commons.apache.org/proper/commons-pool/

配置详解:

maxTotal  允许创建资源的最大数量,默认值 -1,-1 代表无数量限制(int类型)

blockWhenExhausted 默认值 true ,当资源耗尽时,是否阻塞等待获取资源

maxWaitMillis 获取资源时的等待时间,单位毫秒。当 blockWhenExhausted 配置为 true 时,此值有效。 -1 代表无时间限制,一直阻塞直到有可用的资源。(long类型)

testOnBorrow  默认值 false ,当设置为true时,调用  factory.validateObject() 方法

testOnCreate   默认值 false,当设置为true时,调用  factory.validateObject() 方法

(备注:如果 testOnBorrow 或者 testOnCreate 中有一个 配置 为 true 时,就调用  factory.validateObject() )

lifo  资源的存取数据结构,默认值 truetrue 资源按照栈结构存取,false 资源按照队列结构存取

fairness 当从池中获取资源或者将资源还回池中时 是否使用  java.util.concurrent.locks.ReentrantLock.ReentrantLock 的公平锁机制。 默认值 falsetrue 使用公平锁,false 不使用公平锁,

timeBetweenEvictionRunsMillis  回收资源线程的执行周期,单位毫秒。默认值 -1 ,-1 表示不启用线程回收资源。(long类型)

evictionPolicyClassName 资源回收策略,  默认值 org.apache.commons.pool2.impl.DefaultEvictionPolicy(String类型)

minEvictableIdleTimeMillis 资源最小空闲时间,  默认值 1800000,单位 毫秒,(long类型 )

softMinEvictableIdleTimeMillis 软资源最小空闲时间, 默认值 -1 ,单位 毫秒,(long类型 )

(备注,这个两个参数,在资源回收策略中,会使用到)

maxIdle  最大空闲资源数,默认值 8int类型)

minIdle  最小空闲资源数,默认值 0int类型 )

testWhileIdle  默认值 false; 设置为 true 时,当回收策略返回false时,则 调用 factory.activateObject()和factory.validateObject()

testOnReturn 默认值 false; 设置为 true 时,当将资源返还个资源池时候,验证资源的有效性,调用 factory.validateObject()方法,如果无效,则调用 factory.destroyObject()方法

numTestsPerEvictionRun  资源回收线程执行一次回收操作,回收资源的数量。默认值 3, (int类型)。

备注:

当 设置为0时,不回收资源。

设置为 小于0时,回收资源的个数为  (int)Math.ceil( 池中空闲资源个数 / Math.abs(numTestsPerEvictionRun) );

设置为 大于0时,回收资源的个数为  Math.min( numTestsPerEvictionRun,池中空闲的资源个数 );

使用案例:

对象池工厂类,负责生产、回收、校验对象。

public class StringBufferFactory
    extends BasePooledObjectFactory<StringBuffer> {

    @Override
    public StringBuffer create() {
        return new StringBuffer();
    }

    /**
     * Use the default PooledObject implementation.
     */
    @Override
    public PooledObject<StringBuffer> wrap(StringBuffer buffer) {
        return new DefaultPooledObject<StringBuffer>(buffer);
    }

    /**
     * When an object is returned to the pool, clear the buffer.
     */
    @Override
    public void passivateObject(PooledObject<StringBuffer> pooledObject) {
        pooledObject.getObject().setLength(0);
    }

    @Override
    public void activateObject(PooledObject<StringBuffer> p) throws Exception {
        // TODO Auto-generated method stub
        super.activateObject(p);
    }
}

具体使用:

import org.apache.commons.pool2.ObjectPool;

public class ReaderUtil {

    private ObjectPool<StringBuffer> pool;

    public ReaderUtil(ObjectPool<StringBuffer> pool) {
        this.pool = pool;
    }

    public String readToString(String abc){
        StringBuffer buf;
        try {
            buf=pool.borrowObject();
            buf.append(abc);
        } catch (Exception e) {
            throw new RuntimeException("Unable to borrow buffer from pool" + e.toString());

        }

        String result=buf.toString().trim();
        try {
//System.out.println("pool sizebefore:"+pool.getNumActive());
System.out.println("pool size before:"+pool.getNumIdle());
            pool.returnObject(buf);
System.out.println("pool size before:"+pool.getNumIdle());
//System.out.println("pool size after:"+pool.getNumActive());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }
}

源码下载

仅此笔记记录!

猜你喜欢

转载自blog.csdn.net/caiwenfeng_for_23/article/details/69808342