JedisPool连接池实现难点
【JedisPool源码】
- package redis.clients.jedis;
- import org.apache.commons.pool.impl.GenericObjectPool.Config;
- import redis.clients.util.Pool;
- public class JedisPool extends Pool<Jedis>
- {
- public JedisPool(GenericObjectPool.Config poolConfig, String host)
- {
- this(poolConfig, host, 6379, 2000, null);
- }
- <strong> public JedisPool(String host, int port)
- {
- super(new GenericObjectPool.Config(), new JedisFactory(host, port, 2000, null));
- }</strong>
- <strong> public JedisPool(GenericObjectPool.Config poolConfig, String host, int port, int timeout, String password)
- {
- super(poolConfig, new JedisFactory(host, port, timeout, password));
- }</strong>
- public JedisPool(GenericObjectPool.Config poolConfig, String host, int port)
- {
- this(poolConfig, host, port, 2000, null);
- }
- public JedisPool(GenericObjectPool.Config poolConfig, String host, int port, int timeout)
- {
- this(poolConfig, host, port, timeout, null);
- }
- }
JedisPool继承了Pool,Pool的源码如下:
- package redis.clients.util;
- import org.apache.commons.pool.PoolableObjectFactory;
- import org.apache.commons.pool.impl.GenericObjectPool;
- import org.apache.commons.pool.impl.GenericObjectPool.Config;
- import redis.clients.jedis.exceptions.JedisConnectionException;
- import redis.clients.jedis.exceptions.JedisException;
- public abstract class Pool<T>
- {
- private final GenericObjectPool internalPool;
- public Pool(GenericObjectPool.Config poolConfig, PoolableObjectFactory factory)
- {
- this.internalPool = new GenericObjectPool(factory, poolConfig);
- }
- public T getResource()
- {
- try {
- return this.internalPool.borrowObject();
- } catch (Exception e) {
- throw new JedisConnectionException("Could not get a resource from the pool", e);
- }
- }
- public void returnResource(T resource)
- {
- try {
- this.internalPool.returnObject(resource);
- } catch (Exception e) {
- throw new JedisException("Could not return the resource to the pool", e);
- }
- }
- public void returnBrokenResource(T resource)
- {
- try {
- this.internalPool.invalidateObject(resource);
- } catch (Exception e) {
- throw new JedisException("Could not return the resource to the pool", e);
- }
- }
- public void destroy()
- {
- try {
- this.internalPool.close();
- } catch (Exception e) {
- throw new JedisException("Could not destroy the pool", e);
- }
- }
- }
至此我们已经清楚了,JedisPool使用了apache的GenericObjectPool来作为redis连接管理pool。GenericObjectPool的官方地址是:http://commons.apache.org/pool/
JedisFactory是PoolableObjectFactory的子类,PoolableObjectFactory提供了可以被Pool管理的对象的若干生命周期方法,JedisFactory的源码如下:
- package redis.clients.jedis;
- import org.apache.commons.pool.BasePoolableObjectFactory;
- class JedisPool$JedisFactory extends BasePoolableObjectFactory
- {
- private final String host;
- private final int port;
- private final int timeout;
- private final String password;
- public JedisPool$JedisFactory(String host, int port, int timeout, String password)
- {
- this.host = host;
- this.port = port;
- this.timeout = ((timeout > 0) ? timeout : -1);
- this.password = password;
- }
- public Object makeObject()
- throws Exception
- {
- Jedis jedis;
- if (this.timeout > 0)
- jedis = new Jedis(this.host, this.port, this.timeout);
- else {
- jedis = new Jedis(this.host, this.port);
- }
- jedis.connect();
- if (null != this.password) {
- jedis.auth(this.password);
- }
- return jedis;
- }
- public void destroyObject(Object obj) throws Exception {
- if (obj instanceof Jedis) {
- Jedis jedis = (Jedis)obj;
- if (!(jedis.isConnected())) return;
- try {
- try {
- jedis.quit();
- } catch (Exception e) {
- }
- jedis.disconnect();
- }
- catch (Exception e)
- {
- }
- }
- }
- public boolean validateObject(Object obj) {
- if (obj instanceof Jedis) {
- Jedis jedis = (Jedis)obj;
- try {
- return ((jedis.isConnected()) && (jedis.ping().equals("PONG")));
- } catch (Exception e) {
- return false;
- }
- }
- return false;
- }
- }
上面代码非常清楚地说明了JedisFactory如何构建、销毁、验证Jedis对象。这些生命周期方法会被GenericObjectPool 的borrowObject,returnObject,invalidateObject等方法调用。
JedisPool连接池实现难点的更多相关文章
- Java与redis交互、Jedis连接池JedisPool
Java与redis交互比较常用的是Jedis. 先导入jar包: commons-pool2-2.3.jar jedis-2.7.0.jar 基本使用: public class RedisTest ...
- 详解Jedis连接池报错处理
在使用Jedis连接池模式下,比较常见的报错如下: redis.clients.jedis.exceptions.JedisConnectionException:Could not get a re ...
- redis单机连接池
一.配置文件 1. db.properties配置文件#IP地址 redis.ip = 127.0.0.1 #端口号 redis.port= #最大连接数 redis.max.total= #最大空闲 ...
- 三:Redis连接池、JedisPool详解、Redisi分布式
单机模式: package com.ljq.utils; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; ...
- redis连接池(JedisPool)资源归还及timeout详解
转载. https://blog.csdn.net/yaomingyang/article/details/79043019 一.连接池资源类详解都在注释上 package redis.v1.clie ...
- redis连接池——JedisPool和JedisCluster的介绍与使用
目录 Jedis使用方式的介绍 Redis连接池介绍 创建连接池配置文件 单机版的Redis连接池 集群版的Redis连接池 总结 Jedis使用方式的介绍 Jedis就是Java实现的操作Redis ...
- Redis缓存连接池管理
import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.util.Assert;import ...
- common-pool2对象池(连接池)的介绍及使用
我们在服务器开发的过程中,往往会有一些对象,它的创建和初始化需要的时间比较长,比如数据库连接,网络IO,大数据对象等.在大量使用这些对象时,如果不采用一些技术优化,就会造成一些不可忽略的性能影响.一种 ...
- Jedis编程设计:连接池
Jedis作为redis的最佳客户端,它提供了连接池的特性,"连接池"在通常情况下可以有效的提高应用的通信能力,并且这是一种良好的设计模式.Jedis的连接池设计基于apa ...
随机推荐
- AIX LVM 常用命令记录
针对物理卷的操作指令 lsdev--列出ODM中的设备 chdev--修改一个AIX设备的属性 mkdev--创建一个AIX设备 chpv--修改物理卷的状态和属性 lspv--查看AIX中物理卷的相 ...
- LOL-无双剑姬我的最爱
LOL打了几年了,是一种娱乐的好方式,但是一个人玩不开黑就很无聊.这游戏最开始我玩的时候无论是赢是输就无所谓的,很高兴的.但是现在输了反而很气愤.也不知道为什么,所以很少玩了. 剑姬对反甲:如果对方出 ...
- Android 程序员必须掌握的三种自动化测试方法
在日常的开发中,尤其是app开发,因为不像web端那样 出错以后可以热更新,所以app开发 一般对软件质量有更高的要求(你可以想一下 一个发出去的版本如果有重大缺陷 需要强制更新新客户端是多么蛋疼的事 ...
- ANDROID模拟器访问本地WEB应用
一个早上就是想让android的模拟器可以访问到web的应用程序,但是一直是不可以,弄的不知所措. 在一般的Java Web程序开发中,我们通常使用localhost或者127.0.0.1来访问本机的 ...
- Karel运行环境配置
1.下载 见http://wenku.baidu.com/view/24762ced998fcc22bcd10d5e.html 2.界面空白问题 问题:运行Karel后,发现整个界面空白一片,没有任何 ...
- linux设置默认路由细节问题
在这里,我想给大家讲解下,linux系统默认路由的设置的一些细节问题.这样在设置多块网卡的时候如何设置路由可以为初学者少走一些弯路. 默认情况下配置多块网卡,每个网卡都要配置ip,每个ip又是在不 ...
- [Everyday Mathematics]20150225
设 $f:\bbR\to\bbR$ 二次可微, 适合 $f(0)=0$. 试证: $$\bex \exists\ \xi\in\sex{-\frac{\pi}{2},\frac{\pi}{2}},\s ...
- java jxl 向Excel中追加数据而不覆盖原来数据的例子
向先原来就有数据的Excel写数据是不会覆盖原有的数据,只是在追加数据. public class Excel { public Excel() { } public void ...
- 【2013微软面试题】输出节点数为n的二叉树的所有形态
转自:http://blog.csdn.net/monsterxd/article/details/8449005 /* * 题意,求节点数为n的二叉树的所有形态,先要想个方式来唯一标示一棵二叉树 ...
- 使用U盘安装win7系统,遇到“无法定位现有系统分区”问题
朋友的本子貌似因为安装360wifi而导致一进入系统就蓝屏重启,虽然之后就卸载了360wifi,但是问题依旧,上网Google了一下,发觉网上不少网友诉苦,也有人分析原因,说是因为360wifi导致了 ...