Basic use of Jedis connection pool

jedis direct

Each operation will create a jedis object. After the execution, close the connection and release it. The corresponding is a Tcp connection.

jedis connection pool

A batch of jedis connection objects are generated in advance and put into the connection pool. When it is necessary to operate redis, the jedis objects are borrowed from the connection pool and returned after the operation. In this way, the jedis object can be reused, avoiding frequent socket connection creation and saving connection overhead.

Scheme comparison

Simple use of connection pool

public class Demo {
    
    public static void main(String[] args) {
        //The connection pool configuration object contains many default configurations
        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
        //Initializing the Jedis connection pool is usually a single example
        JedisPool jedisPool = new JedisPool(poolConfig, "119.23.226.29", 6379);
        Jedis jedis = null;
        try {
            //1. Get the jedis object from the connection pool
            jedis = jedisPool.getResource();
            //2. Perform the operation
            jedis.set("hello", "jedis");
            System.out.println(jedis.get("hello"));
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            //If you use JedisPool, the close operation is not to close the connection, which means to return the connection pool
            if(jedis != null){
                jedis.close();
            }
        }   
    }
}

Connection pool encapsulation use

public class RedisPool {
    //jedis connection pool. Use static to ensure that the connection pool is loaded when tomcat starts
    private static JedisPool pool;
    //Maximum connections in connection pool
    private static Integer maxTotal = Integer.parseInt(PropertiesUtil.getProperty("redis.max.total", "20"));
    //Maximum number of idle connections in the connection pool
    private static Integer maxIdle = Integer.parseInt(PropertiesUtil.getProperty("redis.max.idle", "20"));
    //Minimum number of idle connections in the connection pool
    private static Integer minIdle = Integer.parseInt(PropertiesUtil.getProperty("redis.min.idle", "0"));

    //Verify when borrowing connections
    private static Boolean testOnBorrow = Boolean.parseBoolean(PropertiesUtil.getProperty("redis.test.borrow", "true"));
    //Verify when returning the connection
    private static Boolean testOnReturn = Boolean.parseBoolean(PropertiesUtil.getProperty("redis.test.return", "true"));

    private static String redisIp = PropertiesUtil.getProperty("redis.ip");
    private static Integer redisPort = Integer.parseInt(PropertiesUtil.getProperty("redis.port"));
    private static String password = PropertiesUtil.getProperty("redis.password");

    //Initializing the connection pool will only be called once
    private static void initPool() {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(maxTotal);
        config.setMaxIdle(maxIdle);
        config.setMinIdle(minIdle);
        config.setTestOnBorrow(testOnBorrow);
        config.setTestOnReturn(testOnReturn);
        //When the connection pool is exhausted, if it is blocked, false will throw an exception, and true will block until the timeout throws an exception. The default is true
        config.setBlockWhenExhausted(true);
        //Timeout 2s
        pool = new JedisPool(config, redisIp, redisPort, 1000*2, password);
    }

    static {
        initPool();
    }

    //Borrow an instance from the connection pool
    public static Jedis getJedis() {
        return pool.getResource();
    }
}
public class RedisPoolUtil {
    public static String set(String key, String value) {
        Jedis jedis = null;
        String result = null;
        try {
            jedis = RedisPool.getJedis();
            result = jedis.set(key, value);
        } catch (Exception e) {
            log.error("set key:{} value:{} error", key, value, e);
        } finally {
            shutdown(jedis);
        }
        return result;
    }
    
    public static void shutdown(Jedis jedis) {
        if (null != jedis) {
            // close will judge whether the connection is broken and perform the corresponding recycling operation
            jedis.close();
        }
    }
}

Tags: Java Jedis Redis socket Tomcat

Posted on Thu, 07 May 2020 13:30:10 -0400 by sunsun