序
本文主要解析一下apache common pools下的GenericObjectPool的參數(shù)設(shè)置
GenericObjectPool
commons-pool2-2.4.2-sources.jar!/org/apache/commons/pool2/impl/GenericObjectPool.java
public class GenericObjectPool<T> extends BaseGenericObjectPool<T>
implements ObjectPool<T>, GenericObjectPoolMXBean, UsageTracking<T> {
//......
}
默認(rèn)配置見
commons-pool2-2.4.2-sources.jar!/org/apache/commons/pool2/impl/GenericObjectPoolConfig.java
public class GenericObjectPoolConfig extends BaseObjectPoolConfig {
/**
* The default value for the {@code maxTotal} configuration attribute.
* @see GenericObjectPool#getMaxTotal()
*/
public static final int DEFAULT_MAX_TOTAL = 8;
/**
* The default value for the {@code maxIdle} configuration attribute.
* @see GenericObjectPool#getMaxIdle()
*/
public static final int DEFAULT_MAX_IDLE = 8;
/**
* The default value for the {@code minIdle} configuration attribute.
* @see GenericObjectPool#getMinIdle()
*/
public static final int DEFAULT_MIN_IDLE = 0;
private int maxTotal = DEFAULT_MAX_TOTAL;
private int maxIdle = DEFAULT_MAX_IDLE;
private int minIdle = DEFAULT_MIN_IDLE;
//......
}
pool基本參數(shù)
基本參數(shù)
- lifo
GenericObjectPool 提供了后進(jìn)先出(LIFO)與先進(jìn)先出(FIFO)兩種行為模式的池榕莺。默認(rèn)為true留储,即當(dāng)池中有空閑可用的對象時(shí)救恨,調(diào)用borrowObject方法會返回最近(后進(jìn)
)的實(shí)例 - fairness
當(dāng)從池中獲取資源或者將資源還回池中時(shí) 是否使用java.util.concurrent.locks.ReentrantLock.ReentrantLock 的公平鎖機(jī)制,默認(rèn)為false
數(shù)量控制參數(shù)
maxTotal
鏈接池中最大連接數(shù),默認(rèn)為8maxIdle
鏈接池中最大空閑的連接數(shù),默認(rèn)也為8minIdle
連接池中最少空閑的連接數(shù),默認(rèn)為0
超時(shí)參數(shù)
- maxWaitMillis
當(dāng)連接池資源耗盡時(shí)辨绊,等待時(shí)間钞脂,超出則拋異常怔昨,默認(rèn)為-1即永不超時(shí) - blockWhenExhausted
當(dāng)這個(gè)值為true的時(shí)候逗威,maxWaitMillis參數(shù)才能生效惨缆。為false的時(shí)候东羹,當(dāng)連接池沒資源剂桥,則立馬拋異常。默認(rèn)為true
test參數(shù)
- testOnCreate
默認(rèn)false百姓,create的時(shí)候檢測是有有效渊额,如果無效則從連接池中移除,并嘗試獲取繼續(xù)獲取 - testOnBorrow
默認(rèn)false垒拢,borrow的時(shí)候檢測是有有效旬迹,如果無效則從連接池中移除,并嘗試獲取繼續(xù)獲取 - testOnReturn
默認(rèn)false求类,return的時(shí)候檢測是有有效奔垦,如果無效則從連接池中移除,并嘗試獲取繼續(xù)獲取 - testWhileIdle
默認(rèn)false尸疆,在evictor線程里頭椿猎,當(dāng)evictionPolicy.evict方法返回false時(shí)惶岭,而且testWhileIdle為true的時(shí)候則檢測是否有效,如果無效則移除
檢測參數(shù)
- timeBetweenEvictionRunsMillis
空閑鏈接檢測線程檢測的周期犯眠,毫秒數(shù)按灶。如果為負(fù)值,表示不運(yùn)行檢測線程筐咧。默認(rèn)為-1.
commons-pool2-2.4.2-sources.jar!/org/apache/commons/pool2/impl/GenericObjectPool.java
public GenericObjectPool(PooledObjectFactory<T> factory,
GenericObjectPoolConfig config) {
super(config, ONAME_BASE, config.getJmxNamePrefix());
if (factory == null) {
jmxUnregister(); // tidy up
throw new IllegalArgumentException("factory may not be null");
}
this.factory = factory;
idleObjects = new LinkedBlockingDeque<PooledObject<T>>(config.getFairness());
setConfig(config);
startEvictor(getTimeBetweenEvictionRunsMillis());
}
commons-pool2-2.4.2-sources.jar!/org/apache/commons/pool2/impl/BaseGenericObjectPool.java
/**
* The idle object evictor {@link TimerTask}.
*
* @see GenericKeyedObjectPool#setTimeBetweenEvictionRunsMillis
*/
class Evictor extends TimerTask {
/**
* Run pool maintenance. Evict objects qualifying for eviction and then
* ensure that the minimum number of idle instances are available.
* Since the Timer that invokes Evictors is shared for all Pools but
* pools may exist in different class loaders, the Evictor ensures that
* any actions taken are under the class loader of the factory
* associated with the pool.
*/
@Override
public void run() {
ClassLoader savedClassLoader =
Thread.currentThread().getContextClassLoader();
try {
if (factoryClassLoader != null) {
// Set the class loader for the factory
ClassLoader cl = factoryClassLoader.get();
if (cl == null) {
// The pool has been dereferenced and the class loader
// GC'd. Cancel this timer so the pool can be GC'd as
// well.
cancel();
return;
}
Thread.currentThread().setContextClassLoader(cl);
}
// Evict from the pool
try {
evict();
} catch(Exception e) {
swallowException(e);
} catch(OutOfMemoryError oome) {
// Log problem but give evictor thread a chance to continue
// in case error is recoverable
oome.printStackTrace(System.err);
}
// Re-create idle instances.
try {
ensureMinIdle();
} catch (Exception e) {
swallowException(e);
}
} finally {
// Restore the previous CCL
Thread.currentThread().setContextClassLoader(savedClassLoader);
}
}
}
- numTestsPerEvictionRun
在每次空閑連接回收器線程(如果有)運(yùn)行時(shí)檢查的連接數(shù)量鸯旁,默認(rèn)為3
private int getNumTests() {
int numTestsPerEvictionRun = getNumTestsPerEvictionRun();
if (numTestsPerEvictionRun >= 0) {
return Math.min(numTestsPerEvictionRun, idleObjects.size());
} else {
return (int) (Math.ceil(idleObjects.size() /
Math.abs((double) numTestsPerEvictionRun)));
}
}
minEvictableIdleTimeMillis
連接空閑的最小時(shí)間,達(dá)到此值后空閑連接將可能會被移除量蕊。默認(rèn)為1000L * 60L * 30LsoftMinEvictableIdleTimeMillis
連接空閑的最小時(shí)間铺罢,達(dá)到此值后空閑鏈接將會被移除,且保留minIdle個(gè)空閑連接數(shù)残炮。默認(rèn)為-1.evictionPolicyClassName
evict策略的類名韭赘,默認(rèn)為org.apache.commons.pool2.impl.DefaultEvictionPolicy
public class DefaultEvictionPolicy<T> implements EvictionPolicy<T> {
@Override
public boolean evict(EvictionConfig config, PooledObject<T> underTest,
int idleCount) {
if ((config.getIdleSoftEvictTime() < underTest.getIdleTimeMillis() &&
config.getMinIdle() < idleCount) ||
config.getIdleEvictTime() < underTest.getIdleTimeMillis()) {
return true;
}
return false;
}
}
這里就用到了上面提到的兩個(gè)參數(shù)