Redis教程 —— JedisPoolConfig配置

JedisPoolConfig参数配置详情

JedisPoolConfig config = new JedisPoolConfig();
//连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
config.setBlockWhenExhausted(true);
//设置的逐出策略类名, 默认DefaultEvictionPolicy(当连接超过最大空闲时间,或连接数超过最大空闲连接数)
config.setEvictionPolicyClassName("org.apache.commons.pool2.impl.DefaultEvictionPolicy");
//是否启用pool的jmx管理功能, 默认true
config.setJmxEnabled(true);
//MBean ObjectName = new ObjectName("org.apache.commons.pool2:type=GenericObjectPool,name=" + "pool" + i); 默认为"pool",
config.setJmxNamePrefix("pool");
//是否启用后进先出, 默认true
config.setLifo(true);
//最大空闲连接数, 默认8个
config.setMaxIdle(8);
//最大连接数, 默认8个
config.setMaxTotal(8);
//获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间, 默认-1
config.setMaxWaitMillis(-1);
//逐出连接的最小空闲时间 默认1800000毫秒(30分钟)
config.setMinEvictableIdleTimeMillis(1800000);
//最小空闲连接数, 默认0
config.setMinIdle(0);
//每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3
config.setNumTestsPerEvictionRun(3);
//对象空闲多久后逐出, 当空闲时间>该值 且 空闲连接>最大空闲数 时直接逐出,不再根据MinEvictableIdleTimeMillis判断(默认逐出策略)
config.setSoftMinEvictableIdleTimeMillis(1800000);
//在获取连接的时候检查有效性, 默认false
config.setTestOnBorrow(false);
//在空闲时检查有效性, 默认false
config.setTestWhileIdle(false);
//逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1
config.setTimeBetweenEvictionRunsMillis(-1);
JedisPool pool = new JedisPool(config, "localhost",);
int timeout=3000;
new JedisSentinelPool(master, sentinels, poolConfig,timeout);//timeout 读取超时


查看JedisPoolConfig 源代码可以看出继承关系和初始值状态

package redis.clients.jedis;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

public class JedisPoolConfig extends GenericObjectPoolConfig {

    public JedisPoolConfig() {
        setTestWhileIdle(true);
        setMinEvictableIdleTimeMillis(60000L);
        setTimeBetweenEvictionRunsMillis(30000L);
        setNumTestsPerEvictionRun(-1);
    }
}

JedisPoolConfig 继承BaseObjectPoolConfig

package org.apache.commons.pool2.impl;

public class GenericObjectPoolConfig extends BaseObjectPoolConfig {

    public GenericObjectPoolConfig() {
        maxTotal = 8;
        maxIdle = 8;
        minIdle = 0;
    }
        
        // 省略部分代码...

    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;
    private int maxIdle;
    private int minIdle;
}

BaseObjectPoolConfig继承BaseObjectPoolConfig

package org.apache.commons.pool2.impl;

public abstract class BaseObjectPoolConfig implements Cloneable {

    public BaseObjectPoolConfig() {
        lifo = true;
        fairness = false;
        maxWaitMillis = -1L;
        minEvictableIdleTimeMillis = 1800000L;
        softMinEvictableIdleTimeMillis = 1800000L;
        numTestsPerEvictionRun = 3;
        evictionPolicyClassName = "org.apache.commons.pool2.impl.DefaultEvictionPolicy";
        testOnCreate = false;
        testOnBorrow = false;
        testOnReturn = false;
        testWhileIdle = false;
        timeBetweenEvictionRunsMillis = -1L;
        blockWhenExhausted = true;
        jmxEnabled = true;
        jmxNamePrefix = "pool";
        jmxNameBase = "pool";
    }

    //省略部分代码...

    public static final boolean DEFAULT_LIFO = true;
    public static final boolean DEFAULT_FAIRNESS = false;
    public static final long DEFAULT_MAX_WAIT_MILLIS = -1L;
    public static final long DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS = 1800000L;
    public static final long DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS = -1L;
    public static final int DEFAULT_NUM_TESTS_PER_EVICTION_RUN = 3;
    public static final boolean DEFAULT_TEST_ON_CREATE = false;
    public static final boolean DEFAULT_TEST_ON_BORROW = false;
    public static final boolean DEFAULT_TEST_ON_RETURN = false;
    public static final boolean DEFAULT_TEST_WHILE_IDLE = false;
    public static final long DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS = -1L;
    public static final boolean DEFAULT_BLOCK_WHEN_EXHAUSTED = true;
    public static final boolean DEFAULT_JMX_ENABLE = true;
    public static final String DEFAULT_JMX_NAME_PREFIX = "pool";
    public static final String DEFAULT_JMX_NAME_BASE = null;
    public static final String DEFAULT_EVICTION_POLICY_CLASS_NAME = "org.apache.commons.pool2.impl.DefaultEvictionPolicy";
    private boolean lifo;
    private boolean fairness;
    private long maxWaitMillis;
    private long minEvictableIdleTimeMillis;
    private long softMinEvictableIdleTimeMillis;
    private int numTestsPerEvictionRun;
    private String evictionPolicyClassName;
    private boolean testOnCreate;
    private boolean testOnBorrow;
    private boolean testOnReturn;
    private boolean testWhileIdle;
    private long timeBetweenEvictionRunsMillis;
    private boolean blockWhenExhausted;
    private boolean jmxEnabled;
    private String jmxNamePrefix;
    private String jmxNameBase;

}


未经允许请勿转载:程序喵 » Redis教程 —— JedisPoolConfig配置

点  赞 (0) 打  赏
分享到: