4. Construction and test of Redis connection pool

Keywords: Database Redis Jedis shell

First, we will create a RedisPool class in our project
The code is as follows:

package com.mmall.common;

import com.mmall.util.PropertiesUtil;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author Chakid
 * @since 2018-10-30 20:15
 */
public class RedisPool {
    private static JedisPool pool; //jedis connection pool

    //IP address of redis
    private static String redisIp=PropertiesUtil.getProperty("redis.ip");

    //redis open port
    private static Integer redisPort =Integer.parseInt(PropertiesUtil.getProperty("redis.port"));

    //jedis maximum connections
    private static Integer maxTotal= Integer.parseInt(PropertiesUtil.getProperty("redis.max.total","20"));

    //Maximum number of idle connections
    private static Integer maxIdle= Integer.parseInt(PropertiesUtil.getProperty("redis.max.idle","10"));

    //Minimum free connections
    private static Integer minIdle= Integer.parseInt(PropertiesUtil.getProperty("redis.min.idle","2"));

    //When getting a connection from the jedis connection pool, verify and return the available connections
    private static boolean testBorrow= Boolean.parseBoolean(PropertiesUtil.getProperty("redis.test.borrow","true"));

    //When putting the connection back into the jedis connection pool, verify and return the available connections
    private static boolean testReturn= Boolean.parseBoolean(PropertiesUtil.getProperty("redis.test.return","true"));


    //Initialize connection pool
    private static void initPool(){

        JedisPoolConfig config = new JedisPoolConfig();

        //Set related values to config
        config.setMaxTotal(maxTotal);
        config.setMaxIdle(maxIdle);
        config.setMinIdle(minIdle);
        config.setTestOnBorrow(testBorrow);
        config.setTestOnReturn(testReturn);

        //When the connection is exhausted, if it is blocked, false will throw an exception, true until the blocking timeout, and the default is true
        config.setBlockWhenExhausted(true);

        pool = new JedisPool(config,redisIp,redisPort,1000*2);
    }


    //Instantiate connection pool
    static {
        initPool();
    }

    //Get a connection object from the connection pool
    public static Jedis getJedis(){
        return pool.getResource();
    }


    //If it's a broken connection, put it in the BrokenResource
    public static void returnBrokenResource(Jedis jedis){
        pool.returnBrokenResource(jedis);
    }

    //Put jedis back in the connection pool
    public static void returnResource(Jedis jedis){
        pool.returnResource(jedis);
    }



    //Test our connection with main

    public static void main(String[] args) {
        Jedis jedis = pool.getResource();

        jedis.set("chakidkey","chakidvalue");

        returnResource(jedis);

        pool.destroy();

        System.out.println("program is end~");
    }

}

Then, since I configure the configuration information separately, if I only test the connection of Jedis separately, I can fill in the corresponding parameters below me~

#start redis config

#IP address of redis server
redis.ip=119.29.xx.xxx

#redis port
redis.port=6379

#maximum connection
redis.max.total=20

#Maximum number of idle connections
redis.max.idle=10

#Minimum free connections
redis.min.idle=2

#When getting a connection from the jedis connection pool, verify and return the available connections
redis.test.borrow=true

#When we put the connection back to the jedis connection pool and verify and return the available connection return to false, we can improve our concurrency efficiency when the concurrency is high
redis.test.return=false

#end redis config

The related code is explained in the comments~
After the test code is written, the next step is to test our connection:
Use x shell to connect to our server and open Redis service


image.png

Then Xshell starts our connection client:

image.png

I found that there were some in it. First, I deleted them and executed: flushall
image.png

OK, I found that all the key s have been cleared by us. Next, we need to run the code we wrote above. Just run the main function~
OK, the execution is finished. Let's see if there is any data in our redis~

Eh, I found that the data has been added, so the test is successful~


image.png

Posted by Virii on Mon, 09 Dec 2019 11:21:05 -0800