java jedis2.8

Table of Contents

java jedis2.8

第一种

今天把jedis升级为2.8.0,原来pool.returnResource(jedis),可以直接使用为jedis.close()。 如:

    ......
    public static void close(Jedis jedis) {
        if (null != jedis) {
            pool.returnResource(jedis);
        }
    }

更改为:
    public static void close(Jedis jedis) {
        if (null != jedis) {
            jedis.close();
        }
    }

调用:
...
Jedis jedis = RedisHandler.getJedis();
try{
    result = jedis.incr(key);
}catch(Exception e){
    log4j.error(e.getMessage());
}finally{
    RedisHandler.close(jedis);
}
...

可以查看之前的文章

第二种

或可以采用下面这种使用方法:

public class RedisHandler {

   /**
    *
    */
    private static JedisPool pool = null;

    /**
     *
     */
    private static JedisPoolConfig conf = new JedisPoolConfig();

    /**
     *
     */
    static {
    JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(6000);
        config.setMaxIdle(1000);
        config.setMaxWaitMillis(20000);
        pool = new JedisPool(conf, "IP地址", 端口, 20000);
    }

    public static JedisPool getJedisPool() {
    return pool;
    }

}

调用:
try (Jedis jedis = RedisHandler.getJedisPool().getResource()) {
    Pipeline p = jedis.pipelined();
    p.incr("view:test");
}

pom.xml

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.8.0</version>
</dependency>

备注

备注防忘记。建议使用第二种

Author: josephzeng (josephzeng36@gmail.com)

Last Updated 2016-02-18. Created by Emacs 24.5.1 (Org mode 8.2.10)

Validate