jedis学习笔记【2】——连接池

  • jar版本:
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.8.1</version>
</dependency>

2.8.1版本jedis内依赖2.4.2的commons-pool2.jar

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
    <version>2.4.2</version>
    <type>jar</type>
    <scope>compile</scope>
</dependency>

  • 连接池定义:
JedisPool jedisPool = new JedisPool();

JedisPool的构造函数有多个入参:

public JedisPool(GenericObjectPoolConfig poolConfig, URI uri, int connectionTimeout, int soTimeout) {
        super(poolConfig, new JedisFactory(uri, connectionTimeout, soTimeout, (String)null));
    }

根本为GenericObjectPoolConfig 和 JedisFactory这两个类

public class GenericObjectPoolConfig extends BaseObjectPoolConfig {
    public static final int DEFAULT_MAX_TOTAL = 8;
    public static final int DEFAULT_MAX_IDLE = 8;
    public static final int DEFAULT_MIN_IDLE = 0;
    private int maxTotal = 8;//最大连接数
    private int maxIdle = 8;//最大空闲数,空闲连接大于这个数会进行回收
    private int minIdle = 0;//最小空闲数,空闲连接小于这个数会建立新的连接

    ...
    }

//GenericObjectPoolConfig 的父类
public abstract class BaseObjectPoolConfig implements Cloneable {
    private boolean lifo = true;//连接池放池化对象方式(true:放在空闲队列最前面,false:放在空闲队列最后面)
    private boolean fairness = false;//等待线程拿空闲连接的方式(true:相当于等待线程是在先进先出去拿空闲连接)
    private long maxWaitMillis = -1L;//当连接池资源耗尽时,调用者最大阻塞的时间,超时将跑出异常。单位,毫秒数;默认为-1.表示永不超时. 默认值 -1
    private long minEvictableIdleTimeMillis = 1800000L;//连接空闲的最小时间,达到此值后空闲连接将可能会被移除。负值(-1)表示不移除;
    private long softMinEvictableIdleTimeMillis = 1800000L;//连接空闲的最小时间,达到此值后空闲链接将会被移除,且保留“minIdle”个空闲连接数。负值(-1)表示不移除。
    private int numTestsPerEvictionRun = 3;//对于“空闲链接”检测线程而言,每次检测的链接资源的个数。
    private String evictionPolicyClassName = "org.apache.commons.pool2.impl.DefaultEvictionPolicy";
    private boolean testOnCreate = false;
    private boolean testOnBorrow = false;//向调用者输出“链接”资源时,是否检测是有有效,如果无效则从连接池中移除,并尝试获取继续获取。默认为false。建议保持默认值.
    private boolean testOnReturn = false;
    private boolean testWhileIdle = false;//向调用者输出“链接”对象时,是否检测它的空闲超时;默认为false。如果“链接”空闲超时,将会被移除;建议保持默认值。默认值false
    private long timeBetweenEvictionRunsMillis = -1L;//“空闲链接”检测线程,检测的周期,毫秒数。如果为负值,表示不运行“检测线程”。默认值 -1L
    private boolean blockWhenExhausted = true;
    private boolean jmxEnabled = true;
    private String jmxNamePrefix = "pool";
    private String jmxNameBase;
class JedisFactory implements PooledObjectFactory<Jedis> {
    private final AtomicReference<HostAndPort> hostAndPort = new AtomicReference();
    private final int connectionTimeout;
    private final int soTimeout;
    private final String password;
    private final int database;
    private final String clientName;

    public JedisFactory(String host, int port, int connectionTimeout, int soTimeout, String password, int database, String clientName) {
        this.hostAndPort.set(new HostAndPort(host, port));//服务器地址和端口(默认localhost和6379)
        this.connectionTimeout = connectionTimeout;//连接超时(默认2000ms)
        this.soTimeout = soTimeout;//响应超时(默认2000ms)
        this.password = password;//密码(默认null)
        this.database = database;//数据库(默认0)
        this.clientName = clientName;//客户端名称(默认null)
    }
}

  • 简单连接池示例:
    JedisPool jedisPool = new JedisPool();
    //获取连接
    Jedis jedis = jedisPool.getResource();
    //操作
    jedis.set("a","a");
    jedis.get("a");
    //关闭连接
    jedis.close();

参考链接:
Redis入门很简单之四【初识Jedis】

Apache Commons-pool2(整理)

猜你喜欢

转载自blog.csdn.net/yx1214442120/article/details/51957722