【可改进的问题】
问题是jedispool有没有办法监控状态,比如说当前连接有多少,当前idle连接有多少,之类的同求。真心不想每次都构建连接然后在手动将连接返回池。
 
 

【JedisPool源码】

  1. package redis.clients.jedis;
  2. import org.apache.commons.pool.impl.GenericObjectPool.Config;
  3. import redis.clients.util.Pool;
  4. public class JedisPool extends Pool<Jedis>
  5. {
  6. public JedisPool(GenericObjectPool.Config poolConfig, String host)
  7. {
  8. this(poolConfig, host, 6379, 2000, null);
  9. }
  10. <strong>  public JedisPool(String host, int port)
  11. {
  12. super(new GenericObjectPool.Config(), new JedisFactory(host, port, 2000, null));
  13. }</strong>
  14. <strong>  public JedisPool(GenericObjectPool.Config poolConfig, String host, int port, int timeout, String password)
  15. {
  16. super(poolConfig, new JedisFactory(host, port, timeout, password));
  17. }</strong>
  18. public JedisPool(GenericObjectPool.Config poolConfig, String host, int port)
  19. {
  20. this(poolConfig, host, port, 2000, null);
  21. }
  22. public JedisPool(GenericObjectPool.Config poolConfig, String host, int port, int timeout)
  23. {
  24. this(poolConfig, host, port, timeout, null);
  25. }
  26. }

JedisPool继承了Pool,Pool的源码如下:

  1. package redis.clients.util;
  2. import org.apache.commons.pool.PoolableObjectFactory;
  3. import org.apache.commons.pool.impl.GenericObjectPool;
  4. import org.apache.commons.pool.impl.GenericObjectPool.Config;
  5. import redis.clients.jedis.exceptions.JedisConnectionException;
  6. import redis.clients.jedis.exceptions.JedisException;
  7. public abstract class Pool<T>
  8. {
  9. private final GenericObjectPool internalPool;
  10. public Pool(GenericObjectPool.Config poolConfig, PoolableObjectFactory factory)
  11. {
  12. this.internalPool = new GenericObjectPool(factory, poolConfig);
  13. }
  14. public T getResource()
  15. {
  16. try {
  17. return this.internalPool.borrowObject();
  18. } catch (Exception e) {
  19. throw new JedisConnectionException("Could not get a resource from the pool", e);
  20. }
  21. }
  22. public void returnResource(T resource)
  23. {
  24. try {
  25. this.internalPool.returnObject(resource);
  26. } catch (Exception e) {
  27. throw new JedisException("Could not return the resource to the pool", e);
  28. }
  29. }
  30. public void returnBrokenResource(T resource)
  31. {
  32. try {
  33. this.internalPool.invalidateObject(resource);
  34. } catch (Exception e) {
  35. throw new JedisException("Could not return the resource to the pool", e);
  36. }
  37. }
  38. public void destroy()
  39. {
  40. try {
  41. this.internalPool.close();
  42. } catch (Exception e) {
  43. throw new JedisException("Could not destroy the pool", e);
  44. }
  45. }
  46. }

至此我们已经清楚了,JedisPool使用了apache的GenericObjectPool来作为redis连接管理pool。GenericObjectPool的官方地址是:http://commons.apache.org/pool/

JedisFactory是PoolableObjectFactory的子类,PoolableObjectFactory提供了可以被Pool管理的对象的若干生命周期方法,JedisFactory的源码如下:

  1. package redis.clients.jedis;
  2. import org.apache.commons.pool.BasePoolableObjectFactory;
  3. class JedisPool$JedisFactory extends BasePoolableObjectFactory
  4. {
  5. private final String host;
  6. private final int port;
  7. private final int timeout;
  8. private final String password;
  9. public JedisPool$JedisFactory(String host, int port, int timeout, String password)
  10. {
  11. this.host = host;
  12. this.port = port;
  13. this.timeout = ((timeout > 0) ? timeout : -1);
  14. this.password = password;
  15. }
  16. public Object makeObject()
  17. throws Exception
  18. {
  19. Jedis jedis;
  20. if (this.timeout > 0)
  21. jedis = new Jedis(this.host, this.port, this.timeout);
  22. else {
  23. jedis = new Jedis(this.host, this.port);
  24. }
  25. jedis.connect();
  26. if (null != this.password) {
  27. jedis.auth(this.password);
  28. }
  29. return jedis;
  30. }
  31. public void destroyObject(Object obj) throws Exception {
  32. if (obj instanceof Jedis) {
  33. Jedis jedis = (Jedis)obj;
  34. if (!(jedis.isConnected())) return;
  35. try {
  36. try {
  37. jedis.quit();
  38. } catch (Exception e) {
  39. }
  40. jedis.disconnect();
  41. }
  42. catch (Exception e)
  43. {
  44. }
  45. }
  46. }
  47. public boolean validateObject(Object obj) {
  48. if (obj instanceof Jedis) {
  49. Jedis jedis = (Jedis)obj;
  50. try {
  51. return ((jedis.isConnected()) && (jedis.ping().equals("PONG")));
  52. } catch (Exception e) {
  53. return false;
  54. }
  55. }
  56. return false;
  57. }
  58. }

