最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

jedis連接池對commons-pool的封裝示例詳解

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

文本主要研究一下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,在構(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來創(chuàng)建GenericObjectPool,它的getResource、returnResource、returnBrokenResourceObject、destroy方法內(nèi)部都是委托給了GenericObjectPool
它有一個實(shí)現(xiàn)類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);
  }
}

這個類未來將要被廢棄,它主要是設(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有異常的話會執(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有異常的話會執(zhí)行returnBrokenResource

小結(jié)

jedis主要有三個對象對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連接池對commons-pool的封裝示例詳解的詳細(xì)內(nèi)容,更多關(guān)于jedis連接池封裝commons-pool的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 解決rror updating database.Cause:java.sql.SQLSyntaxErrorException問題

    解決rror updating database.Cause:java.sql.SQLSyntaxE

    這篇文章主要介紹了解決rror updating database.Cause:java.sql.SQLSyntaxErrorException問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • Java ArrayList如何實(shí)現(xiàn)生成不重復(fù)隨機(jī)數(shù)

    Java ArrayList如何實(shí)現(xiàn)生成不重復(fù)隨機(jī)數(shù)

    這篇文章主要介紹了Java ArrayList如何實(shí)現(xiàn)生成不重復(fù)隨機(jī)數(shù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-09-09
  • SpringBoot中日志切面實(shí)現(xiàn)小結(jié)

    SpringBoot中日志切面實(shí)現(xiàn)小結(jié)

    本文介紹了SpringBoot中日志切面實(shí)現(xiàn)小結(jié),通過定義一個自定義注解和創(chuàng)建一個日志切面類,為方法添加日志記錄功能,感興趣的可以了解一下
    2024-11-11
  • 關(guān)于@Value注解失效的原因分析

    關(guān)于@Value注解失效的原因分析

    這篇文章主要介紹了關(guān)于@Value注解失效的原因分析,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • spring boot環(huán)境抽象的實(shí)現(xiàn)方法

    spring boot環(huán)境抽象的實(shí)現(xiàn)方法

    在實(shí)際開發(fā)中,開發(fā)人員在編寫springboot的時候通常要在本地環(huán)境測試然后再部署到Production環(huán)境,這兩種環(huán)境一般來講是不同的,最主要的區(qū)別就是數(shù)據(jù)源的不同。本文主要介紹了這兩種,感興趣的可以了解一下
    2019-04-04
  • spring mvc DispatcherServlet之前端控制器架構(gòu)詳解

    spring mvc DispatcherServlet之前端控制器架構(gòu)詳解

    這篇文章主要為大家詳細(xì)介紹了spring mvc DispatcherServlet之前端控制器架構(gòu),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • SpringBoot獲取ApplicationContext的3種方式

    SpringBoot獲取ApplicationContext的3種方式

    這篇文章主要為大家詳細(xì)介紹了SpringBoot獲取ApplicationContext的3種方式,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • SpringBoot項(xiàng)目使用aop案例詳解

    SpringBoot項(xiàng)目使用aop案例詳解

    這篇文章主要介紹了SpringBoot項(xiàng)目使用aop的相關(guān)知識,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-04-04
  • JSON Web Token(JWT)原理入門教程詳解

    JSON Web Token(JWT)原理入門教程詳解

    這篇文章主要為大家介紹了JSON Web Token(JWT)原理入門教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪
    2022-04-04
  • SpringMVC框架搭建idea2021.3.2操作數(shù)據(jù)庫的示例詳解

    SpringMVC框架搭建idea2021.3.2操作數(shù)據(jù)庫的示例詳解

    這篇文章主要介紹了SpringMVC框架搭建idea2021.3.2操作數(shù)據(jù)庫,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04

最新評論

城固县| 芦溪县| 临泽县| 牡丹江市| 旌德县| 淄博市| 景谷| 绥宁县| 内丘县| 马尔康县| 新沂市| 阳谷县| 都江堰市| 南投市| 筠连县| 正宁县| 伊川县| 湟中县| 托克逊县| 邹平县| 舟山市| 鹰潭市| 清河县| 广平县| 延长县| 绥阳县| 上蔡县| 桐梓县| 丰顺县| 綦江县| 上杭县| 陇西县| 舞阳县| 藁城市| 万州区| 西昌市| 永修县| 沁阳市| 玛曲县| 克拉玛依市| 伽师县|