我们通常在使用JedisPoolConfig进行连接池配置的时候,minEvictableIdleTimeMillis和softMinEvictableIdleTimeMillis这两个参数经常会不懂其含义,
查各种资料也没有非常明确的说到底该如何设置,即使知道如何设置,也不知道其原理,只知道这两个参数是和逐出线程有关的。下面根据源码进行探索。
我们通常是通过JedisPool构造线程池,追溯其父类的创建过程,发现Pool<T>这个泛型类的构造方法调用过程如下:

public Pool(GenericObjectPoolConfig poolConfig, PooledObjectFactory<T> factory) {
this.initPool(poolConfig, factory);
} public void initPool(GenericObjectPoolConfig poolConfig, PooledObjectFactory<T> factory) {
if(this.internalPool != null) {
try {
this.closeInternalPool();
} catch (Exception var4) {
;
}
} this.internalPool = new GenericObjectPool(factory, poolConfig);
}

发现其创建了一个GenericObjectPool对象,构造方法如下:

public GenericObjectPool(PooledObjectFactory<T> factory, GenericObjectPoolConfig config) {
super(config, "org.apache.commons.pool2:type=GenericObjectPool,name=", config.getJmxNamePrefix());
this.factoryType = null;
this.maxIdle = 8;
this.minIdle = 0;
this.allObjects = new ConcurrentHashMap();
this.createCount = new AtomicLong(0L);
this.abandonedConfig = null;
if(factory == null) {
this.jmxUnregister();
throw new IllegalArgumentException("factory may not be null");
} else {
this.factory = factory;
this.idleObjects = new LinkedBlockingDeque(config.getFairness());
this.setConfig(config);
this.startEvictor(this.getTimeBetweenEvictionRunsMillis());
}
}

其中this.startEvictor(this.getTimeBetweenEvictionRunsMillis());方法的调用,正是开启逐出线程运行的作用,

我们可以发现,源码通过周期性的调度逐出任务(timeBetweenEvictionRunsMillis大于0时),将空闲的连接逐出线程池。

final void startEvictor(long delay) {
Object var3 = this.evictionLock;
synchronized(this.evictionLock) {
if(null != this.evictor) {
EvictionTimer.cancel(this.evictor);
this.evictor = null;
this.evictionIterator = null;
} if(delay > 0L) {
this.evictor = new BaseGenericObjectPool.Evictor();
EvictionTimer.schedule(this.evictor, delay, delay);
} }
}

下面将是我们今天研究的重点,this.evictor。

逐出有逐出策略,如果不配置则采用默认的逐出策略DefaultEvictionPolicy,其中的evict方法返回true时才执行逐出的操作

public class DefaultEvictionPolicy<T> implements EvictionPolicy<T> {
public DefaultEvictionPolicy() {
} public boolean evict(EvictionConfig config, PooledObject<T> underTest, int idleCount) {
return config.getIdleSoftEvictTime() < underTest.getIdleTimeMillis() && config.getMinIdle() < idleCount || config.getIdleEvictTime() < underTest.getIdleTimeMillis();
}
}

真正的逐出方法执行的是以下内容

public void evict() throws Exception {
this.assertOpen();
if(this.idleObjects.size() > 0) {
PooledObject<T> underTest = null;
EvictionPolicy<T> evictionPolicy = this.getEvictionPolicy();
Object var3 = this.evictionLock;
synchronized(this.evictionLock) {
EvictionConfig evictionConfig = new EvictionConfig(this.getMinEvictableIdleTimeMillis(), this.getSoftMinEvictableIdleTimeMillis(), this.getMinIdle());
boolean testWhileIdle = this.getTestWhileIdle();
int i = 0;
int m = this.getNumTests(); while(true) {
if(i >= m) {
break;
} if(this.evictionIterator == null || !this.evictionIterator.hasNext()) {
this.evictionIterator = new EvictionIterator(this, this.idleObjects);
} if(!this.evictionIterator.hasNext()) {
return;
} label81: {
try {
underTest = this.evictionIterator.next();
} catch (NoSuchElementException var15) {
--i;
this.evictionIterator = null;
break label81;
} if(!underTest.startEvictionTest()) {
--i;
} else {
boolean evict;
try {
evict = evictionPolicy.evict(evictionConfig, underTest, this.idleObjects.size());
} catch (Throwable var14) {
PoolUtils.checkRethrow(var14);
this.swallowException(new Exception(var14));
evict = false;
} if(evict) {
this.destroy(underTest);
this.destroyedByEvictorCount.incrementAndGet();
} else {
if(testWhileIdle) {
boolean active = false; try {
this.factory.activateObject(underTest);
active = true;
} catch (Exception var13) {
this.destroy(underTest);
this.destroyedByEvictorCount.incrementAndGet();
} if(active) {
if(!this.factory.validateObject(underTest)) {
this.destroy(underTest);
this.destroyedByEvictorCount.incrementAndGet();
} else {
try {
this.factory.passivateObject(underTest);
} catch (Exception var12) {
this.destroy(underTest);
this.destroyedByEvictorCount.incrementAndGet();
}
}
}
} if(!underTest.endEvictionTest(this.idleObjects)) {
;
}
}
}
} ++i;
}
}
} AbandonedConfig ac = this.abandonedConfig;
if(ac != null && ac.getRemoveAbandonedOnMaintenance()) {
this.removeAbandoned(ac);
} }

我们重点看两行代码,第8行是创建了逐出配置,根据你配置的minEvictableIdleTimeMillis和softMinEvictableIdleTimeMillis,如果存在负数,则设为long类型的最大值。

