【可改进的问题】
问题是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. session_start保存的客户端cookie的值什么时候改变

    //cookie记录的session_id立刻改变了session_start();echo "old:".session_id();session_regenerate_id() ...

  2. 别说你会用 Google 搜索

    Google 在我们的日常生活中越来越重要,很多时候,包括我在内的很多人一天也离不开 Google,但是,你真的会用 Google 吗?  PHP MySQL "Web developmen ...

  3. linux下mysql操作的命令

    最近在学习mysql,还是只菜鸟,找到下面篇文章对初学者挺有用的,所以共享下 1.linux下启动mysql的命令:   mysqladmin start /ect/init.d/mysql star ...

  4. window 与ubuntu共享文件 hgfs下为空和不用每次挂载hgfs的方法

    解决hgfs为空的方法: sudo apt-get install open-vm-dkms sudo mount -t vmhgfs .host:/ /mnt/hgfs 解决每次都要挂载的方法: 1 ...

  5. AE+C# 图层中增加相应属性标注

    原文 AE+C# 图层中增加相应属性标注 ) { IGeoFeatureLayer pGeoFeatureLayer; ILineLabelPosition pLineLabelPosition; I ...

  6. 解决:cc1.exe: sorry, unimplemented: 64-bit mode not compiled in

    在win下用Go语言的cgo时(比如下面场景)我们会用到的GCC编译器,Win下我们一般用MinGW. Golang连接Oracle数据库:win下 golang 跨平台编译 MinGW全称Minim ...

  7. 【剑指offer 面试题38】数字在排序数组中出现的次数

    思路: 利用二分查找,分别查找待统计数字的头和尾的下标,最后做差加一即为结果. C++: #include <iostream> #include <vector> using ...

  8. centos7虚拟机无法上网的解决办法

    今天在VMware虚拟机中经过千辛万苦终于安装好了centos7..正兴致勃勃的例行yum update 却发现centos系统貌似默认网卡没配置好,反馈无法联网.经过一番研究,终于让centos连上 ...

  9. 使用json格式的数据进行通信

    4 Java对象转换成JSON 4.1 问题 将Java对象转换成符合JSON格式的字符串,并测试. 4.2 方案 使用与json-lib.jar相关的jar文件完成类型的转换. 4.3 步骤 步骤一 ...

  10. leetcode:ZigZag Conversion 曲线转换

    Question: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of ...