jedis连接池对commons-pool的封装示例详解

 更新时间:2023年09月25日 10:44:14   作者:codecraft  
这篇文章主要为大家介绍了jedis连接池对commons-pool的封装示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

文本主要研究一下jedis连接池对commons-pool的封装

JedisPoolConfig

jedis-3.8.0-sources.jar!/redis/clients/jedis/JedisPoolConfig.java

public class JedisPoolConfig extends GenericObjectPoolConfig<Jedis> {
  public JedisPoolConfig() {
    // defaults to make your life with connection pool easier :)
    setTestWhileIdle(true);
    setMinEvictableIdleTimeMillis(60000);
    setTimeBetweenEvictionRunsMillis(30000);
    setNumTestsPerEvictionRun(-1);
  }
}
JedisPoolConfig继承了GenericObjectPoolConfig,在构造器里头设置了默认的参数,即testWhileIdle为true,minEvictableIdleTime为60s,timeBetweenEvictionRuns为30s,numTestsPerEvictionRun为-1

JedisFactory

见上一篇文章聊聊JedisFactory

Pool

jedis-3.8.0-sources.jar!/redis/clients/jedis/util/Pool.java

public abstract class Pool<T> implements Closeable {
  /**
   * @deprecated This will be private in future.
   */
  @Deprecated
  protected GenericObjectPool<T> internalPool;
  public Pool(final GenericObjectPoolConfig<T> poolConfig, PooledObjectFactory<T> factory) {
    initPool(poolConfig, factory);
  }
  /**
   * @param poolConfig
   * @param factory
   * @deprecated This method will be private in future.
   */
  @Deprecated
  public void initPool(final GenericObjectPoolConfig<T> poolConfig, PooledObjectFactory<T> factory) {
    if (this.internalPool != null) {
      try {
        closeInternalPool();
      } catch (Exception e) {
      }
    }
    this.internalPool = new GenericObjectPool<>(factory, poolConfig);
  }
  public T getResource() {
    try {
      return internalPool.borrowObject();
    } catch (NoSuchElementException nse) {
      if (null == nse.getCause()) { // The exception was caused by an exhausted pool
        throw new JedisExhaustedPoolException(
            "Could not get a resource since the pool is exhausted", nse);
      }
      // Otherwise, the exception was caused by the implemented activateObject() or ValidateObject()
      throw new JedisException("Could not get a resource from the pool", nse);
    } catch (Exception e) {
      throw new JedisConnectionException("Could not get a resource from the pool", e);
    }
  }
  public void returnResource(final T resource) {
    if (resource != null) {
      returnResourceObject(resource);
    }
  }
  /**
   * @param resource
   * @deprecated This will be removed in next major release. Use {@link Pool#returnResource(java.lang.Object)}.
   */
  @Deprecated
  protected void returnResourceObject(final T resource) {
    try {
      internalPool.returnObject(resource);
    } catch (RuntimeException e) {
      throw new JedisException("Could not return the resource to the pool", e);
    }
  }
  public void destroy() {
    closeInternalPool();
  }
/**
   * @deprecated This will be removed in next major release. Use {@link Pool#destroy()}.
   */
  @Deprecated
  protected void closeInternalPool() {
    try {
      internalPool.close();
    } catch (RuntimeException e) {
      throw new JedisException("Could not destroy the pool", e);
    }
  }
  /**
   * @param resource
   * @deprecated This will be removed in next major release. Use {@link Pool#returnBrokenResource(java.lang.Object)}.
   */
  @Deprecated
  protected void returnBrokenResourceObject(final T resource) {
    try {
      internalPool.invalidateObject(resource);
    } catch (Exception e) {
      throw new JedisException("Could not return the broken resource to the pool", e);
    }
  }
  //......
}
Pool声明实现Closeable接口,它的构造器根据GenericObjectPoolConfig和PooledObjectFactory来创建GenericObjectPool,它的getResource、returnResource、returnBrokenResourceObject、destroy方法内部都是委托给了GenericObjectPool
它有一个实现类JedisPoolAbstract,而JedisPoolAbstract还有两个子类,分别是JedisPool、JedisSentinelPool

JedisPoolAbstract

jedis-3.8.0-sources.jar!/redis/clients/jedis/JedisPoolAbstract.java

/**
 * @deprecated This class will be removed in future. If you are directly manipulating this class,
 * you are suggested to change your code to use {@link Pool Pool&lt;Jedis&gt;} instead.
 */
@Deprecated
public class JedisPoolAbstract extends Pool<Jedis> {
  /**
   * Using this constructor means you have to set and initialize the internalPool yourself.
   *
   * @deprecated This constructor will be removed in future.
   */
  @Deprecated
  public JedisPoolAbstract() {
    super();
  }
  public JedisPoolAbstract(GenericObjectPoolConfig<Jedis> poolConfig,
      PooledObjectFactory<Jedis> factory) {
    super(poolConfig, factory);
  }
}

这个类未来将要被废弃,它主要是设置了Pool的泛型为Jedis

JedisPool

jedis-3.8.0-sources.jar!/redis/clients/jedis/JedisPool.java

public class JedisPool extends JedisPoolAbstract {
  //......
  @Override
  public Jedis getResource() {
    Jedis jedis = super.getResource();
    jedis.setDataSource(this);
    return jedis;
  }
  @Override
  public void returnResource(final Jedis resource) {
    if (resource != null) {
      try {
        resource.resetState();
        returnResourceObject(resource);
      } catch (RuntimeException e) {
        returnBrokenResource(resource);
        log.warn("Resource is returned to the pool as broken", e);
      }
    }
  }
}

