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 ...
随机推荐
- 【自动化测试】Selenium处理富文本框
http://blog.csdn.net/fudax/article/details/8089404 selenium处理富文本框,日历控件等 调用JS修改value值 document.getEle ...
- SSH思路
hibernate的配置写到spring的配置中,用spring管理和调用hibernate的工厂和session等.struts的话,通常有2中.一种是用spring中的一个工厂类代替struts的 ...
- ora-0000 normal跟/etc/hosts有关
当hosts文件配置错误时,用sqlplus登录后startup nomount,就会报错ORA-00000 [oracle11g@testdb2 dbs]$ sqlplus "/ as s ...
- 在python中如何设置当前工作目录
import osos.chdir('要设置的当前目录') >>> import os >>> os.getcwd() 'D:\\Python27' >> ...
- 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境:4.安装Oracle RAC FAQ-4.1.系统界面报错Gnome
1.错误信息:登录系统后,屏幕弹出几个错误对话框,无菜单.无按钮 GConf error: Failed to contact configuration server; some possible ...
- 嵌入式 GDB调试死锁示例
死锁:一种情形,此时执行程序中两个或多个线程发生永久堵塞(等待),每个线程都在等待被 其他线程占用并堵塞了的资源.例如,如果线程A锁住了记录1并等待记录2,而线程B锁住了记录2并等待记录1,这样两个线 ...
- Oracle的函数返回表类型(转)
在SQL Server中有表变量,可以在function中方便地返回,习惯SQL Server或者需要把脚本从SQL Server转到Oracle中的朋友可以都会碰到这个问题. Oracle的func ...
- 44、自定义仿IOS对话框
private Dialog myDialog = null; private void ExitLogin() { View view = LayoutInflater.from(MainActiv ...
- Linux基本命令(10)其他命令
其他命令 命令 功能 命令 功能 echo 显示一字串 passwd 修改密码 clear 清除显示器 lpr 打印 lpq 查看在打印队列中等待的作业 lprm 取消打印队列中的作业 10.1 ec ...
- 中文+django1.9+python3.5一些注意点
1.模板html文件里一定要加 <!DOCTYPE html><meta http-equiv="Content-type" content="text ...