Bootstrap

JedisPool.returnResource()遭弃用后替代方法

变动

自Jedis3.0版本后jedisPool.returnResource(final Jedis resource)遭弃用,官方重写了Jedis的close()方法用以代替

代码如下:

/**
   * @deprecated starting from Jedis 3.0 this method will not be exposed.
   * Resource cleanup should be done using @see {@link redis.clients.jedis.Jedis#close()}
   */
  @Override
  @Deprecated
  public void returnResource(final Jedis resource) {
    if (resource != null) {
      try {
        resource.resetState();
        returnResourceObject(resource);
      } catch (Exception e) {
        returnBrokenResource(resource);
        throw new JedisException("Could not return the resource to the pool", e);
      }
    }
  }

官方建议应用redis.clients.jedis#Jedis的close方法进行资源回收,官方代码如下:

@Override
  public void close() {
    if (dataSource != null) {
      if (client.isBroken()) {
        this.dataSource.returnBrokenResource(this);
      } else {
        this.dataSource.returnResource(this);
      }
    } else {
      client.close();
    }
  }

示例


修改成
jedisPool.returnResource(jedis)
jedis.close()

Jedis3.0之前 使用jedisPool.returnResource(jedis)

		
	import com.alibaba.fastjson.JSON;
	import redis.clients.jedis.Jedis;
	import redis.clients.jedis.JedisPool;
	import redis.clients.jedis.JedisPoolConfig;
	
	import java.util.ResourceBundle;
	
	public class RedisClient {
	
	        /**
	         * 池化管理jedis链接池
	         */
	        public static JedisPool jedisPool;
	
	        static {
	
	            //读取相关的配置
	            ResourceBundle resourceBundle = ResourceBundle.getBundle("redis");
	            int maxActive = Integer.parseInt(resourceBundle.getString("redis.pool.maxActive"));
	            int maxIdle = Integer.parseInt(resourceBundle.getString("redis.pool.maxIdle"));
	            int maxWait = Integer.parseInt(resourceBundle.getString("redis.pool.maxWait"));
	
	            String ip = resourceBundle.getString("redis.ip");
	            int port = Integer.parseInt(resourceBundle.getString("redis.port"));
	
	            JedisPoolConfig config = new JedisPoolConfig();
	            //设置最大连接数
	            config.setMaxTotal(maxActive);
	            //设置最大空闲数
	            config.setMaxIdle(maxIdle);
	            //设置超时时间
	            config.setMaxWaitMillis(maxWait);
	
	            //初始化连接池
	            jedisPool = new JedisPool(config, ip, port);
	        }
	
	        /**
	         * 向缓存中设置字符串内容
	         *
	         * @param key   key
	         * @param value value
	         * @return
	         * @throws Exception
	         */
	        public static boolean set(String key, String value) throws Exception {
	            Jedis jedis = null;
	            try {
	                jedis = jedisPool.getResource();
	                jedis.set(key, value);
	                return true;
	            } catch (Exception e) {
	                e.printStackTrace();
	                return false;
	            } finally {
	                jedisPool.returnResource(jedis);
	            }
	        }
	
	        /**
	         * 向缓存中设置对象
	         *
	         * @param key
	         * @param value
	         * @return
	         */
	        public static boolean set(String key, Object value) {
	            Jedis jedis = null;
	            try {
	                String objectJson = JSON.toJSONString(value);
	                jedis = jedisPool.getResource();
	                jedis.set(key, objectJson);
	                return true;
	            } catch (Exception e) {
	                e.printStackTrace();
	                return false;
	            } finally {
	                jedisPool.returnResource(jedis);
	            }
	        }
	
	        /**
	         * 删除缓存中得对象,根据key
	         *
	         * @param key
	         * @return
	         */
	        public static boolean del(String key) {
	            Jedis jedis = null;
	            try {
	                jedis = jedisPool.getResource();
	                jedis.del(key);
	                return true;
	            } catch (Exception e) {
	                e.printStackTrace();
	                return false;
	            } finally {
	                jedisPool.returnResource(jedis);
	            }
	        }
	
	        /**
	         * 根据key 获取内容
	         *
	         * @param key
	         * @return
	         */
	        public static Object get(String key) {
	            Jedis jedis = null;
	            try {
	                jedis = jedisPool.getResource();
	                Object value = jedis.get(key);
	                return value;
	            } catch (Exception e) {
	                e.printStackTrace();
	                return false;
	            } finally {
	                jedisPool.returnResource(jedis);
	            }
	        }
	
	
	        /**
	         * 根据key 获取对象
	         *
	         * @param key
	         * @return
	         */
	        public static <T> T get(String key, Class<T> clazz) {
	            Jedis jedis = null;
	            try {
	                jedis = jedisPool.getResource();
	                String value = jedis.get(key);
	                return JSON.parseObject(value, clazz);
	            } catch (Exception e) {
	                e.printStackTrace();
	                return null;
	            } finally {
	                jedisPool.returnResource(jedis);
	            }
	        }
	}

