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 ...
随机推荐
- (六)6.7 Neurons Networks whitening
PCA的过程结束后,还有一个与之相关的预处理步骤,白化(whitening) 对于输入数据之间有很强的相关性,所以用于训练数据是有很大冗余的,白化的作用就是降低输入数据的冗余,通过白化可以达到(1)降 ...
- 中文分词系列(一) 双数组Tire树(DART)详解
1 双数组Tire树简介 双数组Tire树是Tire树的升级版,Tire取自英文Retrieval中的一部分,即检索树,又称作字典树或者键树.下面简单介绍一下Tire树. 1.1 Tire树 Trie ...
- 【英语】Bingo口语笔记(4) - Pick系列
take your pick. 你挑吧 pick on 找茬
- java-过滤器-监听器-拦截器
1.过滤器 Servlet中的过滤器Filter是实现了javax.servlet.Filter接口的服务器端程序,主要的用途是过滤字符编码.做一些业务逻辑判断等.其工作原理是,只要你在web.xml ...
- poj 1986 Distance Queries
好像是模板题 当作练习题 不错: 要求任意两点之间的距离.可以假设一个根节点,然后所有点到根节点的距离,然后求出任意两点多公共祖先: 距离就变成了 dis[u]+dis[v] - 2*dis[ ...
- mac os 系统密码正确的 但是进不了系统
今天开始重新学习C 但是一早起来开机进不了系统 密码明明正确的 无语了,后来打苹果售后电话解决了. 今天记录下如何解决的,以后万一又出现这种问题,至于原因嘛 我也不知道 有可能跟我装的双系统有关系.只 ...
- php 计算本月第一天 本月最后一天 下个月第一天
本文转载自 http://jin541223.blog.163.com/blog/static/1637398052011111233018533/ //本周第一天(星期日为一周开始) echo ...
- trunc的使用
1.日期比较时精确到日,可以使用 TRUNC(sysdate,'dd')函数.函数支持格式有:yyyy MM dd hh Mi可以用 select TRUNC(sysdate,'yyyy') fr ...
- CentOS安装tomcat
一.下载Tomcat 1..进入Tomcat官网:http://tomcat.apache.org/ 左侧选择相应的版本 点击Tomcat 6.0后 点击tar.gz下载apache-tomcat-6 ...
- 查看MySQL的警告信息
在王MySQL数据库导入数据的时候经常会出现警告,这些警告很容易被忽视,今天到数据的时候突然想看看警告的内容是什么,百度了一下mysql查看警告的命令 show warnings; 命令很简明,一查看 ...