JedisPool覆盖了getResource和returnResource方法,其中getResource新增了设置dataSource给jedis;returnResource方法新增了jedis的resetState操作,return有异常的话会执行returnBrokenResource

JedisSentinelPool

jedis-3.8.0-sources.jar!/redis/clients/jedis/JedisSentinelPool.java

public class JedisSentinelPool extends JedisPoolAbstract {
  @Override
  public Jedis getResource() {
    while (true) {
      Jedis jedis = super.getResource();
      jedis.setDataSource(this);
      // get a reference because it can change concurrently
      final HostAndPort master = currentHostMaster;
      final HostAndPort connection = new HostAndPort(jedis.getClient().getHost(), jedis.getClient()
          .getPort());
      if (master.equals(connection)) {
        // connected to the correct master
        return jedis;
      } else {
        returnBrokenResource(jedis);
      }
    }
  }
  @Override
  public void returnResource(final Jedis resource) {
    if (resource != null) {
      try {
        resource.resetState();
        returnResourceObject(resource);
      } catch (RuntimeException e) {
        returnBrokenResource(resource);
        log.debug("Resource is returned to the pool as broken", e);
      }
    }
  }
  //......
}

 JedisSentinelPool覆盖了getResource和returnResource方法,其中getResource新增了设置dataSource给jedis,然后判断master;returnResource方法新增了jedis的resetState操作,return有异常的话会执行returnBrokenResource

小结

jedis主要有三个对象对commons-pool进行包装,分别是JedisPoolConfig(继承了GenericObjectPoolConfig),JedisFactory(实现了PooledObjectFactory<Jedis>接口)、Pool(提供了get和return方法,内部委托给GenericObjectPool)

JedisPoolConfig继承了GenericObjectPoolConfig,在构造器里头设置了默认的参数,即testWhileIdle为true,minEvictableIdleTime为60s,timeBetweenEvictionRuns为30s,numTestsPerEvictionRun为-1

以上就是jedis连接池对commons-pool的封装示例详解的详细内容,更多关于jedis连接池封装commons-pool的资料请关注脚本之家其它相关文章!

相关文章

  • Mybatis逆向工程笔记小结

    Mybatis逆向工程笔记小结

    MyBatis官方为我们提供了一个逆向工程,通过这个逆向工程,只需要建立好数据表,MyBatis就会根据这个表自动生成pojo类、mapper接口、sql映射文件,本文主要介绍了Mybatis逆向工程笔记小结,具有一定的参考价值,感兴趣的可以了解一下
    2024-05-05
  • Java方法参数是引用调用还是值调用?

    Java方法参数是引用调用还是值调用?

    Java方法参数是引用调用还是值调用?这是一个值得思考的问题。阅读本文,找出答案
    2016-02-02
  • MyBatis 源码分析 之SqlSession接口和Executor类

    MyBatis 源码分析 之SqlSession接口和Executor类

    mybatis框架在操作数据的时候,离不开SqlSession接口实例类的作用,下面通过本文给大家实例剖析MyBatis 源码分析之SqlSession接口和Executor类,需要的朋友参考下吧
    2017-02-02
  • 学习Java的9张思维导图

    学习Java的9张思维导图

    这篇文章主要为大家详细介绍了学习Java的9张思维导图,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-03-03
  • 深入浅析MyBatis foreach标签

    深入浅析MyBatis foreach标签

    Mybatis foreach 标签用于循环语句,它很好的支持了数据和 List、set 接口的集合,并对此提供遍历的功能,本文给大家介绍MyBatis foreach标签的相关知识,感兴趣的朋友一起看看吧
    2021-09-09
  • SpringBoot Actuator未授权访问漏洞的排查和解决方法

    SpringBoot Actuator未授权访问漏洞的排查和解决方法

    Spring Boot Actuator 是开发和管理生产级 Spring Boot 应用程序的重要工具,它可以帮助你确保应用程序的稳定性和性能,本文给大家介绍了SpringBoot Actuator未授权访问漏洞的排查和解决方法,需要的朋友可以参考下
    2024-05-05
  • java并发使用CountDownLatch在生产环境翻车剖析

    java并发使用CountDownLatch在生产环境翻车剖析

    这篇文章主要为大家介绍了java并发使用CountDownLatch在生产环境翻车的示例剖析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • MyBatis-plus 模糊查询的使用

    MyBatis-plus 模糊查询的使用

    这篇文章主要介绍了MyBatis-plus 模糊查询的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • java稀疏数组的示例代码

    java稀疏数组的示例代码

    这篇文章主要介绍了java稀疏数组,稀疏数组,记录一共有几行几列,有多少个不同值,把具有不同值的元素和行里了及值记录在一个小规模的数组中,从而缩小程序的规模,对java稀疏数组相关知识感兴趣的朋友一起看看吧
    2022-07-07
  • spring 整合 mybatis 中数据源的几种配置方式(总结篇)

    spring 整合 mybatis 中数据源的几种配置方式(总结篇)

    因为spring 整合mybatis的过程中, 有好几种整合方式,尤其是数据源那块,经常看到不一样的配置方式,总感觉有点乱,所以今天有空总结下,感兴趣的朋友跟随脚本之家小编一起学习吧
    2018-05-05

最新评论