Jedis3.0之后使用jedis.close()

	import com.alibaba.fastjson.JSON;
	import redis.clients.jedis.Jedis;
	import redis.clients.jedis.JedisPool;
	import redis.clients.jedis.JedisPoolConfig;
	
	import java.util.ResourceBundle;
	
	public class RedisClient {
	
	        /**
	         * 池化管理jedis链接池
	         */
	        public static JedisPool jedisPool;
	
	        static {
	
	            //读取相关的配置
	            ResourceBundle resourceBundle = ResourceBundle.getBundle("redis");
	            int maxActive = Integer.parseInt(resourceBundle.getString("redis.pool.maxActive"));
	            int maxIdle = Integer.parseInt(resourceBundle.getString("redis.pool.maxIdle"));
	            int maxWait = Integer.parseInt(resourceBundle.getString("redis.pool.maxWait"));
	
	            String ip = resourceBundle.getString("redis.ip");
	            int port = Integer.parseInt(resourceBundle.getString("redis.port"));
	
	            JedisPoolConfig config = new JedisPoolConfig();
	            //设置最大连接数
	            config.setMaxTotal(maxActive);
	            //设置最大空闲数
	            config.setMaxIdle(maxIdle);
	            //设置超时时间
	            config.setMaxWaitMillis(maxWait);
	
	            //初始化连接池
	            jedisPool = new JedisPool(config, ip, port);
	        }
	
	        /**
	         * 向缓存中设置字符串内容
	         *
	         * @param key   key
	         * @param value value
	         * @return
	         * @throws Exception
	         */
	        public static boolean set(String key, String value) throws Exception {
	            Jedis jedis = null;
	            try {
	                jedis = jedisPool.getResource();
	                jedis.set(key, value);
	                return true;
	            } catch (Exception e) {
	                e.printStackTrace();
	                return false;
	            } finally {
	                jedis.close();
	            }
	        }
	
	        /**
	         * 向缓存中设置对象
	         *
	         * @param key
	         * @param value
	         * @return
	         */
	        public static boolean set(String key, Object value) {
	            Jedis jedis = null;
	            try {
	                String objectJson = JSON.toJSONString(value);
	                jedis = jedisPool.getResource();
	                jedis.set(key, objectJson);
	                return true;
	            } catch (Exception e) {
	                e.printStackTrace();
	                return false;
	            } finally {
	                jedis.close();
	            }
	        }
	
	        /**
	         * 删除缓存中得对象,根据key
	         *
	         * @param key
	         * @return
	         */
	        public static boolean del(String key) {
	            Jedis jedis = null;
	            try {
	                jedis = jedisPool.getResource();
	                jedis.del(key);
	                return true;
	            } catch (Exception e) {
	                e.printStackTrace();
	                return false;
	            } finally {
	                jedis.close();
	            }
	        }
	
	        /**
	         * 根据key 获取内容
	         *
	         * @param key
	         * @return
	         */
	        public static Object get(String key) {
	            Jedis jedis = null;
	            try {
	                jedis = jedisPool.getResource();
	                Object value = jedis.get(key);
	                return value;
	            } catch (Exception e) {
	                e.printStackTrace();
	                return false;
	            } finally {
	                jedis.close();
	            }
	        }
	
	
	        /**
	         * 根据key 获取对象
	         *
	         * @param key
	         * @return
	         */
	        public static <T> T get(String key, Class<T> clazz) {
	            Jedis jedis = null;
	            try {
	                jedis = jedisPool.getResource();
	                String value = jedis.get(key);
	                return JSON.parseObject(value, clazz);
	            } catch (Exception e) {
	                e.printStackTrace();
	                return null;
	            } finally {
	                jedis.close();
	            }
	        }
	}

参考

1. JedisPool.returnResource()遭弃用
2. Jedis-returnResource使用注意事项

;