对象池GenericObjectPool配置参数详解

更新时间:2023-05-12 02:01:06 阅读: 评论:0

对象池GenericObjectPool配置参数详解
对象池 GenericObjectPool 配置参数详解
依赖
<dependency>
<groupId>s</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.3</version>
</dependency>
使⽤ GenericObjectPool,有必要了解⼀下 GenericObjectPoolConfig,下⾯将说明⼀下其配置参数。
1. ⽗类BaObjectPoolConfig配置参数
/**
* Provides the implementation for the common attributes shared by the
* sub-class. New instances of this class will be created using the defaults
* defined by the public constants.
* <p>
* This class is not thread-safe.
*  * @since 2.0
*/
public abstract class BaObjectPoolConfig extends BaObject implements Cloneable {
private boolean lifo = DEFAULT_LIFO;
private boolean fairness = DEFAULT_FAIRNESS;
private long maxWaitMillis = DEFAULT_MAX_WAIT_MILLIS;
private long minEvictableIdleTimeMillis = DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS;
private long evictorShutdownTimeoutMillis = DEFAULT_EVICTOR_SHUTDOWN_TIMEOUT_MILLIS;
private long softMinEvictableIdleTimeMillis = DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS;
private int numTestsPerEvictionRun = DEFAULT_NUM_TESTS_PER_EVICTION_RUN;
private String evictionPolicyClassName = DEFAULT_EVICTION_POLICY_CLASS_NAME;
private boolean testOnCreate = DEFAULT_TEST_ON_CREATE;
private boolean testOnBorrow = DEFAULT_TEST_ON_BORROW;
private boolean testOnReturn = DEFAULT_TEST_ON_RETURN;
private boolean testWhileIdle = DEFAULT_TEST_WHILE_IDLE;
private long timeBetweenEvictionRunsMillis = DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MIL
LIS;
private boolean blockWhenExhausted = DEFAULT_BLOCK_WHEN_EXHAUSTED;
private boolean jmxEnabled = DEFAULT_JMX_ENABLE;
private String jmxNamePrefix = DEFAULT_JMX_NAME_PREFIX;
private String jmxNameBa = DEFAULT_JMX_NAME_BASE;
}
lifo
提供了后进先出(LIFO)与先进先出(FIFO)两种⾏为模式的池;
默认 DEFAULT_LIFO = true, 当池中有空闲可⽤的对象时,调⽤borrowObject⽅法会返回最近(后进)的实例。
s.pool2.impl.GenericObjectPool
if(getLifo()){
idleObjects.addFirst(p);
}el{
idleObjects.addLast(p);
}
fairness
当从池中获取资源或者将资源还回池中时,是否使⽤;urrent.locks.ReentrantLock.ReentrantLock 的公平锁机制。默认DEFAULT_FAIRNESS = fal
s.pool2.impl.GenericObjectPool
idleObjects =new LinkedBlockingDeque<PooledObject<T>>(Fairness());
maxWaitMillis
当连接池资源⽤尽后,调⽤者获取连接时的最⼤等待时间(单位 :毫秒);
默认值 DEFAULT_MAX_WAIT_MILLIS = -1L, 永不超时。
s.pool2.impl.GenericObjectPool
@Override
public T borrowObject()throws Exception {
return borrowObject(getMaxWaitMillis());
}
minEvictableIdleTimeMillis
连接的最⼩空闲时间,达到此值后该空闲连接可能会被移除(还需看是否已达最⼤空闲连接数);
默认值 DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS = 1000L * 60L * 30L
s.pool2.impl.GenericObjectPool
final EvictionConfig evictionConfig =new EvictionConfig(
getMinEvictableIdleTimeMillis(),
getSoftMinEvictableIdleTimeMillis(),
getMinIdle());
evictorShutdownTimeoutMillis
关闭驱逐线程的超时时间;
默认值 DEFAULT_EVICTOR_SHUTDOWN_TIMEOUT_MILLIS = 10L * 1000L
s.pool2.impl.BaGenericObjectPool
final void startEvictor(final long delay){
synchronized(evictionLock){
if(null != evictor){
EvictionTimer.cancel(evictor, evictorShutdownTimeoutMillis, TimeUnit.MILLISECONDS);
evictor = null;
evictionIterator = null;
}
if(delay >0){
evictor =new Evictor();
EvictionTimer.schedule(evictor, delay, delay);
}
}
}
softMinEvictableIdleTimeMillis
连接空闲的最⼩时间,达到此值后空闲链接将会被移除,且保留minIdle个空闲连接数;
默认值 DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS = -1
numTestsPerEvictionRun
检测空闲对象线程每次运⾏时检测的空闲对象的数量;
如果 numTestsPerEvictionRun>=0, 则取numTestsPerEvictionRun 和池内的连接数 的较⼩值 作为每次检测的连接数;
如果 numTestsPerEvictionRun<0,则每次检查的连接数是检查时池内连接的总数除以这个值的绝对值再向上取整的结果;
默认值 DEFAULT_NUM_TESTS_PER_EVICTION_RUN = 3
s.pool2.impl.GenericObjectPool
private int getNumTests(){
final int numTestsPerEvictionRun =getNumTestsPerEvictionRun();
if(numTestsPerEvictionRun >=0){
return Math.min(numTestsPerEvictionRun, idleObjects.size());
}
return(int)(il(idleObjects.size()/
Math.abs((double) numTestsPerEvictionRun)));
}
evictionPolicyClassName
驱逐策略的类名;
默认值 DEFAULT_EVICTION_POLICY_CLASS_NAME = “s.pool2.impl.DefaultEvictionPolicy”s.pool2.impl.GenericObjectPool
public final void tEvictionPolicyClassName(final String evictionPolicyClassName){
try{
Class<?> clazz;
try{
clazz = Class.forName(evictionPolicyClassName,true, Thread.currentThread().getContextClassLoader());
}catch(final ClassNotFoundException e){
clazz = Class.forName(evictionPolicyClassName);
}
final Object policy = wInstance();
if(policy instanceof EvictionPolicy<?>){
@SuppressWarnings("unchecked")// safe, becau we just checked the class
final EvictionPolicy<T> evicPolicy =(EvictionPolicy<T>) policy;
this.evictionPolicy = evicPolicy;
}el{
throw new IllegalArgumentException("["+ evictionPolicyClassName +"] does not implement EvictionPolicy");
}
}catch(final ClassNotFoundException e){
throw new IllegalArgumentException("Unable to create EvictionPolicy instance of type "+ evictionPolicyClassName, e);
}catch(final InstantiationException e){
throw new IllegalArgumentException("Unable to create EvictionPolicy instance of type "+ evictionPolicyClassName, e);
}catch(final IllegalAccessException e){
throw new IllegalArgumentException("Unable to create EvictionPolicy instance of type "+ evictionPolicyClassName, e);
}
}
testOnCreate
在创建对象时检测对象是否有效(true : 是) , 配置true会降低性能;
默认值 DEFAULT_TEST_ON_CREATE = fal。
s.pool2.impl.GenericObjectPool##borrowObject(final long borrowMaxWaitMillis)
PooledObject<T> p = null;
// ...省略
// 配置true,新创建对象都会检测对象是否有效【 create && getTestOnCreate() 】
if(p != null &&(getTestOnBorrow()|| create &&getTestOnCreate())){
boolean validate =fal;
Throwable validationThrowable = null;
try{
validate = factory.validateObject(p);
}catch(final Throwable t){
PoolUtils.checkRethrow(t);
validationThrowable = t;
}
if(!validate){
try{
destroy(p);
destroyedByBorrowValidationCount.incrementAndGet();
}catch(final Exception e){
/
/ Ignore - validation failure is more important
// Ignore - validation failure is more important
}
p = null;
if(create){
final NoSuchElementException ne =new NoSuchElementException("Unable to validate object");
ne.initCau(validationThrowable);
throw ne;
}
}
}
testOnBorrow
在从对象池获取对象时是否检测对象有效(true : 是) , 配置true会降低性能;
默认值 DEFAULT_TEST_ON_BORROW = fal
s.pool2.impl.GenericObjectPool##borrowObject(final long borrowMaxWaitMillis)**
// 配置true,从对象池获取对象,总是要检测对象是否有效【 getTestOnBorrow() 】
if(p != null &&(getTestOnBorrow()|| create &&getTestOnCreate())){
// ...省略
}
testOnReturn
在向对象池中归还对象时是否检测对象有效(true : 是) , 配置true会降低性能;
默认值 DEFAULT_TEST_ON_RETURN = fal
s.pool2.impl.GenericObjectPool##returnObject(final T obj)
if(getTestOnReturn()){
if(!factory.validateObject(p)){
try{
destroy(p);
}catch(final Exception e){
swallowException(e);
}
try{
ensureIdle(1,fal);
}catch(final Exception e){
swallowException(e);
}
updateStatsReturn(activeTime);
return;
}
}
testWhileIdle
在检测空闲对象线程检测到对象不需要移除时,是否检测对象的有效性。建议配置为true,不影响性能,并且保证安全性;默认值 DEFAULT_TEST_WHILE_IDLE = fal
s.pool2.impl.GenericObjectPool##evict()
final boolean testWhileIdle =getTestWhileIdle();
// .... 代码省略
// 配置为true,检测空闲对象线程检测到对象不需要移除时,检测对象的有效性
if(testWhileIdle){
boolean active =fal;
try{
factory.activateObject(underTest);
active =true;
}catch(final Exception e){
destroy(underTest);
destroyedByEvictorCount.incrementAndGet();
}
if(active){
if(!factory.validateObject(underTest)){
destroy(underTest);
destroyedByEvictorCount.incrementAndGet();
}el{
}el{
try{
factory.passivateObject(underTest);
}catch(final Exception e){
destroy(underTest);
destroyedByEvictorCount.incrementAndGet();
}
}
}
}
timeBetweenEvictionRunsMillis
空闲连接检测的周期(单位毫秒);如果为负值,表⽰不运⾏检测线程;
默认值 DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS = -1L
s.pool2.impl.GenericObjectPool
public GenericObjectPool(final PooledObjectFactory<T> factory,final GenericObjectPoolConfig config){
super(config, ONAME_BASE, JmxNamePrefix());
if(factory == null){
jmxUnregister();// tidy up
throw new IllegalArgumentException("factory may not be null");
}
this.factory = factory;
idleObjects =new LinkedBlockingDeque<PooledObject<T>>(Fairness());
tConfig(config);
// 启动空闲连接检测线程
startEvictor(getTimeBetweenEvictionRunsMillis());
}
blockWhenExhausted
当对象池没有空闲对象时,新的获取对象的请求是否阻塞(true 阻塞,maxWaitMillis 才⽣效; fal 连接池没有资源⽴马抛异常)默认值 DEFAULT_BLOCK_WHEN_EXHAUSTED = true
s.pool2.impl.GenericObjectPool##borrowObject(final long borrowMaxWaitMillis)
final boolean blockWhenExhausted =getBlockWhenExhausted();
// ... 省略
if(blockWhenExhausted){
if(p == null){
if(borrowMaxWaitMillis <0){
p = idleObjects.takeFirst();
}el{
p = idleObjects.pollFirst(borrowMaxWaitMillis,
TimeUnit.MILLISECONDS);
}
}
if(p == null){
throw new NoSuchElementException(
"Timeout waiting for idle object");
}
}
jmxEnabled
是否注册JMX
默认值 DEFAULT_JMX_ENABLE = true
s.pool2.impl.BaGenericObjectPool
public BaGenericObjectPool(final BaObjectPoolConfig config,final String jmxNameBa,final String jmxNamePrefix){ JmxEnabled()){
}el{
}

本文发布于:2023-05-12 02:01:06,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/885816.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:对象   检测   空闲   是否   配置   连接
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图