我们通常在使用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. Bootstrap3基础 栅格系统 col-lg/md/sm/xs-* 简单示例

      内容 参数   OS   Windows 10 x64   browser   Firefox 65.0.2   framework     Bootstrap 3.3.7   editor    ...

  2. LaTex 使用特殊章节符号 (§)

    参考: LaTex 使用特殊章节符号 (§) LaTex 使用特殊章节符号 (§) 在.tex文件开头,加上以下内容: \usepackage[utf8]{inputenc} \usepackage{ ...

  3. 访问github慢的解决方案

    问题描述 打开https://github.com很慢. 解决办法 1.打开文件C:\Windows\System32\drivers\etc\hosts 2.添加如下内容: 151.101.44.2 ...

  4. EF的优缺点

    优点: 1.简洁的Linq to Sql语句大大提高了开发人员的效率,不要再写复杂的sql语句: 2.不再需要再管应用程序如何去连接数据库: 3.EF可以用作用于数据服务和OData Service的 ...

  5. 渐变UI

    1.h #import <UIKit/UIKit.h> @interface UIView (Gradient) /* The array of CGColorRef objects de ...

  6. 练习markdown语法

    这是一级标题 这是二级标题 这是三级标题 -列表试验 -据说这样无编号 编号文档 编号文档 编号文档 插入链接测试 插入图片测试 引用测试> 一蓑烟雨任平生 粗体测试我是加粗的 斜体测试我是斜体 ...

  7. [Linux]ubuntu安装基本流程

    ubuntu安装基本流程 1.设置分辨率2.设置语言环境3.设置服务器镜像源4.添加终端5.apt.apt-get更新和升级系统软件 sudo apt update/upgrade sudo apt- ...

  8. 接口测试工具postman

    一. 安装 1. 免费官网链接:https://www.getpostman.com/postman,下载好后双击.exe程序安装即可 2. 注意事项:建议安装在非系统盘,即C盘以外的盘,路径最好用全 ...

  9. BP neural network optimized by PSO algorithm on Ammunition storage reliability prediction 阅读笔记

    1.BP neural network optimized by PSO algorithm on Ammunition storage reliability prediction 文献简介文献来源 ...

  10. 在jsp中如何使用javax.servlet.http.HttpServlet,javax.servlet.GenericServlet, javax.servlet.Servlet