jedis调用redis之pool

package com.alex.xiyoubang;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class PoolUtil {

    private static JedisPool pool = null;


    public static JedisPool getPool() {
        if (pool == null) {
            JedisPoolConfig config = new JedisPoolConfig();
            //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例。
            config.setMaxIdle(1);
            config.setMaxTotal(20);
            //表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException;
            config.setMaxWaitMillis(6000);
            //在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
            config.setTestOnBorrow(true);
            pool = new JedisPool(config, "192.168.1.171", 6379);
        }
        return pool;
    }


    public static void returnResource(JedisPool pool, Jedis redis) {
        if (redis != null) {
            pool.returnResource(redis);
        }
    }


    public static String get(String key){
        String value = null;

        JedisPool pool = null;
        Jedis jedis = null;
        try {
            pool = getPool();
            jedis = pool.getResource();
            value = jedis.get(key);
        } catch (Exception e) {
            //释放redis对象
            pool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            //返还到连接池
            returnResource(pool, jedis);
        }

        return value;
    }


    public static String set(String key,String val){

        String value = null;
        JedisPool pool = null;
        Jedis jedis = null;
        try {
            pool = getPool();
            jedis = pool.getResource();
            jedis.flushDB();
            value = jedis.set(key,val);
        } catch (Exception e) {
            //释放redis对象
            pool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            //返还到连接池
            returnResource(pool, jedis);
        }

        return value;
    }

    public static void main(String[] args) {
        set("name", "Alex");
        System.out.println(get("name"));
    }
}

JedisPoolConfig源码属性

public class JedisPoolConfig extends GenericObjectPoolConfig {

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

public class GenericObjectPoolConfig extends BaseObjectPoolConfig {

    public GenericObjectPoolConfig()
    {
        maxTotal = 8;
        maxIdle = 8;
        minIdle = 0;
    }

    public int getMaxTotal()
    {
        return maxTotal;
    }

    public void setMaxTotal(int maxTotal)
    {
        this.maxTotal = maxTotal;
    }

    public int getMaxIdle()
    {
        return maxIdle;
    }

    public void setMaxIdle(int maxIdle)
    {
        this.maxIdle = maxIdle;
    }

    public int getMinIdle()
    {
        return minIdle;
    }

    public void setMinIdle(int minIdle)
    {
        this.minIdle = minIdle;
    }

    public GenericObjectPoolConfig clone()
    {
        return (GenericObjectPoolConfig)super.clone();
        CloneNotSupportedException e;
        e;
        throw new AssertionError();
    }

    public volatile Object clone()
        throws CloneNotSupportedException
    {
        return clone();
    }

    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;
}


public abstract class BaseObjectPoolConfig implements Cloneable {

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

    public boolean getLifo()
    {
        return lifo;
    }

    public void setLifo(boolean lifo)
    {
        this.lifo = lifo;
    }

    public long getMaxWaitMillis()
    {
        return maxWaitMillis;
    }

    public void setMaxWaitMillis(long maxWaitMillis)
    {
        this.maxWaitMillis = maxWaitMillis;
    }

    public long getMinEvictableIdleTimeMillis()
    {
        return minEvictableIdleTimeMillis;
    }

    public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis)
    {
        this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
    }

    public long getSoftMinEvictableIdleTimeMillis()
    {
        return softMinEvictableIdleTimeMillis;
    }

    public void setSoftMinEvictableIdleTimeMillis(long softMinEvictableIdleTimeMillis)
    {
        this.softMinEvictableIdleTimeMillis = softMinEvictableIdleTimeMillis;
    }

    public int getNumTestsPerEvictionRun()
    {
        return numTestsPerEvictionRun;
    }

    public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun)
    {
        this.numTestsPerEvictionRun = numTestsPerEvictionRun;
    }

    public boolean getTestOnBorrow()
    {
        return testOnBorrow;
    }

    public void setTestOnBorrow(boolean testOnBorrow)
    {
        this.testOnBorrow = testOnBorrow;
    }

    public boolean getTestOnReturn()
    {
        return testOnReturn;
    }

    public void setTestOnReturn(boolean testOnReturn)
    {
        this.testOnReturn = testOnReturn;
    }

    public boolean getTestWhileIdle()
    {
        return testWhileIdle;
    }

    public void setTestWhileIdle(boolean testWhileIdle)
    {
        this.testWhileIdle = testWhileIdle;
    }

    public long getTimeBetweenEvictionRunsMillis()
    {
        return timeBetweenEvictionRunsMillis;
    }

    public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis)
    {
        this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
    }

    public String getEvictionPolicyClassName()
    {
        return evictionPolicyClassName;
    }

    public void setEvictionPolicyClassName(String evictionPolicyClassName)
    {
        this.evictionPolicyClassName = evictionPolicyClassName;
    }

    public boolean getBlockWhenExhausted()
    {
        return blockWhenExhausted;
    }

    public void setBlockWhenExhausted(boolean blockWhenExhausted)
    {
        this.blockWhenExhausted = blockWhenExhausted;
    }

    public boolean getJmxEnabled()
    {
        return jmxEnabled;
    }

    public void setJmxEnabled(boolean jmxEnabled)
    {
        this.jmxEnabled = jmxEnabled;
    }

    public String getJmxNamePrefix()
    {
        return jmxNamePrefix;
    }

    public void setJmxNamePrefix(String jmxNamePrefix)
    {
        this.jmxNamePrefix = jmxNamePrefix;
    }

    public static final boolean DEFAULT_LIFO = true;
    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_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_EVICTION_POLICY_CLASS_NAME = "org.apache.commons.pool2.impl.DefaultEvictionPolicy";
    private boolean lifo;
    private long maxWaitMillis;
    private long minEvictableIdleTimeMillis;
    private long softMinEvictableIdleTimeMillis;
    private int numTestsPerEvictionRun;
    private String evictionPolicyClassName;
    private boolean testOnBorrow;
    private boolean testOnReturn;
    private boolean testWhileIdle;
    private long timeBetweenEvictionRunsMillis;
    private boolean blockWhenExhausted;
    private boolean jmxEnabled;
    private String jmxNamePrefix;
}

你可能感兴趣的:(redis,源码,jedis,Class,poor)