欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jedis連接池對(duì)commons-pool的封裝示例詳解

 更新時(shí)間:2023年09月25日 10:44:14   作者:codecraft  
這篇文章主要為大家介紹了jedis連接池對(duì)commons-pool的封裝示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

文本主要研究一下jedis連接池對(duì)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,在構(gòu)造器里頭設(shè)置了默認(rèn)的參數(shù),即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聲明實(shí)現(xiàn)Closeable接口,它的構(gòu)造器根據(jù)GenericObjectPoolConfig和PooledObjectFactory來(lái)創(chuàng)建GenericObjectPool,它的getResource、returnResource、returnBrokenResourceObject、destroy方法內(nèi)部都是委托給了GenericObjectPool
它有一個(gè)實(shí)現(xiàn)類JedisPoolAbstract,而JedisPoolAbstract還有兩個(gè)子類,分別是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);
  }
}

這個(gè)類未來(lái)將要被廢棄,它主要是設(shè)置了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方法,其中g(shù)etResource新增了設(shè)置dataSource給jedis;returnResource方法新增了jedis的resetState操作,return有異常的話會(huì)執(zhí)行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方法,其中g(shù)etResource新增了設(shè)置dataSource給jedis,然后判斷master;returnResource方法新增了jedis的resetState操作,return有異常的話會(huì)執(zhí)行returnBrokenResource

小結(jié)

jedis主要有三個(gè)對(duì)象對(duì)commons-pool進(jìn)行包裝,分別是JedisPoolConfig(繼承了GenericObjectPoolConfig),JedisFactory(實(shí)現(xiàn)了PooledObjectFactory<Jedis>接口)、Pool(提供了get和return方法,內(nèi)部委托給GenericObjectPool)

JedisPoolConfig繼承了GenericObjectPoolConfig,在構(gòu)造器里頭設(shè)置了默認(rèn)的參數(shù),即testWhileIdle為true,minEvictableIdleTime為60s,timeBetweenEvictionRuns為30s,numTestsPerEvictionRun為-1

以上就是jedis連接池對(duì)commons-pool的封裝示例詳解的詳細(xì)內(nèi)容,更多關(guān)于jedis連接池封裝commons-pool的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Mybatis逆向工程筆記小結(jié)

    Mybatis逆向工程筆記小結(jié)

    MyBatis官方為我們提供了一個(gè)逆向工程,通過(guò)這個(gè)逆向工程,只需要建立好數(shù)據(jù)表,MyBatis就會(huì)根據(jù)這個(gè)表自動(dòng)生成pojo類、mapper接口、sql映射文件,本文主要介紹了Mybatis逆向工程筆記小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-05-05
  • Java方法參數(shù)是引用調(diào)用還是值調(diào)用?

    Java方法參數(shù)是引用調(diào)用還是值調(diào)用?

    Java方法參數(shù)是引用調(diào)用還是值調(diào)用?這是一個(gè)值得思考的問題。閱讀本文,找出答案
    2016-02-02
  • MyBatis 源碼分析 之SqlSession接口和Executor類

    MyBatis 源碼分析 之SqlSession接口和Executor類

    mybatis框架在操作數(shù)據(jù)的時(shí)候,離不開SqlSession接口實(shí)例類的作用,下面通過(guò)本文給大家實(shí)例剖析MyBatis 源碼分析之SqlSession接口和Executor類,需要的朋友參考下吧
    2017-02-02
  • 學(xué)習(xí)Java的9張思維導(dǎo)圖

    學(xué)習(xí)Java的9張思維導(dǎo)圖

    這篇文章主要為大家詳細(xì)介紹了學(xué)習(xí)Java的9張思維導(dǎo)圖,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • 深入淺析MyBatis foreach標(biāo)簽

    深入淺析MyBatis foreach標(biāo)簽

    Mybatis foreach 標(biāo)簽用于循環(huán)語(yǔ)句,它很好的支持了數(shù)據(jù)和 List、set 接口的集合,并對(duì)此提供遍歷的功能,本文給大家介紹MyBatis foreach標(biāo)簽的相關(guān)知識(shí),感興趣的朋友一起看看吧
    2021-09-09
  • SpringBoot Actuator未授權(quán)訪問漏洞的排查和解決方法

    SpringBoot Actuator未授權(quán)訪問漏洞的排查和解決方法

    Spring Boot Actuator 是開發(fā)和管理生產(chǎn)級(jí) Spring Boot 應(yīng)用程序的重要工具,它可以幫助你確保應(yīng)用程序的穩(wěn)定性和性能,本文給大家介紹了SpringBoot Actuator未授權(quán)訪問漏洞的排查和解決方法,需要的朋友可以參考下
    2024-05-05
  • java并發(fā)使用CountDownLatch在生產(chǎn)環(huán)境翻車剖析

    java并發(fā)使用CountDownLatch在生產(chǎn)環(huán)境翻車剖析

    這篇文章主要為大家介紹了java并發(fā)使用CountDownLatch在生產(chǎn)環(huán)境翻車的示例剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • MyBatis-plus 模糊查詢的使用

    MyBatis-plus 模糊查詢的使用

    這篇文章主要介紹了MyBatis-plus 模糊查詢的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • java稀疏數(shù)組的示例代碼

    java稀疏數(shù)組的示例代碼

    這篇文章主要介紹了java稀疏數(shù)組,稀疏數(shù)組,記錄一共有幾行幾列,有多少個(gè)不同值,把具有不同值的元素和行里了及值記錄在一個(gè)小規(guī)模的數(shù)組中,從而縮小程序的規(guī)模,對(duì)java稀疏數(shù)組相關(guān)知識(shí)感興趣的朋友一起看看吧
    2022-07-07
  • spring 整合 mybatis 中數(shù)據(jù)源的幾種配置方式(總結(jié)篇)

    spring 整合 mybatis 中數(shù)據(jù)源的幾種配置方式(總結(jié)篇)

    因?yàn)閟pring 整合mybatis的過(guò)程中, 有好幾種整合方式,尤其是數(shù)據(jù)源那塊,經(jīng)??吹讲灰粯拥呐渲梅绞剑偢杏X有點(diǎn)亂,所以今天有空總結(jié)下,感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧
    2018-05-05

最新評(píng)論