jedis連接池對(duì)commons-pool的封裝示例詳解
序
文本主要研究一下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<Jedis>} 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)文章
Java方法參數(shù)是引用調(diào)用還是值調(diào)用?
Java方法參數(shù)是引用調(diào)用還是值調(diào)用?這是一個(gè)值得思考的問題。閱讀本文,找出答案2016-02-02MyBatis 源碼分析 之SqlSession接口和Executor類
mybatis框架在操作數(shù)據(jù)的時(shí)候,離不開SqlSession接口實(shí)例類的作用,下面通過(guò)本文給大家實(shí)例剖析MyBatis 源碼分析之SqlSession接口和Executor類,需要的朋友參考下吧2017-02-02SpringBoot Actuator未授權(quán)訪問漏洞的排查和解決方法
Spring Boot Actuator 是開發(fā)和管理生產(chǎn)級(jí) Spring Boot 應(yīng)用程序的重要工具,它可以幫助你確保應(yīng)用程序的穩(wěn)定性和性能,本文給大家介紹了SpringBoot Actuator未授權(quán)訪問漏洞的排查和解決方法,需要的朋友可以參考下2024-05-05java并發(fā)使用CountDownLatch在生產(chǎn)環(huán)境翻車剖析
這篇文章主要為大家介紹了java并發(fā)使用CountDownLatch在生產(chǎn)環(huán)境翻車的示例剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08spring 整合 mybatis 中數(shù)據(jù)源的幾種配置方式(總結(jié)篇)
因?yàn)閟pring 整合mybatis的過(guò)程中, 有好幾種整合方式,尤其是數(shù)據(jù)源那塊,經(jīng)??吹讲灰粯拥呐渲梅绞剑偢杏X有點(diǎn)亂,所以今天有空總結(jié)下,感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧2018-05-05