Create JedisPoolConfig - Java Big Data

Java examples for Big Data:Jedis

Description

Create JedisPoolConfig

Demo Code



import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;


public class RedisClient implements Serializable {


    private static final long serialVersionUID = 1357749867950858521L;

    private Jedis jedis; 
    private JedisPool jedisPool; 
    private ShardedJedis shardedJedis; 
    private ShardedJedisPool shardedJedisPool; 

    private static Integer maxActive = 20;
    private static Integer maxIdle = 5;
    private static Long maxWait = 1000l;
    private static String host = "10.33.3.225";
    private static Integer port = 6379;

    public RedisClient() {
        initPool();//from   w w w. ja v a2  s .  com
        initShardedPool();
    }

    private void initPool() {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(maxActive);
        config.setMaxIdle(maxIdle);
        config.setMaxWaitMillis(maxWait);
        config.setTestOnBorrow(false);
        jedisPool = new JedisPool(config, host, port);
    }

    private void initShardedPool() {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(maxActive);
        config.setMaxIdle(5);
        config.setMaxWaitMillis(maxWait);
        config.setTestOnBorrow(false);
        List<JedisShardInfo> shards = new ArrayList<>();
        shards.add(new JedisShardInfo("10.33.3.225", 6379, "master"));
        shardedJedisPool = new ShardedJedisPool(config, shards);
    }

    public Jedis getJedis() {
        return jedis;
    }

    public void setJedis(Jedis jedis) {
        this.jedis = jedis;
    }

    public JedisPool getJedisPool() {
        return jedisPool;
    }

    public void setJedisPool(JedisPool jedisPool) {
        this.jedisPool = jedisPool;
    }

    public ShardedJedis getShardedJedis() {
        return shardedJedis;
    }

    public void setShardedJedis(ShardedJedis shardedJedis) {
        this.shardedJedis = shardedJedis;
    }

    public ShardedJedisPool getShardedJedisPool() {
        return shardedJedisPool;
    }

    public void setShardedJedisPool(ShardedJedisPool shardedJedisPool) {
        this.shardedJedisPool = shardedJedisPool;
    }

    public static Integer getMaxActive() {
        return maxActive;
    }

    public static void setMaxActive(Integer maxActive) {
        RedisClient.maxActive = maxActive;
    }

    public static Integer getMaxIdle() {
        return maxIdle;
    }

    public static void setMaxIdle(Integer maxIdle) {
        RedisClient.maxIdle = maxIdle;
    }

    public static Long getMaxWait() {
        return maxWait;
    }

    public static void setMaxWait(Long maxWait) {
        RedisClient.maxWait = maxWait;
    }

    public static String getHost() {
        return host;
    }

    public static void setHost(String host) {
        RedisClient.host = host;
    }

    public static Integer getPort() {
        return port;
    }

    public static void setPort(Integer port) {
        RedisClient.port = port;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }

}

Related Tutorials