最近看到有人在使用jedis链接redis时,出现链接超时的情况,因此看了看jedis在代码中参数的作用.
jedis的配置参数主要用于pool中,目前默认采用的是GenericObjectPool类中。看类的说明文档就已经很明白了.
主要的参数有一下几个:
maxActive 最大链接数
maxIdle 最小链接数
whenExhaustedAction 这是pool用尽时,jedis的行为配置,默认为 WHEN_EXHAUSTED_BLOCK ,也就是阻塞,等待pool中有可用的链接,阻塞时间有maxWait设置,如果值为小于0,则无线阻塞,知道有可用链接。
其他的行为有,WHEN_EXHAUSTED_FAIL (直接抛出异常),WHEN_EXHAUSTED_GROW(新建一个连接)
maxWait
testOnBorrow ,获取连接之前,是否测试连接可用,默认是false。网络不稳定的情况,可以采用true,如果测试不通过,从pool中移除,并再次执行获取连接。
testOnReturn , 返回连接时,是否测试连接可用,默认是false。
另外还有一部分配置pool队列,已经对pool中空闲的连接进行验证的配置参数,跟c3p0等连接池没有太多差异.
原文如下:
* A <tt>GenericObjectPool</tt> provides a number of configurable parameters:
* <ul>
* <li>
* {@link #setMaxActive <i>maxActive</i>} controls 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 non-positive, there is no
* limit to the number of objects that can be managed by the pool at one time.
* When {@link #setMaxActive <i>maxActive</i>} is reached, the pool is said
* to be exhausted. The default setting for this parameter is 8.
* </li>
* <li>
* {@link #setMaxIdle <i>maxIdle</i>} controls the maximum number of objects
* that can sit idle in the pool at any time. When negative, there is no
* limit to the number of objects that may be idle at one time. The default
* setting for this parameter is 8.
* </li>
* <li>
* {@link #setWhenExhaustedAction <i>whenExhaustedAction</i>} specifies the
* behavior of the {@link #borrowObject} method when the pool is exhausted:
* <ul>
* <li>
* When {@link #setWhenExhaustedAction <i>whenExhaustedAction</i>} is
* {@link #WHEN_EXHAUSTED_FAIL}, {@link #borrowObject} will throw
* a {@link NoSuchElementException}
* </li>
* <li>
* When {@link #setWhenExhaustedAction <i>whenExhaustedAction</i>} is
* {@link #WHEN_EXHAUSTED_GROW}, {@link #borrowObject} will create a new
* object and return it (essentially making {@link #setMaxActive <i>maxActive</i>}
* meaningless.)
* </li>
* <li>
* When {@link #setWhenExhaustedAction <i>whenExhaustedAction</i>}
* is {@link #WHEN_EXHAUSTED_BLOCK}, {@link #borrowObject} will block
* (invoke {@link Object#wait()}) until a new or idle object is available.
* If a positive {@link #setMaxWait <i>maxWait</i>}
* value is supplied, then {@link #borrowObject} will block for at
* most that many milliseconds, after which a {@link NoSuchElementException}
* will be thrown. If {@link #setMaxWait <i>maxWait</i>} is non-positive,
* the {@link #borrowObject} method will block indefinitely.
* </li>
* </ul>
* The default <code>whenExhaustedAction</code> setting is
* {@link #WHEN_EXHAUSTED_BLOCK} and the default <code>maxWait</code>
* setting is -1. By default, therefore, <code>borrowObject</code> will
* block indefinitely until an idle instance becomes available.
* </li>
* <li>
* When {@link #setTestOnBorrow <i>testOnBorrow</i>} is set, the pool will
* attempt to validate each object before it is returned from the
* {@link #borrowObject} method. (Using the provided factory's
* {@link PoolableObjectFactory#validateObject} method.) Objects that fail
* to validate will be dropped from the pool, and a different object will
* be borrowed. The default setting for this parameter is
* <code>false.</code>
* </li>
* <li>
* When {@link #setTestOnReturn <i>testOnReturn</i>} is set, the pool will
* attempt to validate each object before it is returned to the pool in the
* {@link #returnObject} method. (Using the provided factory's
* {@link PoolableObjectFactory#validateObject}
* method.) Objects that fail to validate will be dropped from the pool.
* The default setting for this parameter is <code>false.</code>
* </li>
* </ul>
* <p>
* Optionally, one may configure the pool to examine and possibly evict objects
* as they sit idle in the pool and to ensure that a minimum number of idle
* objects are available. This is performed by an "idle object eviction"
* thread, which runs asynchronously. Caution should be used when configuring
* this optional feature. Eviction runs contend with client threads for access
* to objects in the pool, so if they run too frequently performance issues may
* result. The idle object eviction thread may be configured using the following
* attributes:
* <ul>
* <li>
* {@link #setTimeBetweenEvictionRunsMillis <i>timeBetweenEvictionRunsMillis</i>}
* indicates how long the eviction thread should sleep before "runs" of examining
* idle objects. When non-positive, no eviction thread will be launched. The
* default setting for this parameter is -1 (i.e., idle object eviction is
* disabled by default).
* </li>
* <li>
* {@link #setMinEvictableIdleTimeMillis <i>minEvictableIdleTimeMillis</i>}
* specifies the minimum amount of time that an object may sit idle in the pool
* before it is eligible for eviction due to idle time. When non-positive, no object
* will be dropped from the pool due to idle time alone. This setting has no
* effect unless <code>timeBetweenEvictionRunsMillis > 0.</code> The default
* setting for this parameter is 30 minutes.
* </li>
* <li>
* {@link #setTestWhileIdle <i>testWhileIdle</i>} indicates whether or not idle
* objects should be validated using the factory's
* {@link PoolableObjectFactory#validateObject} method. Objects that fail to
* validate will be dropped from the pool. This setting has no effect unless
* <code>timeBetweenEvictionRunsMillis > 0.</code> The default setting for
* this parameter is <code>false.</code>
* </li>
* <li>
* {@link #setSoftMinEvictableIdleTimeMillis <i>softMinEvictableIdleTimeMillis</i>}
* specifies the minimum amount of time an object may sit idle in the pool
* before it is eligible for eviction by the idle object evictor
* (if any), with the extra condition that at least "minIdle" object instances
* remain in the pool. When non-positive, no objects will be evicted from the pool
* due to idle time alone. This setting has no effect unless
* <code>timeBetweenEvictionRunsMillis > 0.</code> and it is superceded by
* {@link #setMinEvictableIdleTimeMillis <i>minEvictableIdleTimeMillis</i>}
* (that is, if <code>minEvictableIdleTimeMillis</code> is positive, then
* <code>softMinEvictableIdleTimeMillis</code> is ignored). The default setting for
* this parameter is -1 (disabled).
* </li>
* <li>
* {@link #setNumTestsPerEvictionRun <i>numTestsPerEvictionRun</i>}
* determines the number of objects examined in each run of the idle object
* evictor. This setting has no effect unless
* <code>timeBetweenEvictionRunsMillis > 0.</code> The default setting for
* this parameter is 3.
* </li>
* </ul>
* <p>
* <p>
* The pool can be configured to behave as a LIFO queue with respect to idle
* objects - always returning the most recently used object from the pool,
* or as a FIFO queue, where borrowObject always returns the oldest object
* in the idle object pool.
* <ul>
* <li>
* {@link #setLifo <i>lifo</i>}
* determines whether or not the pool returns idle objects in
* last-in-first-out order. The default setting for this parameter is
* <code>true.</code>
* </li>
* </ul>
* <p>
* GenericObjectPool is not usable without a {@link PoolableObjectFactory}. A
* non-<code>null</code> factory must be provided either as a constructor argument
* or via a call to {@link #setFactory} before the pool is used.
* <p>