摘要:如果為負(fù)值,表示不運行檢測線程。默認(rèn)為策略的類名,默認(rèn)為這里就用到了上面提到的兩個參數(shù)對象池原理分析避免泄漏配置參數(shù)詳解,以及資源回收,從池中獲取資源,將資源返還給池邏輯解析
序
本文主要解析一下apache common pools下的GenericObjectPool的參數(shù)設(shè)置
GenericObjectPoolcommons-pool2-2.4.2-sources.jar!/org/apache/commons/pool2/impl/GenericObjectPool.java
public class GenericObjectPoolextends BaseGenericObjectPool implements ObjectPool , GenericObjectPoolMXBean, UsageTracking { //...... }
默認(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)池中有空閑可用的對象時,調(diào)用borrowObject方法會返回最近(后進(jìn))的實例
fairness
當(dāng)從池中獲取資源或者將資源還回池中時 是否使用java.util.concurrent.locks.ReentrantLock.ReentrantLock 的公平鎖機制,默認(rèn)為false
maxTotal
鏈接池中最大連接數(shù),默認(rèn)為8
maxIdle
鏈接池中最大空閑的連接數(shù),默認(rèn)也為8
minIdle
連接池中最少空閑的連接數(shù),默認(rèn)為0
maxWaitMillis
當(dāng)連接池資源耗盡時,等待時間,超出則拋異常,默認(rèn)為-1即永不超時
blockWhenExhausted
當(dāng)這個值為true的時候,maxWaitMillis參數(shù)才能生效。為false的時候,當(dāng)連接池沒資源,則立馬拋異常。默認(rèn)為true
testOnCreate
默認(rèn)false,create的時候檢測是有有效,如果無效則從連接池中移除,并嘗試獲取繼續(xù)獲取
testOnBorrow
默認(rèn)false,borrow的時候檢測是有有效,如果無效則從連接池中移除,并嘗試獲取繼續(xù)獲取
testOnReturn
默認(rèn)false,return的時候檢測是有有效,如果無效則從連接池中移除,并嘗試獲取繼續(xù)獲取
testWhileIdle
默認(rèn)false,在evictor線程里頭,當(dāng)evictionPolicy.evict方法返回false時,而且testWhileIdle為true的時候則檢測是否有效,如果無效則移除
timeBetweenEvictionRunsMillis
空閑鏈接檢測線程檢測的周期,毫秒數(shù)。如果為負(fù)值,表示不運行檢測線程。默認(rèn)為-1.
commons-pool2-2.4.2-sources.jar!/org/apache/commons/pool2/impl/GenericObjectPool.java
public GenericObjectPool(PooledObjectFactoryfactory, 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 >(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
在每次空閑連接回收器線程(如果有)運行時檢查的連接數(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
連接空閑的最小時間,達(dá)到此值后空閑連接將可能會被移除。默認(rèn)為1000L 60L 30L
softMinEvictableIdleTimeMillis
連接空閑的最小時間,達(dá)到此值后空閑鏈接將會被移除,且保留minIdle個空閑連接數(shù)。默認(rèn)為-1.
evictionPolicyClassName
evict策略的類名,默認(rèn)為org.apache.commons.pool2.impl.DefaultEvictionPolicy
public class DefaultEvictionPolicyimplements EvictionPolicy { @Override public boolean evict(EvictionConfig config, PooledObject underTest, int idleCount) { if ((config.getIdleSoftEvictTime() < underTest.getIdleTimeMillis() && config.getMinIdle() < idleCount) || config.getIdleEvictTime() < underTest.getIdleTimeMillis()) { return true; } return false; } }
doc這里就用到了上面提到的兩個參數(shù)
Apache commons-pool對象池原理分析
GenericObjectPool 避免泄漏
apache-common-pool2(配置參數(shù)詳解,以及資源回收,從池中獲取資源,將資源返還給池 邏輯解析)
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/67806.html
摘要:實際應(yīng)用中由于程序?qū)崿F(xiàn)的問題,可能造成在一些極端的情況下出現(xiàn)沒有被調(diào)用導(dǎo)致的泄漏問題。在的時候檢查是否有泄漏的時候檢查泄漏如果一個對象之后秒還沒有返還給,認(rèn)為是泄漏的對象秒運行一次維護(hù)任務(wù) 更多內(nèi)容,請訪問 https://my.oschina.net/u/5751... GenericObjectPool GenericObjectPool 是 Apache Commons Poo...
摘要:使用提供了中對象池管理方式,它們的使用方式基本一樣,這里以對象池為例介紹其使用方式,一般實現(xiàn)自己的對象池需要經(jīng)過個步驟實現(xiàn)接口該接口是一種工廠模式,實現(xiàn)其目的是讓對象池通過該工廠模式創(chuàng)建管理的對象創(chuàng)建對象池實例創(chuàng)建對象池我們假設(shè)對象是一 common-pool2 使用 common-pool2提供了3中對象池管理方式,它們的使用方式基本一樣,這里以GenericObjectPool對象...
摘要:序本文主要聊聊的參數(shù)。主要用來做連接池的泄露檢測用。的狀態(tài)一般是用于連接泄露的檢測,檢測的是在使用的對象,比如懷疑那個對象被占用時間超長,那估計是程序異常或?qū)е聦ο罅说洑w還,或者對象之后使用時間太長。 序 本文主要聊聊GenericObjectPool的abandon參數(shù)。主要用來做連接池的泄露檢測用。 object的狀態(tài) commons-pool2-2.4.2-sources.j...
摘要:,的使用連接池的配置信息說明一個可以有多少個實例最大最小獲得一個實例的時候是否檢查連接可用性一個實例給時,是否檢查連接可用性狀態(tài)監(jiān)測用異步線程進(jìn)行檢查,一次最多的里的實例個數(shù) 1,JedisPool的使用 //WHEN_EXHAUSTED_FAIL = 0; ...
閱讀 2573·2021-09-30 10:00
閱讀 3508·2021-09-22 10:54
閱讀 6325·2021-09-07 10:28
閱讀 2964·2019-08-29 13:53
閱讀 755·2019-08-29 12:42
閱讀 972·2019-08-26 13:51
閱讀 1268·2019-08-26 13:32
閱讀 3036·2019-08-26 10:39