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使用注意事项

  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值