Today I found that the default connection method of Jedis is jedis=new Jedis("localhost",6379), and connection timeout always occurs. Later I found that the jedis class package also has a way to set the maximum connection time.
1->Getting a Jedis instance needs to be obtained from JedisPool;
2->After using up the Jedis instance, you need to return it to JedisPool;
3->If Jedis makes an error during use, it also needs to be returned to JedisPool;
The code is as follows
Copy the code code as follows:
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(100);
config.setMaxIdle(20);
config.setMaxWait(1000l);
JedisPool pool;
pool = new JedisPool(config, "2xx.xx.xx.14", 6379);
boolean borrowOrOprSuccess = true;
try {
jedis = pool.getResource();
// do redis opt by instance
} catch (JedisConnectionException e) {
borrowOrOprSuccess = false;
if (jedis != null)
pool.returnBrokenResource(jedis);
} finally {
if (borrowOrOprSuccess)
pool.returnResource(jedis);
}
jedis = pool.getResource();
JedisPool depends on apache class package
commons-pool-1.5.6.jar
1->Although JedisConnectionException is thrown, there are actually two types of errors. One is pool.getResource(), which cannot get an available jedis instance; the other is that errors during jedis.set/get will also throw this Exception. ;In order to realize the distinction, it is implemented based on whether the instance is null. If it is null, it proves that the instance has not been initialized at all, so there is no need to return it to the pool; if the instance is not null, it proves that it needs to be returned to the pool;
2->When an instance error occurs, returnBrokenResource must also be called to return it to the pool. Otherwise, there may still be data in the buffer of the instance obtained through getResource next time, and a problem may occur!
The configuration parameters of JedisPool largely depend on actual application requirements and software and hardware capabilities. I have never used commons-pool before, so this time I spent a whole room looking at the meaning of these parameters. . . Most of the configuration parameters of JedisPool are assigned by the corresponding items of JedisPoolConfig.
maxActive: Controls how many jedis instances a pool can be allocated, obtained through pool.getResource(); if the value is -1, it means there is no limit; if the pool has allocated maxActive jedis instances, the status of the pool at this time becomes Exhausted, in JedisPoolConfig
maxIdle: Controls the maximum number of jedis instances in the idle state that a pool can have;
whenExhaustedAction: Indicates the action to be taken by the pool when all jedis instances in the pool have been allocated; there are three types of WHEN_EXHAUSTED_FAIL by default (which means that when there is no jedis instance, it will be thrown directly
NoSuchElementException), WHEN_EXHAUSTED_BLOCK (means it is blocked, or a JedisConnectionException is thrown when maxWait is reached), WHEN_EXHAUSTED_GROW (means a new jedis instance is created, which means that the set maxActive is useless);
maxWait: indicates the maximum waiting time when borrowing a Jedis instance. If the waiting time is exceeded, JedisConnectionException will be thrown directly;
testOnBorrow: When borrowing a jedis instance, whether to perform the Alidate operation in advance; if true, the obtained jedis instances are available;
testOnReturn: whether to perform the validate operation in advance when returning to the pool;
testWhileIdle: If true, it means that there is an idle object evitor thread scanning the idle object. If the validate fails, the object will be dropped from the pool; this item is only meaningful when timeBetweenEvictionRunsMillis is greater than 0;
timeBetweenEvictionRunsMillis: indicates the number of milliseconds that the idle object evitor needs to sleep between two scans;
numTestsPerEvictionRun: Indicates the maximum number of objects scanned by the idle object evitor each time;
minEvictableIdleTimeMillis: Indicates the minimum time for an object to stay in the idle state at least before it can be scanned and evicted by the idle object evitor; this item is only meaningful when timeBetweenEvictionRunsMillis is greater than 0;
softMinEvictableIdleTimeMillis: Based on minEvictableIdleTimeMillis, at least minIdle objects are added to the pool. If -1, evicted will not evict any objects based on idle time. If minEvictableIdleTimeMillis>0, this setting is meaningless, and it is only meaningful when timeBetweenEvictionRunsMillis is greater than 0;
lifo: When borrowObject returns an object, DEFAULT_LIFO (last in first out, the most frequently used queue similar to cache) is used. If it is False, it means FIFO queue;
The default settings of JedisPoolConfig for some parameters are as follows:
testWhileIdle=true
minEvictableIdleTimeMills=60000
timeBetweenEvictionRunsMillis=30000
numTestsPerEvictionRun=-1