我也是參考其他博主的:
比如:
https://www.cnblogs.com/hujihon/p/5313731.html
下面是我自己看的一些源碼+個人理解:
maxTotal:默認值8
解釋:在指定時刻通過pool能夠獲取到的最大的連接的jedis個數
getMaxTotal():
Returns the maximum number of objects that can be allocated 分配指派 by the pool (checked out to clients, or idle awaiting checkout) at a given time. When negative, there is no limit to the number of objects that can be managed by the pool at one time.
Returns:the cap on the total number of object instances managed by the pool.
個人理解,在獲取當時通過pool能夠獲取到的最大的連接的jedis個數(已經被客戶端 連接上或者正在閑置等待客戶端連接)
舉例:
當我設置<property name="maxTotal" value="10"/>時,如果
for(int i=0;i<15;i++){
Jedis resource = jedisSentinelPool.getResource();
System.out.println(resource);
// resource.close();
}
如果沒有close的話,只取到了10個連接:會報錯:

報錯截圖:

當把resource.close();放開后:
沒報錯,每次都是取到一個對象:

maxIdle:默認值8
最大能夠保持idle的數量,控制一個pool最多有多少個狀態為idle的jedis實例
/** * Returns the cap on the number of "idle" instances in the pool. If maxIdle * is set too low on heavily loaded systems it is possible you will see * objects being destroyed and almost immediately new objects being created. * This is a result of the active threads momentarily returning objects * faster than they are requesting them them, causing the number of idle * objects to rise above maxIdle. The best value for maxIdle for heavily * loaded system will vary but the default is a good starting point. * * @return the maximum number of "idle" instances that can be held in the * pool or a negative value if there is no limit * * @see #setMaxIdle */
第一段的最后一句話說明如果加載系統負載不是很大的話,默認值就不錯
minIdle:默認值為0
minIdle:在容器中的最小的閑置連接數,僅僅在此值為正數且timeBetweenEvictionRunsMillis值大於0時有效
確保在對象逐出線程工作后確保線程池中有最小的實例數量,如果該值設定大於maxidle的值,此值不會生效,maxidle的值會生效 /** * Sets the target for the minimum number of idle objects to maintain in * the pool. This setting only has an effect if it is positive and * {@link #getTimeBetweenEvictionRunsMillis()} is greater than zero. If this * is the case, an attempt is made to ensure that the pool has the required * minimum number of instances during idle object eviction runs. * <p> * If the configured value of minIdle is greater than the configured value * for maxIdle then the value of maxIdle will be used instead. * * @param minIdle * The minimum number of objects. * * @see #getMinIdle() * @see #getMaxIdle() * @see #getTimeBetweenEvictionRunsMillis() */ public void setMinIdle(int minIdle) { this.minIdle = minIdle; }
maxWaitMillis:默認值-1
maxWaitMillis: 當連接池內的連接耗盡時,getBlockWhenExhausted為true時,連接會阻塞,超過了阻塞的時間(設定的maxWaitMillis,單位毫秒)時會報錯 /** * Sets the maximum amount of time (in milliseconds) the * <code>borrowObject()</code> method should block before throwing an * exception when the pool is exhausted and * {@link #getBlockWhenExhausted} is true. When less than 0, the * <code>borrowObject()</code> method may block indefinitely. * * @param maxWaitMillis the maximum number of milliseconds * <code>borrowObject()</code> will block or negative * for indefinitely. * * @see #getMaxWaitMillis * @see #setBlockWhenExhausted */ public final void setMaxWaitMillis(long maxWaitMillis) { this.maxWaitMillis = maxWaitMillis; }
maxWaitMillis測試:




