最近看到有人在使用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 GenericObjectPool provides a number of configurable parameters:
*
*
* {@link #setMaxActive maxActive} 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 maxActive} is reached, the pool is said
* to be exhausted. The default setting for this parameter is 8.
*
*
* {@link #setMaxIdle maxIdle} 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.
*
*
* {@link #setWhenExhaustedAction whenExhaustedAction} specifies the
* behavior of the {@link #borrowObject} method when the pool is exhausted:
*
*
* When {@link #setWhenExhaustedAction whenExhaustedAction} is
* {@link #WHEN_EXHAUSTED_FAIL}, {@link #borrowObject} will throw
* a {@link NoSuchElementException}
*
*
* When {@link #setWhenExhaustedAction whenExhaustedAction} is
* {@link #WHEN_EXHAUSTED_GROW}, {@link #borrowObject} will create a new
* object and return it (essentially making {@link #setMaxActive maxActive}
* meaningless.)
*
*
* When {@link #setWhenExhaustedAction whenExhaustedAction}
* 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 maxWait}
* 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 maxWait} is non-positive,
* the {@link #borrowObject} method will block indefinitely.
*
*
* The default whenExhaustedAction
setting is
* {@link #WHEN_EXHAUSTED_BLOCK} and the default maxWait
* setting is -1. By default, therefore, borrowObject
will
* block indefinitely until an idle instance becomes available.
*
*
* When {@link #setTestOnBorrow testOnBorrow} 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
* false.
*
*
* When {@link #setTestOnReturn testOnReturn} 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 false.
*
*
*
* 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:
*
*
* {@link #setTimeBetweenEvictionRunsMillis timeBetweenEvictionRunsMillis}
* 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).
*
*
* {@link #setMinEvictableIdleTimeMillis minEvictableIdleTimeMillis}
* 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 timeBetweenEvictionRunsMillis > 0.
The default
* setting for this parameter is 30 minutes.
*
*
* {@link #setTestWhileIdle testWhileIdle} 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
* timeBetweenEvictionRunsMillis > 0.
The default setting for
* this parameter is false.
*
*
* {@link #setSoftMinEvictableIdleTimeMillis softMinEvictableIdleTimeMillis}
* 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
* timeBetweenEvictionRunsMillis > 0.
and it is superceded by
* {@link #setMinEvictableIdleTimeMillis minEvictableIdleTimeMillis}
* (that is, if minEvictableIdleTimeMillis
is positive, then
* softMinEvictableIdleTimeMillis
is ignored). The default setting for
* this parameter is -1 (disabled).
*
*
* {@link #setNumTestsPerEvictionRun numTestsPerEvictionRun}
* determines the number of objects examined in each run of the idle object
* evictor. This setting has no effect unless
* timeBetweenEvictionRunsMillis > 0.
The default setting for
* this parameter is 3.
*
*
*
*
* 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.
*
*
* {@link #setLifo lifo}
* determines whether or not the pool returns idle objects in
* last-in-first-out order. The default setting for this parameter is
* true.
*
*
*
* GenericObjectPool is not usable without a {@link PoolableObjectFactory}. A
* non-null
factory must be provided either as a constructor argument
* or via a call to {@link #setFactory} before the pool is used.
*