上面代码非常清楚地说明了JedisFactory如何构建、销毁、验证Jedis对象。这些生命周期方法会被GenericObjectPool 的borrowObject,returnObject,invalidateObject等方法调用。

JedisPool连接池实现难点的更多相关文章

  1. Java与redis交互、Jedis连接池JedisPool

    Java与redis交互比较常用的是Jedis. 先导入jar包: commons-pool2-2.3.jar jedis-2.7.0.jar 基本使用: public class RedisTest ...

  2. 详解Jedis连接池报错处理

    在使用Jedis连接池模式下,比较常见的报错如下: redis.clients.jedis.exceptions.JedisConnectionException:Could not get a re ...

  3. redis单机连接池

    一.配置文件 1. db.properties配置文件#IP地址 redis.ip = 127.0.0.1 #端口号 redis.port= #最大连接数 redis.max.total= #最大空闲 ...

  4. 三:Redis连接池、JedisPool详解、Redisi分布式

    单机模式: package com.ljq.utils; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; ...

  5. redis连接池(JedisPool)资源归还及timeout详解

    转载. https://blog.csdn.net/yaomingyang/article/details/79043019 一.连接池资源类详解都在注释上 package redis.v1.clie ...

  6. redis连接池——JedisPool和JedisCluster的介绍与使用

    目录 Jedis使用方式的介绍 Redis连接池介绍 创建连接池配置文件 单机版的Redis连接池 集群版的Redis连接池 总结 Jedis使用方式的介绍 Jedis就是Java实现的操作Redis ...

  7. Redis缓存连接池管理

    import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.util.Assert;import ...

  8. common-pool2对象池(连接池)的介绍及使用

    我们在服务器开发的过程中,往往会有一些对象,它的创建和初始化需要的时间比较长,比如数据库连接,网络IO,大数据对象等.在大量使用这些对象时,如果不采用一些技术优化,就会造成一些不可忽略的性能影响.一种 ...

  9. Jedis编程设计:连接池

        Jedis作为redis的最佳客户端,它提供了连接池的特性,"连接池"在通常情况下可以有效的提高应用的通信能力,并且这是一种良好的设计模式.Jedis的连接池设计基于apa ...

随机推荐

  1. python编码问题(1)

    一.字符编码基础 字符编码是计算机对字符的格式化,从而能够在计算机系统中存储与传输. 1.ASCII码 在计算机内部,所有的信息最终都表示为一个二进制的字符串.每一个二进制位(bit)有0和1两种状态 ...

  2. yaf框架流程二

    这篇讲讲yaf的配置文件,首先上我的配置代码: [common] ;必选配置 ;application.directory String 应用的绝对目录路径 ;可选配置 ;名称 值类型 默认值 说明 ...

  3. swun 1612 合并果子

      //思路:这题思路似乎很简单,每次取出最小的两个堆合并, //但是由于数据太大,不能采取每次进行排序的方式,所以 //想到用优先队列,以数据小的优先级更高为标准,但是 //优先队列中的数据默认情况 ...

  4. Android 签名详解

    Android 签名详解 AndroidOPhoneAnt设计模式Eclipse  在Android 系统中,所有安装 到 系统的应用程序都必有一个数字证书,此数字证书用于标识应用程序的作者和在应用程 ...

  5. ubuntu下 apt-get install 下载的文件存放的目录

    apt-get把下载的deb包全部放在/var/cache/apt/archives下面,该目录下的文件可以删除.当然用 sudo apt-get clean 命令也可以,这个命令只会删除缓存起来的d ...

  6. $(document).Ready()方法 VS OnLoad事件 VS $(window).load()方法

    $(document).Ready()方法 VS OnLoad事件 VS $(window).load()方法接触JQuery一般最先学到的是何时启动事件.在曾经很长一段时间里,在页面载入后引发的事件 ...

  7. [Papers]NSE, $u$, Lorentz space [Bjorland-Vasseur, JMFM, 2011]

    $$\bex \int_0^T\frac{\sen{\bbu}_{L^{q,\infty}}^p}{\ve+\ln \sex{e+\sen{\bbu}_{L^\infty}}}\rd s<\in ...

  8. ylb:exists(存在)的应用实例

    ylbtech-SQL Server:exists(存在)的应用实例 SQL Server exists(存在)的应用实例. 1,exists(存在)的应用实例 返回顶部 -- =========== ...

  9. C++ 虚拟继承

    1.为什么要引入虚拟继承 虚拟继承是多重继承中特有的概念.虚拟基类是为解决多重继承而出现的.如:类D继承自类B1.B2,而类B1.B2都继 承自类A,因此在类D中两次出现类A中的变量和函数.为了节省内 ...

  10. JS方式调用本地的可执行文件

    看到一个方法,有些用,先存下来,有用的时候再用. 前几天,在IE,FIREFOX中实现了用JS方式调用本地的可执行文件.地址:www.yihaomen.com/article/js/211.htm , ...