可以看到我設定的最大的連接數為3個,在主線程中我要獲取4個連接,在獲取3個后,第四個就沒能獲取到了,這樣設定了等待超時時間后,過了3秒的超時時間就報錯了!
testOnBorrow:
testOnBorrow:在borrow一個jedis實例時,是否提前進行validate操作;如果為true,則得到的jedis實例均是可用的;默認是false private volatile boolean testOnBorrow = BaseObjectPoolConfig.DEFAULT_TEST_ON_BORROW; public static final boolean DEFAULT_TEST_ON_BORROW = false; /** * Sets whether objects borrowed from the pool will be validated before * being returned from the <code>borrowObject()</code> method. Validation is * performed by the <code>validateObject()</code> method of the factory * associated with the pool. If the object fails to validate, it will be * removed from the pool and destroyed, and a new attempt will be made to * borrow an object from the pool. *如果對象驗證失敗,會從池中移除,嘗試從池中獲取一個新的連接 * @param testOnBorrow <code>true</code> if objects should be validated * before being returned from the * <code>borrowObject()</code> method * * @see #getTestOnBorrow */ public final void setTestOnBorrow(boolean testOnBorrow) { this.testOnBorrow = testOnBorrow; }
testWhileIdle:
testWhileIdle:如果為true,表示有一個idle object evitor線程對idle object進行掃描,如果validate失敗,此object會被從pool中drop掉;這一項只有在timeBetweenEvictionRunsMillis大於0時才有意義;默認是false if testWhileIdle is true, examined objects are validated when visited (and removed if invalid); otherwise only objects that have been idle for more than <code>minEvicableIdleTimeMillis</code> are removed.</p> private volatile boolean testWhileIdle = BaseObjectPoolConfig.DEFAULT_TEST_WHILE_IDLE; public static final boolean DEFAULT_TEST_WHILE_IDLE = false; /** * Returns whether objects sitting idle in the pool will be validated by the * idle object evictor (if any - see * {@link #setTimeBetweenEvictionRunsMillis(long)}). Validation is performed * by the <code>validateObject()</code> method of the factory associated * with the pool. If the object fails to validate, it will be removed from * the pool and destroyed. Note that setting this property has no effect * unless the idle object evictor is enabled by setting * <code>timeBetweenEvictionRunsMillis</code> to a positive value. *會通過validateObject()方法進行校驗,如果校驗失敗,連接會被從池中移除。僅僅在timeBetweenEvictionRunsMillis設置為正數時才有效 * @param testWhileIdle * <code>true</code> so objects will be validated by the evictor * * @see #getTestWhileIdle * @see #setTimeBetweenEvictionRunsMillis */ public final void setTestWhileIdle(boolean testWhileIdle) { this.testWhileIdle = testWhileIdle; }
timeBetweenEvictionRunsMillis:
timeBetweenEvictionRunsMillis:表示idle object evitor兩次掃描之間要sleep的毫秒數,逐出掃描的時間間隔(毫秒),如果為負數,則不運行逐出線程,默認為-1
/**
* Sets the number of milliseconds to sleep between runs of the idle
* object evictor thread. When non-positive, no idle object evictor thread
* will be run.
*
* @param timeBetweenEvictionRunsMillis
* number of milliseconds to sleep between evictor runs
*
* @see #getTimeBetweenEvictionRunsMillis
*/
public final void setTimeBetweenEvictionRunsMillis(
long timeBetweenEvictionRunsMillis) {
this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
startEvictor(timeBetweenEvictionRunsMillis);
}
evictionPolicyClassName:
evictionPolicyClassName:設置的逐出策略類名,默認就是DefaultEvictionPolicy(當連接超過最大空閑時間時或連接數超過最大空閑連接數) <property name="evictionPolicyClassName" value="org.apache.commons.pool2.impl.DefaultEvictionPolicy"></property> /** * Sets the name of the {@link EvictionPolicy} implementation that is * used by this pool. The Pool will attempt to load the class using the * thread context class loader. If that fails, the Pool will attempt to load * the class using the class loader that loaded this class. * * @param evictionPolicyClassName the fully qualified class name of the * new eviction policy * * @see #getEvictionPolicyClassName() */ public final void setEvictionPolicyClassName( String evictionPolicyClassName) { try { Class<?> clazz; try { clazz = Class.forName(evictionPolicyClassName, true, Thread.currentThread().getContextClassLoader()); } catch (ClassNotFoundException e) { clazz = Class.forName(evictionPolicyClassName); } Object policy = clazz.newInstance(); if (policy instanceof EvictionPolicy<?>) { @SuppressWarnings("unchecked") // safe, because we just checked the class EvictionPolicy<T> evicPolicy = (EvictionPolicy<T>) policy; this.evictionPolicy = evicPolicy; } } catch (ClassNotFoundException e) { throw new IllegalArgumentException( "Unable to create EvictionPolicy instance of type " + evictionPolicyClassName, e); } catch (InstantiationException e) { throw new IllegalArgumentException( "Unable to create EvictionPolicy instance of type " + evictionPolicyClassName, e); } catch (IllegalAccessException e) { throw new IllegalArgumentException( "Unable to create EvictionPolicy instance of type " + evictionPolicyClassName, e); } }
其他的沒有整理,后期有時間的話再細看下~
2018-09-06
先搞明白-后熟悉-最后精湛
2018-09-12有修改