public EvictionConfig(long poolIdleEvictTime, long poolIdleSoftEvictTime, int minIdle) {
if(poolIdleEvictTime > 0L) {
this.idleEvictTime = poolIdleEvictTime;
} else {
this.idleEvictTime = 9223372036854775807L;
} if(poolIdleSoftEvictTime > 0L) {
this.idleSoftEvictTime = poolIdleSoftEvictTime;
} else {
this.idleSoftEvictTime = 9223372036854775807L;
} this.minIdle = minIdle;
}

再看第40行代码,再结合DefaultEvictionPolicy的evict方法,我们可以看到,真正的逐出依据是:

1.连接空闲时间大于softMinEvictableIdleTimeMillis并且当前连接池的空闲连接数大于最小空闲连接数minIdle;

2.连接空闲时间大于minEvictableIdleTimeMillis。

1或者2成立即可逐出,注意是或的关系。

所以,结论如下:

如果要连接池只根据softMinEvictableIdleTimeMillis进程逐出,那么需要将minEvictableIdleTimeMillis设置为负数(即最大值);
如果要连接池只根据minEvictableIdleTimeMillis进程逐出,那么需要将softMinEvictableIdleTimeMillis设置为负数(即最大值),理论上设置minIdle很大也是可以的,但是实际上不行;

jedis连接池参数minEvictableIdleTimeMillis和softMinEvictableIdleTimeMillis探索的更多相关文章

  1. Jedis连接池

    jedis是官方首选的java客户端开发包 Redis不仅是使用命令来操作,现在基本上主流的语言都有客户端支持,比如java.C.C#.C++.php.Node.js.Go等. 在官方网站里列一些Ja ...

  2. 详解Jedis连接池报错处理

    在使用Jedis连接池模式下,比较常见的报错如下: redis.clients.jedis.exceptions.JedisConnectionException:Could not get a re ...

  3. 为什么要用Jedis连接池+浅谈jedis连接池使用

    为什么要使用Jedis连接池 Redis作为缓存数据库理论上和MySQL一样需要客户端和服务端建立起来连接进行相关操作,使用MySQL的时候相信大家都会使用一款开源的连接池,例如C3P0.因为直连会消 ...

  4. Java Redis系列3(Jedis的使用+jedis连接池技术)

    Jedis的使用 什么是Jedis? 一款Java操作redis数据库的工具 使用步骤 1.下载redis所需的java包 2.使用步骤 import org.junit.Test; public c ...

  5. Java与redis交互、Jedis连接池JedisPool

    Java与redis交互比较常用的是Jedis. 先导入jar包: commons-pool2-2.3.jar jedis-2.7.0.jar 基本使用: public class RedisTest ...

  6. C3P0连接池参数配置说明

    C3P0连接池参数配置说明 created by cjk on 2017.8.15 常用配置 initialPoolSize:连接池初始化时创建的连接数,default : 3(建议使用) minPo ...

  7. Spring Boot为我们准备了最佳的数据库连接池方案,只需要在属性文件(例如application.properties)中配置需要的连接池参数即可。

    Spring Boot为我们准备了最佳的数据库连接池方案,只需要在属性文件(例如application.properties)中配置需要的连接池参数即可.

  8. Jedis与Jedis连接池

    1.Jedis简介 实际开发中,我们需要用Redis的连接工具连接Redis然后操作Redis, 对于主流语言,Redis都提供了对应的客户端: https://redis.io/clients 2. ...

  9. 三、redis学习(jedis连接池)

    一.jedis连接池 二.jedis连接池+config配置文件 三.jedis连接池+config配置文件+util工具类 util类 public class JedisPoolUtils { / ...

随机推荐

  1. Linux根目录下各个目录的用途及含义

    Linux根目录下各个目录的用途及含义 Linux ./bin 重要的二进制 (binary) 应用程序,包含二进制文件,系统的所有用户使用的命令都在这个目录下. ./boot 启动 (boot) 配 ...

  2. OpenVPN部署,实现访问云服务器的内网

    本教程不描述如何FQ 一.OpenVPN服务端部署 $ yum -y install net-tools lzo lzo-devel openssl-devel pam-devel gcc gcc-c ...

  3. out对象

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  4. oledb

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data; ...

  5. “妄”眼欲穿之CSS 居中问题

    妄:狂妄: 不会的东西只有怀着一颗狂妄的心,假装能把它看穿吧. 作为一个什么都不会的小白,为了学习,特别在拿来主义之后写一些对于某些css布局的总结,进一步加深对知识的记忆.知识是人类的共同财富,中华 ...

  6. Vuex状态管理模式

    Store:类似容器,包含应用的大部分状态,一个页面只能有一个store,状态存储是响应式的 State : 包含所有应用级别状态的对象 Getters : 在组件内部获取store中状态的函数 Mu ...

  7. 百度ueditor上传图片时如何设置默认宽高度

    百度ueditor上传图片时如何设置默认宽高度 一.总结 一句话总结:直接css或者js里面限制一下就好,可以用html全局限制一下图片的最大高度 直接css或者js里面限制一下就好,可以用html全 ...

  8. ggplot的boxplot添加显著性 | Add P-values and Significance Levels to ggplots | 方差分析

    参考:Add P-values and Significance Levels toggplots 多组比较,挑选感兴趣的显示显著性. data("ToothGrowth") he ...

  9. node中 path.resolve 和path.join的区别

    path.resolve('a','b','c') 返回a/b/c path.resolve理论上总是以前一个路径作为基础路径,然后匹配当前路径,当前路径会有三种情况根目录(/),当前目录(./),上 ...

  10. vue-cli(vue脚手架) 简单使用

    1.vue-cli 作用 vue-cli作为vue的脚手架,可以帮助我们在实际开发中自动生成vue.js的模板工程. 2.vue-cli 使用 a. 安装全局vue-cli npm install v ...