JedisPool分析
来源:互联网 发布:有道实时翻译软件 编辑:程序博客网 时间:2024/06/05 23:16
jedis是Redis官网推荐的java客户端实现。下面分析一下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;
- }
- }
0 0
- JedisPool分析
- jedisPool
- JedisPool
- JedisPool
- JedisPool介绍
- JedisPool介绍
- JedisPool介绍
- jedisPool实现原理及源码分析(1)----对象池的说明
- 【Redis】spring-data-redis与JedisPool的区别、使用ShardedJedisPool与spring集成的实现及一致性哈希分析
- spring-data-redis与JedisPool的区别、使用ShardedJedisPool与spring集成的实现及一致性哈希分析
- JedisPool配置参数解析
- JedisPool以及参数适用
- JedisPool高并发
- redis配置jedisPool参数
- JedisPool.returnResource()遭弃用
- jedisPool相关参数说明
- JedisPool的配置参数
- redis-Jedispool学习
- C# winform 导出pdf报表
- JSP自定义标签
- Unity协程(Coroutine)管理类——TaskManager工具分享
- 美团推荐算法实践:机器学习重排序模型成亮点
- WINDOWS目录遍历 iconv函数
- JedisPool分析
- sql 2000 实现数据同步(思路)
- 程序员面试失败的5大原因
- MAVEN下载与安装
- android send mail
- centos中mysql远程连接失败解决方案
- ASI导入项目经过和遇到的问题
- jvm监控命令
- 一种更清晰的Android架构