我也是参考其他博主的:
比如:
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有修改